List files and directories.
ls [options]
Options:
- l - long listing format
- a - show all files (including hidden files)
Change current directory (move to another directory)
cd directory-path
Copies a directory or file to destination
cp source destination
Moves a source from one path to another. This command is also used to rename directories and files.
mv path1 path2
mv filename1 filename2
Removes a file(s) or directory(s).
rm file1 file2
rm -r directory1
Options:
- f - force deletion without any prompt
Create a file. If file already exist, update dates accessed and modified
touch filename
Determine filetype of file(s).
file filename
Options:
- i - output MIME type strings
Compares files line by line and displays the difference.
diff file1 file2
Concatenate FILE(s) to standard output. It can be used to display text files on screen, create, copy, merge files.
cat [option] file
cat file1 file2
cat file1 file2 > newmergedfile
cat < file1 > file2 #copy file1 to file2
Displays output one screen at a time. Move to next page by pressing space
or press q
to quit.
more filename
Similar to more
but allows both forward and backward movements. You can also use arrow keys to move one line at a time.
less filename
Prints kernel information on the screen.
uname [option]
uname -a
Lists your running processes & their status.
ps [option]
ps -a
ps -u user
Lists the jobs running in the background along with the job number.
jobs
Displays file system disk space usage.
df
Displays the disk usage of files and/or directories.
du [options] filename|dirname
du -sh /sbin/file1
Options:
- h - Human readable format (size is displayed in KB, MB, GB)
- a - display all (shows disk space for directories at every level and also individual files)
- s - Suppress or Summarize (only shows total disk space occupied)
Displays a dynamic real-time view of a running system with the list of active processes.
top
Shows listing of last logged in user(s).
last
last username
Displays the disk quota (limits) and usage.
quota
Sends a signal to a process. Commonly used to terminate a process.
kill [signal] PID
Kill all processes by name.
killall processname
Options:
- e - Require an exact match for long names
- i - Interactively ask for confirmation before killing
- q - Quiet mode
Moves a background job to foreground
fg [job-id]
Runs a job in the background.
bg [job-id]
nohup i.e. No Hangup, runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out.
nohup command [arg...]
Used to test if a host is reachable. Sends ICMP ECHO_REQUEST to network host(s).
ping [option] host
ping host
ping -t host
Domain information groper, DNS lookup utility.
dig domain [options] [query]
Shows who is logged on and what they are doing.
w [option] [user]
Shell is a user program or an environment provided for user interaction. Shell is a command language interpreter that executes commands read from the standard input device (keyboard) or from a file.
Shell is not part of system kernel but uses the system kernel to execute programs, create files etc.
Several shells available with Linux including:
- SH (POSIX shell or Bourne shell)
- BASH ( Bourne-Again SHell )
- CSH (C SHell)
- KSH (Korn SHell)
- TCSH (TENEX/TOPS C shell)
A shell script begins with a #!
commonly referred to as shebang, also known as sha-bang, hashbang, pound-bang or hash-pling.
It's syntax follows:
#!interpreter [optional-arg]
#!/bin/sh
#!/bin/bash
#!/bin/csh -f
Syntax:
[interpreter] script-name
./script-name
On your terminal type:
bash scriptA
sh scriptA
./scriptA
Variables in shell programming are typeless (no data types). A valid variable name can consist of characters, numbers, hyphens and underscores. There can be no spaces around the "=" assignment sign: that is VAR=value
is valid; VAR = value
is not.
Variables can be accessed by prepending $
before the VARIABLE_NAME
.
MY_MESSAGE="HELLO!"
echo $MY_MESSAGE
There are 4 types of quotes with Shell programming:
Quote | Name | Description |
---|---|---|
" | Double Quote | Evaluates some special charactes ($,`,\$,\',\" and \\ ) & does command substitutions but not meta-characters |
' | Single Quote | No special characters are expanded |
` | Back Quote | Execute everything in between back quotes |
\ | Backslash | Everything immediately following a backslash will not be executed |
Shell supports Array variable. They hold multiple values just like most programming languages. Following are the ways to set values to ARRAY variable:
ARRAY_NAME[Index]=Value
ARRAY_NAME=([INDEX1]=VALUE1 [INDEX2]=VALUE2)
ARRAY_NAME=(VALUE1 VALUE2)
To get a specific value from the ARRAY:
${ARRAY_NAME[Index]}
To get all the values from the ARRAY:
${ARRAY_NAME[*]}
${ARRAY_NAME[@]}
Functions are sequence of code that are grouped together to execute some shell commands in order to return/output some values. Functions may change the state of variable(s) and even exit the shell script execution too. They are defined as:
function myFunction {
# Perform some operations
# echo/return/exit
}
Bash provides 3 types of loops: for
, while
and until
.
for VARIABLE in 1 2 3 4 5 .. N
do
statement
done
for VARIABLE in file1 file2 file3
do
statement
done
for OUTPUT in $(command)
do
command on $OUTPUT
done
OR
for (( EXPRESSION_1; EXPRESSION_2; EXPRESSION_3 ))
do
statement
done
OR with Bash v4.0+
for i in {START..END..INCREMENT}
do
statement
done
while condition; do
statement
done
until condition; do
statement
done
Like other programming languages, bash also supports conditional statements. A sequence of statements can execute if a condition is true.
Syntax:
if [expression1]; then
# executed if expression1 is true
statement
elif [expression2]; then
# executed if expression1 is false but expression2 is true
statement
else
# executed if all the above expressions are false
statement
fi