1 LinuxCommands ShellProgramming
1 LinuxCommands ShellProgramming
Mehmet Kaya
mehmet.kaya@marmara.edu.tr
Fall 2023
Special notations:
. – current directory
.. – parent directory
/ – root
Linux Commands
• ls - (List)
• To list the files in the working directory, use the ls command.
Most commands operate like this:
command -options arguments
Command Result
ls List the files in the working directory
ls /bin List the files in the /bin directory (or any other directory
you care to specify)
ls -l List the files in the working directory in long format
ls -l /etc /bin List the files in the /bin directory and the /etc directory in
long format
ls -al List all files (including hidden files) in the working
directory in long format
A Closer Look At Long Format
• If you use the -l option with ls you will get a file listing
that contains a wealth of information about the files
being listed.
Manipulating Files
• cp - (Copy)
• Copy files and directories
• mv - (Move)
• Move or rename files and directories
• rm- (Remove)
• Remove files and directories
• mkdir- (Make Directory)
• Create directories
• rmdir- (Remove Directory)
• Remove directories
Wildcards
• Wildcards allow you to select filenames based on patterns of characters.
* - Matches any characters
? - Matches any single character
[characters] - Matches any character that is a member of the set characters.
[!characters] - Matches any character that is not a member of the set characters
Pattern Matches
* All filenames
a* All filenames that begin with the character "a"
b*.txt All filenames that begin with the character "b" and end
with the characters ".txt"
Data??? Any filename that begins with the characters "Data"
followed by exactly 3 more characters
[abc]* Any filename that begins with "a" or "b" or "c" followed
by any other characters
cp - Copy
Syntax:
cp Source Destination
Command Result
cp file1 file2 Copies the contents of file1 into file2. If file2 does not
exist, it is created; otherwise, file2 is silently
overwritten with the contents of file1.
cp -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, if file2 exists, the user is prompted before it is
overwritten with the contents of file1.
cp file1 dir1 Copy the contents of file1 (into a file named file1) inside
of directory dir1.
cp -R dir1 dir2 Copy the contents of the directory dir1. If
directory dir2 does not exist, it is created. Otherwise, it
creates a directory named dir1 within directory dir2.
mv - Move
Syntax:
mv Source Destination
Command Result
mv file1 file2 If file2 does not exist, then file1 is
renamed file2. If file2 exists, its contents are silently
replaced with the contents of file1.
mv -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, if file2 exists, the user is prompted before it is
overwritten with the contents of file1.
mv file1 file2 file3 The files file1, file2, file3 are moved to directory dir1.
dir1 If dir1 does not exist, mv will exit with an error.
mv dir1 dir2 If dir2 does not exist, then dir1 is renamed dir2.
If dir2 exists, the directory dir1 is moved within
directory dir2.
rm - Remove
Command Result
rm file1 Delete file1
rm file1 file2 Delete file1 and file2.
rm -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, the user is prompted before each file is deleted.
rm -r dir1 dir2 Directories dir1 and dir2 are deleted along with all of their
contents.
rm - Remove Be careful with rm!
Command Result
rm file1 Delete file1
rm file1 file2 Delete file1 and file2.
rm -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, the user is prompted before each file is deleted.
rm -r dir1 dir2 Directories dir1 and dir2 are deleted along with all of their
contents.
rm - Remove Be careful with rm!
Command Result
rm file1 Delete file1
rm file1 file2 Delete file1 and file2.
rm -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, the user is prompted before each file is deleted.
rm -r dir1 dir2 Directories dir1 and dir2 are deleted along with all of their
contents.
Command Result
rm file1 Delete file1
rm file1 file2 Delete file1 and file2.
rm -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, the user is prompted before each file is deleted.
rm -r dir1 dir2 Directories dir1 and dir2 are deleted along with all of their
contents.
cp *.txt text_files
rm *~
Using Commands With Wildcards..
Command Result
Copy all files in the current working directory with names ending with the
cp *.txt text_files
characters ".txt" to an existing directory named text_files .
rm *~
Using Commands With Wildcards..
Command Result
Copy all files in the current working directory with names ending with the
cp *.txt text_files characters ".txt" to an existing directory named text_files .
Move the subdirectory my_dir and all the files ending in ".bak" in the current
working directory's parent directory to an existing directory named
mv my_dir ../*.bak my_new_dir
my_new_dir .
rm *~
Using Commands With Wildcards..
Command Result
Copy all files in the current working directory with names ending with the
cp *.txt text_files
characters ".txt" to an existing directory named text_files .
Move the subdirectory my_dir and all the files ending in ".bak" in the current
mv my_dir ../*.bak my_new_dir working directory's parent directory to an existing directory named
my_new_dir .
Delete all files in the current working directory that end with the character "~".
rm *~
File Permissions & chmod
chmod - change the permissions of a file.
Syntax:
chmod Permissions FileName
File Permissions & chmod
chmod - change the permissions of a file.
Syntax:
chmod Permissions FileName
• Standard Input
• To redirect standard input from a file instead of the keyboard,
the "<" character is used.
• sort < file_list.txt
• sort command is used for sorting the contents of file_list.txt.
I/O Redirection
• Standard Output
• To redirect standard output to a file, the ">" character is used.
• ls > file_list.txt
• Each time the command above is repeated, file_list.txt is overwritten from
the beginning with the output of the command ls.
• If you want the new results to be appended to the file instead, use ">>".
• ls >> file_list.txt
• Standard Input
• To redirect standard input from a file instead of the keyboard, the "<"
character is used.
• sort < file_list.txt
• sort command is used for sorting the contents of file_list.txt.
• ls -l | sort | wc -l
Example:
$ no=10 # this is ok
$ 10=no # Error, NOT Ok, Value must be on right side of = sign.
• Examples:
$ expr 1 + 3
$ expr 2 – 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 6 + 3` # Use before and after expr keyword
` (back quote) sign
not the (single quote i.e. ') sign.
Exit Status
• By default in Linux if particular command/shell script is executed,
it return two type of values which is used to see whether command
or shell script executed is successful or not.
1. If return value is zero (0), command is successful.
2. If return value is nonzero, command is not successful or some sort of error
executing command/shell script.
Exit Status
• By default in Linux if particular command/shell script is executed,
it return two type of values which is used to see whether command
or shell script executed is successful or not.
1. If return value is zero (0), command is successful.
2. If return value is nonzero, command is not successful or some sort of error
executing command/shell script.
• Examples:
• $ ls
$ echo $? # It will print 0 to indicate command is successful.
$ rm unknownfile
$ echo $? # It will print nonzero value to indicate error.
The read Statement
• Use to get input (data from user) from keyboard and store (data)
to variable.
• Syntax:
read variable1, variable2,...variableN
The read Statement
• Use to get input (data from user) from keyboard and store (data)
to variable.
• Syntax:
read variable1, variable2,...variableN
Example4:
#
# Script to read your name from key-board
#
echo "Your first name please: "
read fname
echo "Hello $fname, Lets be friend!"
The read Statement
• Use to get input (data from user) from keyboard and store (data)
to variable.
• Syntax:
read variable1, variable2,...variableN
Example4:
#
# Script to read your name from key-board
#
echo "Your first name please: "
read fname
echo "Hello $fname, Lets be friend!"
Execute script as:
$ chmod 755 Example4.sh
$ ./Example4.sh
Command Line Arguments
Operator Meaning
string1 = string2 string1 is equal to string2
string1 != string2 string1 is NOT equal to string2
-n string1 string1 is NOT NULL and does exist
-z string1 string1 is NULL and does exist
Example 6:
#
# Script to see whether argument is positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one integers"
exit 1
fi
if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi
Example 6:
#
# Script to see whether argument is positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one integers"
exit 1
fi
if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi
Example 8
#
# Script to test for loop
#
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
Loops in Shell Scripts
• Even you can use following syntax:
Syntax:
for (( expr1; expr2; expr3 ))
do
repeat all statements between do and
done until expr2 is TRUE
done
Loops in Shell Scripts
• Even you can use following syntax:
Syntax:
for (( expr1; expr2; expr3 ))
do
repeat all statements between do and
done until expr2 is TRUE
done
Example 9
#
# Script to test for loop
#
for (( i = 0 ; i <= 5; i++ ))
do
echo "Welcome $i times"
done
Loops in Shell Scripts
• while Loop
Syntax:
while [ condition ]
do
command1
command2
....
....
done
Loops in Shell Scripts
• while Loop Example 10:
Syntax:
while [ condition ] #
do #Script to test while statement
command1 #
command2 #
.... if [ $# -eq 0 ]
.... then
echo "Error - Number missing form command line argument"
done
echo "Syntax : $0 number"
echo " Use to print multiplication table for given number"
exit 1
fi
n=$1
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n` "
i=`expr $i + 1`
done
The case Statement
Syntax:
case $variable-name in
pattern1) command
...
...
command ;;
pattern2) command
...
...
command ;;
patternN) command
...
...
command ;;
*) command
...
...
command ;;
esac
Example 11:
#
#
# Script to test case statement
#
#
if [ -z $1 ]
then
rental="*** Unknown vehicle ***"
elif [ -n $1 ]
then
# otherwise make first arg as rental
rental=$1
fi
case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
*) echo "Sorry, I can not gat a $rental for you";;
esac
Example 11:
# Execute script as:
# $ chmod 755 Example11.sh
# Script to test case statement $ ./Example11.sh car
# $ ./Example11.sh van
# $ ./Example11.sh jeep
$ ./Example11.sh Maserati
if [ -z $1 ]
then
rental="*** Unknown vehicle ***"
elif [ -n $1 ]
then
# otherwise make first arg as rental
rental=$1
fi
case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
*) echo "Sorry, I can not gat a $rental for you";;
esac
More Examples:
• Menu.sh
• ArabicToRoman.sh