Lab Manual 02 Shell Basic Command PDF
Lab Manual 02 Shell Basic Command PDF
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....................................................................................................................11
Pipe in Linux..................................................................................................................................... 12
Compile C program in Linux .......................................................................................................... 12
Lab Activity ....................................................................................................................................... 13
1|Page
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 in short 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.
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.
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.
Command Switch Description Example Output
BASIC COMMANDS
Manual/Help for any command
Opens manual in
terminal, press ‘h’
Gives manual for the for help or ‘q’ to quit
man None man mkdir
specified command and get back to
terminal
2|Page
Operating Systems Lab Manual 02
3|Page
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 summarizes all.
4|Page
Operating Systems Lab Manual 02
5|Page
Operating Systems Lab Manual 02
Directory Basics
6|Page
Operating Systems Lab Manual 02
Every file created in file system has an owner and permissions associated with it. There are
basically three kinds of user available in Linux
1. Read (Denoted by r)
2. Write (Denoted by w)
3. Execute (Denoted by x)
Let us examine ‘-rwxr-x---‘ the first ‘-‘ represent that it’s a file ‘d’ would represent that it’s a
directory, the next 3 characters ‘rwx‘ 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!’
7|Page
Operating Systems Lab Manual 02
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.
Assuming that all the users get rwx permission so 4+2+1 = 7 will get mathematically 777.
Below table shows the syntax and example of using chmod command and also how to
change the owner of the file i.e. chown command.
8|Page
Operating Systems Lab Manual 02
9|Page
Operating Systems Lab Manual 02
10 | 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 ‘?’
11 | P a g e
Operating Systems Lab Manual 02
Pipe in Linux
If a user in Linux likes to combine two or more commands, pipes is the option. Pipe is
represented by the symbol ‘|’. Let us look at the example below:
$ nano hello.c
2. Write the following text to the file:
#include<stdio.h>
int main() {
printf(“hellow world from Cprogram”);
return 0;
}
3. Compile the file and create an executable object file
$ gcc -o Hello hello.c
4. Run the newly created object file
$ ./Hello
12 | 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
2) Create the following directories with one command. dirOSLAB -> subDir ->
subsubdir -> OSLAB2
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.
13 | P a g e
Operating Systems Lab Manual 02
f. Copy all files in the current directory whose names end in ".c" or ".h" into the
subdirectory "Programs".
g. Copy all files in the current directory whose names contain the character
strings "notes" or "misc" into the subdirectory "Misc".
h. Copy all files which begin with "copy.me" into the "OS" subdirectory. Move all
files which begin with "move.me" into the "OS" subdirectory.
i. Delete all files which contain the sequence "del".
Coursera
The Unix Workbench (cover Week 1 and 2 only for Lab 2)
https://www.coursera.org/learn/unix
https://seankross.com/the-unix-workbench/
14 | P a g e