Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Bahria University,

Karachi Campus

LAB EXPERIMENT NO. 02

LIST OF TASKS
TASK NO OBJECTIVE

1 Practice all the Linux commands discussed in this lab while taking assistance using the
man command. Write the complete syntax used for utilizing the cp, mv and rm
commands in Linux shell.
2 Write the C programs provided in this lab and generate their outputs over Linux
environment (provide snapshot).
3 Write a C program on the Linux environment that takes your marks as an input and
display your grades accordingly to that followed at Bahria University. Limit your
program to a maximum of five subjects. Use the suitable logical operator(s), i.e. and
(&&), or (||), not (!), if required.
4 Write a shell script to display your address over multiple lines.
5 Write a shell script that would traverse among any three directories that are placed
under the /home directory. While moving from one directory to another, the script
should display the name of the current working directory and list the content within
that directory, including the hidden files.

Submitted On:
18/03/2021
(Date: DD/MM/YY)
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting
Task 1: Practice all the Linux commands discussed in this lab while taking
assistance using the man command. Write the complete syntax used for
utilizing the cp, mv and rm commands in Linux shell.
1. cp Command:
cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image
of a file on a disk with different file name. cp command requires at least two filenames in its arguments.

Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory

cp -i (i stands for Interactive copying. With this option system first warns the user before overwriting the
destination file. cp prompts for a response, if you press y then it overwrites the file and with any other option
leave it uncopied.)

cp -b With this option cp command creates the backup of the destination file in the same folder with the
different name and in different format.

cp -f If the system is unable to open destination file for writing operation because the user doesn’t have
writing permission for this file then by using -f option with cp command, destination file is deleted first and
then copying of content is done from source to destination file.

cp -r Copying directory structure. With this option cp command shows its recursive behavior by copying the
entire directory structure recursively.
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting
2. mv Command.
mv stands for move. mv is used to move one or more files or directories from one place to another in file
system like Linux. It has two distinct functions:
(i) It rename a file or folder.
(ii) It moves group of files to different directory.

Syntax:
mv [Option] source destination

mv -i (Interactive): Like in cp, the -i option makes the command ask the user for confirmation before moving a
file that would overwrite an existing file, you have to press y for confirm moving, any other key leaves the file
as it is. This option doesn’t work if the file doesn’t exist, it simply rename it or move it to new location.

mv -f (Force): mv prompts for confirmation overwriting the destination file if a file is write protected. The -f
option overrides this minor protection and overwrite the destination file forcefully and delete the source file.

mv -n (no-clobber): With -n option, mv prevent an existing file from being overwritten.

Mv -b(backup): With this option it is easier to take a backup of an existing file that will be overwritten as a
result of the mv command. This will create a backup file with the tilde character(~) appended to it.
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting
3. Rm command.
rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links
and so on from the file system like Linux.

Syntax:
rm [OPTION]... FILE...
rm -i (Interactive Deletion): Like in cp, the -i option makes the command ask the user for confirmation before
removing each file, you have to press y for confirm deletion, any other key leaves the file un-deleted.
Rm -f (Force Deletion): rm prompts for confirmation removal if a file is write protected. The -f option overrides
this minor protection and removes the file forcefully.

Rm -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will delete all the files
and sub-directories recursively of the parent directory. At each stage it deletes everything it finds. Normally,
rm wouldn’t delete the directories but when used with this option, it will delete.

4. Mkdir Command.
mkdir command in Linux allows the user to create directories (also referred to asfolders in some operating
systems). This command can create multiple directories at once as well as set the permissions for the
directories.
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting
5. Rmdir Command
rmdir command is used remove empty directories from the filesystem in Linux. The rmdir command removes
each and every directory specified in the command line only if these directories are empty.

6. Who Command.
who command is used to find out the following information :
1. Time of last system boot
2. Current run level of the system
3. List of logged in users and more.

7. Uname Command:
The command ‘uname‘ displays the information about the system.
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting
Task 2: Write the C programs provided in this lab and generate their outputs
over Linux environment (provide snapshot).
Code #1:

Output:
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting
Code #2:

Output:
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting
Task 03. Write a C program on the Linux environment that takes your marks
as an input and display your grades accordingly to that followed at Bahria
University. Limit your program to a maximum of five subjects. Use the
suitable logical operator(s), i.e., and (&&), or (||), not (!), if required.
Code:
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting

Task 04. Write a shell script to display your address over multiple lines.
Code:

Output:
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting

Task 05. Write a shell script that would traverse among any three directories
that are placed under the /home directory. While moving from one directory to
another, the script should display the name of the current working directory
and list the content within that directory, including the hidden files.
Shell Script:

Output:
Lab No. 02 OPERATING SYSTEM
Linux Commands and Shell Scripting

You might also like