Workshop On LINUX Shell Commands, Shell Script and System Calls
Workshop On LINUX Shell Commands, Shell Script and System Calls
Workshop On LINUX Shell Commands, Shell Script and System Calls
ls stands for List and this command gets system to display the list of files in the directory
you are referring to. The default directory is your current directory.
$ ls
The ls command lists the all files and subdirectories of your current working directory.
Listing Options
Option Action
E.g. $ ls ?
Introductory Linux Workshop 1
SCET, Surat
20th March 2009
Display all files/ directories having single character name.
$ ls *.c
$ ls game*
Display all files with names starts with game keyword like games1, games2,
games11, gameskid etc.
2) cat
This command is useful to create file as well as to display content of files. It is also
useful to append new data into files.
This command create file abc.txt. Here > sign is know as redirection operator. When
use this command, system will create blank abc.txt file and gives you space to write
content. At the end of file press ctrl + Z to save and exit from file. If you try same
command again, system will remove all content from file and open blank file with same
name.
$ cat abc.txt
This command displays the content of file(s). You can see contents of more than one file
using same command.
This command displays the content of abc.txt followed by the content of xyz.txt.
3) cp
Copy the content of an existing file into another file in the same directory or in the other
directory.
$ cp birds birds1
Copy content the file birds into another directory having name student which is
currently in home directory. The name of the file in target directory remains same. To
copy a file into another directory full path name of the target file is needed.
4) rm
$ rm abc.txt
$ rm abc.txt xyz.txt
5) mv
$ mv birds birds1
This command will rename file birds with birds1. Here, birds1 is available in same
directory.
$ mv birds ./new/birds1
This command will move file birds to new directory under current working directory
with the new name birds1.
6) wc
$ wc file1
This command will display number of lines, number of words and number of
characters in the files and name of that file.
Option Action
7) mkdir
$ mkdir CO
$ mkdir CO IT
This command creates two new directory having name CO and IT under the current
directory.
8) rmdir
$ rmdir CO
$ rmdir CO IT
9) cd
Change directory
$ cd CO
$ cd /home/student/s1
$ cd ..
$ pwd
/home/student/user1
This command displays the directory you are working in. For the above output, we can
say that we are in directory user1 which is under subdirectory student under directory
home. And home is under root /.
11) Pg
$ pg abc.txt
If you have a file that is a large text file, and if you wanted to go through it without
using an editor it would be very difficult. To do so, you can use pg command.
After displaying one screenful of text, the system will pause and let you read the text.
Once you are through, press enter key and the next screenful will be displayed. If oyu
press q, the system will come out of the pg command and display system prompt.
12) echo
Display argument
Hor r you
Echo can also display content of variable if the variable name is preceded with a $ .
$ echo $HOME
13) ps
$ ps
Introductory Linux Workshop 5
SCET, Surat
20th March 2009
This command will display the status of all the currently running process. It will
display four basic information. That is PID (Process Identifier), TTY (Terminal
controlling the process), TIME(Execution time), COMD (The command).
14) date
$ date
$ date [MMddhhmmyy]
This command is used to set system date. Here, MM is Month, dd is Date, hh is Hour,
mm is Minute yy is Year
15) cal
Display Calender.
$ cal 3
$ cal 2009
16) who
$ who
This command will list out all user who have currently logged in including yourself.
The output displays four column output : user name, terminal number, date and time
of login.
17) who am i
This command display the same output as who. But it includes the detail about you
only.
18) grep
Introductory Linux Workshop 6
SCET, Surat
20th March 2009
pattern search
This will display all the lines from files having the pattern(word) hello.
We can also use regular expression with grep command. Regular expression contains
wild cards (*,? etc).
This command will display all lines having words with ll in them like hell, hello,
smell, balloon etc.
19) sort
Sort files base on one key field if your file contains records. Otherwise, it will sort
files based on first character on each line.
$ sort file1
when the number of fields are more than one, the key fields are denoted as number in
command.
here +1 is starting key and -2 is end key and out.txt is output file name which you can
use with o option. file1 is source file name.
20) cut
This command is cut out some character(s) or some field(s) from source file.
$ cut c 2 file1
This command will display or cut out 2nd character from source file file1.
$ cut d f 1 file1
21) comm
File Comparison
This command is used to compare two files and it generated three column output.
First column shows the unmatched content from first file, second column shows the
unmatched content from second file and last column shows the common contents
from both files.
22) cmp
File Comparison
This command compares the files and reports at which point the files differ. The
output is in the form of bytes and line number.
23) chmod
Mode of file means the mode in which we can access the file. Using this command
we can change the permission of the file.
In Unix/Linux, any file is having three types of permission (read, write, execute).
Also three types of users are available : Owner, Group and Other
r w x r w x r w x
For read the privilege is 4, for write the privilege is 2 and for execute the privilege is
1.
Now to give write permission to Group users on file file1 , we need command
24) passwd
$ passwd
25) expr
$a=20
$b=10
$expr $a + $b
30
$expr $a /* $b
200
Shell Variable
The shell allows you to define variables. A variable name can begin with any letter or
underscore and can have a total of more than one character. Variable name is case-
sensitive.
To assign value to a variable in shell programming is very simple. The rule is no space
should be there between the variable name and = sign and the value assigned.
e.g
$name=Ram
If you want white space between the value, value should be enclosed in the string quotes
like
$name=Ram Kumar
Ram Kumar
Shell Script
$ sh {filename} <Ret>
The script should be interactive means it must prompt the user to enter the information
needed. Alternatively, the script should accept command line arguments.
In UNIX environment, all command line arguments are known as positional parameters.
script1.sh
read command
This command is useful to accept user input. Input may be any type string or number. We
can also accept multiple values using single read command.
Script2.sh
set command
The set command is part of the shell and is used very effectively for command
substitution. If a particular command is enclosed in back quotes ` in conjunction with
echo, the echo will display the output of the enclosed command.
$echo $1 <Ret>
Sat
so $1 is Mon
The if Statement
Syntax :
if [ condition ]
then
commands
fi
if statement is first check condition, if the condition is true then it will execute
command given in then part. A space should be there among [ and condition and ].
Script3.sh
Syntax :
if [ condition ]
Introductory Linux Workshop 12
SCET, Surat
20th March 2009
then
commands
else
commands
fi
if statement is first check condition, if the condition is true then it will execute
command given in then part otherwise commands under else part will be executed.
Script4.sh
In your script contains multiple else part, instead of using more number of else you can
use elif statement.
Syntax :
if [ condition ]
then
commands
elif [ condition ]
then
commands
elif [ condition ]
then
commands
else
fi
Script5.sh
for Loop
Syntax :
do
commands
done
Here, variable gets value from list of values one by one. For each value of variable
command(s) under do is executed.
Script6.sh
script7.sh
while loop
Syntax :
do
commands
done
In the while loop, the commands between do and done will be executed if the
condition is true.
Script8.sh
until loop
Syntax :
do
commands
done
In the until loop, the commands between do and done will be executed only if the
condition is false.
Script9.sh
Operator Description
-eq equal
String comparison
Operator Description
= equal to
!= not equal to
case statement offers multi-way branching depending upon the user input or a value
assigned to a variable from within the program.
Syntax :
case $ option in
pattern-1)
command(s);;
pattern-2)
command(s);;
esac
The value in option is matched with each pattern till a match is found or the esac
statement is encountered.
Script10.sh
1) open function
Syntax :
#include <sys/types.h>
#include<sys/stat.h>
#include <fcntl.h>
This constants are available in fcntl.h file. One and only one of these three
constants must be specified. The following constants are optional.
program1.c
Syntax :
#include <sys/types.h>
#include<sys/stat.h>
#include <fcntl.h>
One deficiency with this function is that the file is opened only for writing.
program1.c
3) close function
Syntax :
#include <unistd.h>
program2.c
Every open file has an associated current file offset. This is non-negative
integer that measures the number of bytes from the beginning of the file.
Read/Write operations normally start at the current file offset and cause the offset
to be incremented by the number of bytes read and written.
Syntax :
#include <sys/types.h>
#include <unistd.h>
If whence is SEEK_SET, the files offset is set to offset bytes from the
beginning of the file.
If whence is SEEK_CUR, the files offset is set to its current value plus
the offset. The offset is positive or negative.
If whence is SEEK_END, the files offset is set to the size of the file plus
the offset. The offset is positive or negative.
program3.c
5) read function
Syntax:
#include <unistd.h>
The read operation starts at the files current offset. Before a successful return, the
offset is incremented by the number of bytes actually read.
Program2.c
6) write function
Syntax:
#include <unistd.h>
Program2.c
7) stat function
Stat function returns the structure of information about the named file.
Syntax:
#include <sys/types.h>
#include<sys/stat.h>
The first argument is the file name or filename with full path. The second
argument is buffer in which the function returns the status of the file.
struct stat {
};
Regular file, Directory file, Character special file, Block special file, FIFO,
Socket etc.
S_ISSOCK() Socket
Program4.c
8) chmod function
This function allows us to change the file access permission for an existing file.
#include <sys/types.h>
#include<sys/stat.h>
The first argument is the file name or filename with full path. The mode is
specified as the bitwise OR of the constants as follows:
Mode Description
S_IRWXU Read, Write and Execute by User (Owner)
Program5.c
9) remove function
Syntax :
Program6.c
Syntax:
#include <stdio.h>
Points to remember
3) If oldname and newname refer to the same file, the function returns
successfully without changing anything.
Program7.c
Syntax:
#include<sys/stat.h>
The first argument is the file name or filename with full path. The mode is same
as mode concept in chmod function.
Program8.c
Syntax:
#include <unistd.h>
Program9.c
Syntax :
# include <sys/types.h>
#include <dirent.h>
Program
Program10
We can change the current working directory of the calling process by calling the
chdir function.
Syntax:
# include <unistd.h>
Program11
This function gives you the information about current working directory.
Syntax :
#include <unistd.h>
Program12