Basic
BASH Commands:
ls: Displays
the content of a directory
If no external option is given, it will display the
contents of present working directory.
Eg: ls ./abc
ls
ls ~/user
- a : Option to print all files(including hidden files)
- l : Option to give a long of file details.
Eg: ls -a
ls -al
ls -l /
NB: ~ : Denotes the home directory of current user
/ : Denotes the root directory of the Linux System.
Eg: ls /
ls -a ~/
pwd:
Prints the current(present)
working directory.
cd
<directory> : Changes the
working directory to the directory specified.
Eg: cd ~
cd /
NB: cd .. will make the terminal to move to the parent
directory of present working directory.
cd . Will make
the terminal to move to the current directory itself.
passwd : Changes
the password of current user account.
touch <filename> : Creates
a new file
Eg: touch abc.txt
touch ~/abc.txt
nano : A
simple text editor
cp <file_src> <file_dest> : Copies
a source file to a destination directory with or without renaming
(File will be renamed if file_dest is not a directory).
-r or -R or –recursive : Recursive copy or copy all the
subdirectories as well.
Eg: cp ./abc.txt ./dir2/efg.txt
cp -r ./abc ./dir2
mkdir <dir_name> :
Creates a new directory.
-p, --parents : Creates intervening parent directories if they do
not exist.
mv <source> <dest> : Cut
and paste source file to the destination and rename if dest is not a
directory. mv is used also for renaming a file.
Eg: mv ./abc/sample.txt ./efg
mv ./efg/sample.txt ./efg/sample2.txt
rmdir <dir_name> : Deletes
an empty directory.
rm <file_name>: Removes
a file.
-r option can be used to remove directories as well recursively.
-i option can be used to warn before deleting.
command --help : Displays
a simple help page for a command.
man <command> : To get
the manual page for a command. Press 'q' to exit.
info <command> : To
get the info page for a command.
whatis <command> : gives
a short description about the command.
Eg: whatis ls
apropos <keyword> : search
the manual page names and descriptions for commands
Eg: apropos editor
time : Displays the current
system time.
cal : Displays the calender
for the current month.
Eg: cal #Calender for the current month
cal 2 2013 #Calender for February 2103
cal 2012 #Calender for the year 2012
ps : Displays the processes
created by the current user.
who : Displays all the users
currently logged into the system.
w : Displays all the users
currently logged into the system and their resours utilization.
cat : Display the content of
a file on to the screen.
wc : Word Count – Counts
the number of lines, words and characters in a text file.
Eg: wc sample.txt
which <command> : Displays
the actual location of the command / application.
exit or logout :
Terminates the current session.
Redirecting output of a command to file:
Eg: ls -al > new.txt
Typing two or more commands in a single line:
Eg: ls -al ; mkdir abc
Comments given after commands:
Eg: ls -al #Long listing of pwd contents.
echo “<TEXT>” : Prints
a text into console.
Eg: echo “Hello World”
su : Used to change user
account, either to super user or any other user account.
NB: In Ubuntu based linux systems,
we use sudo (SUper
User DO) to exicute a command with root privilage.
Eg: sudo mkdir /abc
sudo apt-get update
script
---------------------------
--------------------------- :
Records the BASH Session to the file typescript
---------------------------
exit
sort <filename> : Sorts
a text file
uname -a : Prints the
information about the linux installed in the system.
lsb_release -a
: Prints the version information about the linux installed.
Keyboard Shortcuts:
-
Key Combination
|
Use
|
Ctrl + D
|
Same as exit , exits
or terminates current session
|
Ctrl + C
|
Closes the currently running program in foreground
|
Ctrl + Z
|
Suspends the currently running program
|
Ctrl + H
|
Same as backspace
|
Ctrl + L
|
Clears the terminal
Same as clear command
|
TAB
|
Predicts and completes a command
|
TAB TAB
|
Lists all command completion possibilities.
|
Files
& Directories in Linux Systems:
In Linux all the things excepts processes are generally
considered as files(devices, directories, etc).
Directories can be treated as special file that
contains the list of files contained in it.
In
the tree structure for directory all the directories contains two
pointers . and .. which
points to the current directory and parent directory respectively.
All the files also have an inode number which along
with . And .. pointers identifies a file in the directory tree
structure. Give -i option along ith ls to get the inode number as
well.
Hidden files have names preceding with a '.'
Important
Directiories in / (Root Directory):
bin : Common files shared by system, administrator and
users.(Eg: commands)
boot : vmlinuz(Kernel Files), GRUB
dev : References to hardware peripherals.
etc : Configuration files(Similar to the control panel
in windows)
home : Directories and home folders for users.
initrd : Information needed during booting.
lib : Library Files
lost + found : Files saved during failures.
var :Variable files like log details, server pages,
temporary variables etc.
usr : Programs, libraries, documentation for all user
related programs.
media : Mount points for media devices.
df
-h : Gives information about
the partitions.
-h : Human readable, ie. Prints the details in a way
easily unstaandable by human beings.
df
-h . : Give the details of
the disk partition that contains the present working directory.
du -h :
Prints
the disk usage details for the current directory.
which
<command>: Give
the absolute path for the command / program.
Eg:
which ls
/bin/ls
$PATH
is an environment variable that contains the list of directories
which has executable files in it. So if we give a command it will
search all the folders in the $PATH variable and if it is there then
it will get executed, else it will print and error “Command not
found..”.
echo
$PATH : Prints
the content of PATH variable.
We
can temporarily change the $PATH variable using export
command.
Eg:
export
PATH=/usr/local/bin:/bin:/usr/sbin
or
export
PATH=$PATH:/usr/sbin
$HOME
– Contains
the home folder for current user.
Eg:
echo $HOME
file <file_name>
: Displays the type of the file.
cmp
<file_1> <file_2> :
Compares two files
Searching
in Linux:
Wild char symbols:
* : Matches any number of characters
? : Matches exactly one character.
[a-z] : Matches any character within the specified
range.
Eg: ls *.txt
mv ./abc/*.txt ./abc/*.c
find <path> -name
<search_string>
find <path> -size <size>
Eg: find /temp -name “abc.txt”
find . -size +50k
locate
<search_string> : Similar
to find but more friendly and less efficient.
~/.bash_history : is a file that contains the bash usage
history.
grep:
Command used to filter input
lines and return only some patterns.
Eg: grep find ~/.bash_history
grep apple ./fruits.txt
NB: /usr/share/dict/words is a dictionary containing all
the English words
Eg: grep man /usr/share/dict/words
cat : Concatenates
and displays the output of one or more files to the screen. Output
displayed in an uncontrolled way.
more : Displays
the output with scrolling downward option.
nano : A simple
text editor.
head : Display
first few lines of a text file.
tail : Display
last few lines of a text file.
File and Directory Permissions:
For each file any users can be classified into any of
the three categories:
ls
-l gives
a detailed listing along with the user and privilege details. There
are separate privileges for read(r), write(w) and execute(x) for
user, group user and other users.
chmod
: Used
to change the user privileges of any file.
Use -r or –recursive to apply privileges excursively
to all subfiles in a directory.
Eg: chmod u+x abc.txt #Add user the privilege to
execute.
chmod
ug+rwx abc.txt #Add
user and group user the privilege to read,write andexecute.
chmod o-x abc.txt #Deducing the power of other
user to execute the file.
chmod 777 abc.txt #Give all privileges to all
categories of users.
Processes:
A program executing in memory is called a process.
Using a terminal we can execute a process in:
<regular_command> :
Runs
the command in background.
<regular_command>
& :
Runs the command in background.
jobs :
Command
to display all the jobs running in background.
Ctrl
+ Z : Suspends
a program running in foreground.
Ctrl
+ C :
Terminate and quit a process running in foreground.
%n :
Every process running in background has got a unique number, we can
get that using job
command.
bg
<%n> :
Reactivate a suspended program in background.
fg
<%n> :
Puts the job back in background.
kill
<%n> :
Terminates a background process.
All the process has got a unique identification number
called PID or Process ID.
A process is being created by another by the process of
forking in which the address space of one process is being cloned.
Every process also have a Parent Process Id or PPID.
ps :
Displays
the processes created by the current user.
-f : Option to display PPID as well.
-e or -A : Display all the system process as well.
-u <username> : diplay all the processes created
by any particular user.
Eg:
ps -e | grep tty
ps -e
kill
<PID> :Terminates
a process.
top :
Displays
the currently running tasks and their CPU utilization.
nohup
<command>
: Executes the command even if we log out of the system and writes
the output to the file nohup.out.
NB: wget is used to download a file.
pstree :
Displays
all currently running processes along with their parent child
relationships.
time
<command>:
Executes the command and displays how much time it takes.
netstat :
Displays
the current network usage and traffic.
vmstat :
Displays
the current virtual memory usage.
sleep <seconds> :
Do nothing for sometime.
Eg: sleep 5
(sleep 1800 ; echo “Lunch Time”) &
I/O Redirection:
> : Used to direct the output to a file.
>> : Used to append the output to a file.
| : Used to take the output of one command to another.
Eg: ls -al > ls.list
top > current.txt
Package Managers:
Redhat Package Manager : RPM
installing an rpm:
rpm -Uvh <path>
-U : Upgrade
-v : Generate more verbose output
-h : Gives a progress bar of installation
To check whether a program is being installed:
rpm -qa | grep gimp
Uninstalling:
rpm -e application
Debian Package Manager : dpkg
To install an application:
sudo dpkg -i package.deb
To check version and details of an installed
application:
dpkg -l application
OR
dpkg -l *application*
APT – Advanced Package Manager
sudo apt-get
update : Will update the
database with list of available packages.
sudo apt-get
install package :
Installs a new package.
sudo apt-get
upgrade : Performs and
upgrade.
ssh
user@host : Secure
Shell Login to a remote server
write
user [tty] : Used
to send messages to other users.
Using
gcc:
gcc
<filename.c> -o <output_filename>
./output_filename
Different
Shells:
sh :
Traditional Unix Shell or Bourne Shell
bash :
Bourne Again Shell, the default linux shell
ksh :
Korn Shell
csh :
C-Shell
tcsh :
Turbo C Shell
$SHELL
: Environment
variable that contains the current shell
/etc/shells -
File containing the list of available shells.
Shell
scripts starts with
#!
/bin/bash
so
that when they are executed, it will be using BASH by default.
Eg:
#!
/bin/bash
echo
“Hello World!..”
Save
it as sample.sh and give chmod +x sample.txt
Execute
it as: ./sample.sh