Manual - Lab02
Manual - Lab02
Sciences
Operating System Lab - 02
Lab Manual
Objective
This lab is all about running commands in Ubuntu Terminal and compiling C program in Ubuntu
Table of Content
Objective ..........................................................................................................................................1
Table of Content...............................................................................................................................1
Shell .................................................................................................................................................2
Commands in Linux .........................................................................................................................2
Patterns and Wildcards....................................................................................................................9
Pipe in Linux................................................................................................................................... 10
Compile C program in Linux .......................................................................................................... 10
Introduction to Shell Scripting ........................................................................................................ 11
Lab Activity ..................................................................................................................................... 12
Operating Systems Lab Manual 02
Shell
Fortunately or unfortunately, a computer can only understand binary language and humans can
easily understand English language or equivalent high level language and therefore it is difficult
to interpret and understand with the computer system. In order to ward off this difficulty every
operating system has got an inbuilt interpreter(Shell). A shell accepts instructions or commands
fed by user in user understandable language and translate it to binary language which a
computer can easily understand. So inshort a Shell is a language translator and in this lab is all
about introducing Shell of the Linux and the commands that are most commonly used.
Figure below will make the above paragraph more meaningful and reader can understand it
better.
Commands in Linux
From here the reader is exposed to the basic Linux commands. All the commands have to be
tried in the terminal. Throughout the lab manuals Ubuntu will be used for explaining the
concepts. To know how to start a Terminal please see Lab Manual 01 – Introduction to
Terminal. The commands with their usage and example is given in the table below:
NOTE: All Linux commands are case sensitive i.e. ‘cp’ is not equivalent to ‘CP’. Also, all the files
and directories in linux are case sensitive so for example ‘/etc/hosts’ is not equivalent to
‘/etc/Hosts’ and so hosts and Hosts are two different files in the same directory.
2|P a g e
Operating Systems Lab Manual 02
3|P a g e
Operating Systems Lab Manual 02
In linux file system, when you type a path starting with a slash (/), then the root of the file tree is
assumed. If you don't start your path with a slash, then the current directory is the assumed
starting point. The screenshot below first shows the current directory /home/paul. From within
this directory, you have to type cd /home instead of cd home to go to the /home directory.
4|P a g e
Operating Systems Lab Manual 02
5|P a g e
Operating Systems Lab Manual 02
overwrites
Preserve permissions and
-p cp -P file* cp None
timestamps
Moves/renames files and mv fileA ~/fileB None
mv none
directories Mv dirA dirB None
none Removes a file rm file1 None
-r Removes a directory rm -r dir1 None
rm
For Removal, removes
-rf rm -rf dir1 None
non-empty directories
Directory Basics
None Determines the current Pwd /home/alishah/Desktop
pwd
path
Creates a directory in mkdir dir1 None
None current or specified sudo mkdir
None
directory /home/dir1
Creates directory or mkdir
mkdir
-p directories in tree hierarchy -p None
manner dir1/subdir/subsubdir
Prints info about the mkdir: created
-v mkdir dir1
directory being created directory 'dir1'
<content of current
Displays the content of ls
directory>
None current directory or
<content of /etc
specified directory ls /etc
directory>
<contents with detail
Displays the content in
-l ls -l like owner, creator
long format and with detail
etc>
Displays the content along
with hidden content of <content of current
ls -a ls -a
current or specified directory>
directory
Displays the content in
-h ls -h <contents>
human readable form
Displays the content in
recursive order (it list file
-R and directories along with ls -R <contents>
files and subdirectories of
subdirectories and so on)
Every file created in file system has an owner and permissions associated with it. There are
basically three kinds of user available in Linux
6|P a g e
Operating Systems Lab Manual 02
Each of the above-mentioned user will have access permissions. Following are the three
permissions associated with all the files.
1. Read (Denoted by r)
2. Write (Denoted by w)
3. Execute (Denoted by x)
Let us examine ‘-rw-rw-r--‘ the first ‘-‘ represent that it’s a file ‘d’ would represent that it’s a
directory, the next 3 characters ‘rw-‘ are the rights for the owner, next three are the permissions
of the group and last three characters are the permissions for the other users/group.
The third column represents states the user who is the owner of the file. Now the question is:
can I change the permission or ownership of a file or directory. The answer is ‘yes!’
Chmod can be issued in two different ways, First method is 4 2 1 code in digital electronics
4 2 1
r w X
1 or 0 1 or 0 1 or 0
This is really simple, if a user has to be assign with all permission (Read, Write and Execute), 1
has to be applied in all the permissions that are required: 1(r) + 1(w) + 1(x) = 1(4) + 1(2) + 1(1) =
7 so 7 is the number that will fetch all the permissions for that file or folder.
7|P a g e
Operating Systems Lab Manual 02
8|P a g e
Operating Systems Lab Manual 02
WildCard ‘*’
• ‘$ ls file*’ - list all the files in current directory starting with filename ‘file’.
• ‘$ ls *2.txt’ - list all the files in current directory ending with ‘2.txt’
WildCard ‘?’
9|P a g e
Operating Systems Lab Manual 02
WildCard ‘[]’
• ‘$ ls rmt[12345]’ - list all the file that begins with ‘rmt’ and has a 1,2,3,4 or 5 after it.
Pipe in Linux
If a user in Linux likes to combine two or more commands, pipes is the option. Example
“ ls -al | grep ‘mp3’ ” many options can be tried easily. Pipe is represented by the symbol ‘|’. Let
us look at the example below:
First the command cat file1.txt is executed and then the output from that command is fed to the
second command as an input. Likewise, many other combinations can be tried.
First ls command will grab the list of files and directories in the current relative directory whose
output will be fed to grep command, that will pick out all the line containing ‘mp3’ pattern which
will be fed to sort command and this will print the output in reverse order as per the -r switch.
$ nano hellow.c
2. Write the following text to the file:
#include<stdio.h>
Int main() {
printf(“hellow world from C program”);
return 0;
}
3. Compile the file and create an executable object file
$ gcc -o hellow.c helloExample
4. Run the newly created object file
$ ./helloExample
10 | P a g e
Operating Systems Lab Manual 02
Advantage of using Shell Script is that It is easy to write, run and debug and disadvantage is
that requirements of high complexity cannot be programmed in Shell.
11 | P a g e
Operating Systems Lab Manual 02
Lab Activity
1) User Account
a. Create a group name ‘OSLAB02’
b. Create a user account ‘OSUser1’ and ‘OSUser2’ and add it to the group which is
created in ‘a’
c. Also add the newly created user to group ‘sudo’
d. Login in to that user using terminal
3) Write 2 C program one prints “I love Operating System” and other prints “I love Linux”.
Compile and Run both programs and print the output to two different files. After then
combine both the files in one new file using a single command.
12 | P a g e