0% found this document useful (0 votes)
17 views128 pages

Os Lab Manual Comp

This is os lab manual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views128 pages

Os Lab Manual Comp

This is os lab manual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 128

10/12/24, 12:39 PM os lab manual comp

Lab Manual
CSC322-Operating Systems

CUI Department of Computer Science


Islamabad Campus

about:blank 1/128
10/12/24, 12:39 PM os lab manual comp

Lab Contents:
Installating Linux Distrubutions on Virtaul Machnes; Linux Command-line Interface: Basic Syntax, Navigation
Commands, File & Directory Handling Commands, I/O Redirection, Controlling Access to Files, Package
Management, Text-processing, Pipelining, Process Management; Writing & Compiling C++ on Linux; Process
Management: Creating Child Processes, IPC with Shared-Memory & Message-Passing, Multithreading,
Synchronization; Shell Scripting: Fundamentals, I/O, Variables, Operators, Conditional Statements, Looping
Statements, Arrays, and Functions.
Student Outcomes (SO)
S.# Description
Apply knowledge of computing fundamentals, knowledge of a computing specialization, and mathematics,
1 science, and domain knowledge appropriate for the computing specialization to the abstraction and
conceptualization of computing models from defined problems and requirements
Identify, formulate, research literature, and solve complex computing problems reaching substantiated
2 conclusions using fundamental principles of mathematics, computing sciences, and relevant domain
disciplines
Create, select, adapt and apply appropriate techniques, resources, and modern computing tools
4
to complex computing activities, with an understanding of the limitations
Intended Learning Outcomes
Blooms Taxonomy
Sr.# Description SO
Learning Level
CLO -5 Operate basic services and functionality of operating systems. Applying 1
CLO -6 Compose Linux commands using Shell scripting. Applying 1,4
CLO -7 Implement the concepts of process management. Applying 2,4
Lab Assessment Policy
The lab work done by the student is evaluated using rubrics defined by the course instructor, viva-voce, project
work/performance. Marks distribution is as follows:
Assignments Lab Mid Term Lab Terminal Total
Exam Exam
25 25 50 100
Note: Midterm and Final term exams must be computer based.

about:blank 2/128
10/12/24, 12:39 PM os lab manual comp

List of Labs

Lab # Main Topic Page #

Lab 01 Installing Linux Distribution on Virtual Machine, Command-line Interface, 3


and Basic Command Structure

Lab 02 Working with Navigation, and File & Directory Handing Commands 25

Lab 03 Controlling Access to Files, and Managing Packages using Commands 33

Lab 04 Text Processing, and Pipelining 46

Lab 05 Managing Processes, and Writing, Compliling & Executing C++ on Linux 53

Lab 06 Using Fork, Exec, Wait & Exit System-Calls for Creating Chile Processes 68

Lab 07 Inter-Process Communications using Shared-Memory & Message-Passing 78

Lab 08 Creating Multithreaded Applications 83

Lab 09 Mid Term Exam

Lab 10 Synchronization: Two-Process Solutions, MUTEX, and Semaphore 88

Lab 11 Writing & Executing Shell Scripts, I/O, Variables, and Operators 99

Lab 12 Writing Shell Scripts using Conditinal-Statements, and Loops 110

Lab 13 Using Arrays, and Functions in Shell Scripts 117

Lab 14 Final Term Exam

about:blank 3/128
10/12/24, 12:39 PM os lab manual comp

Lab No. 01
Installing Linux Distribution on Virtual Machine,
Command-line Interface
Objective:
This lab will introduce the Linux Operating System to you. You will learn the how to createVM
using Virtual-Box, Installing Ubuntu on VM and the basic syntax of Linux Commands.

Activity Outcomes:
On completion of this lab students will be able to:
• Introduction of Linux OS, Linux Distros and Virtual Machines
• Creating VM in Virtual-Box
• Installing Ubuntu on VM
• Writing basic commands in CLI

Instructor Note:
As pre-lab activity, read Chapter 1 to 6 from the book “The Linux Command Line”, William E.Shotts,
Jr.

1) Useful Concepts
Operating System
An operating system (OS) is a program that interacts as interface between a user and ans a computer
system software. It manages computer hardware, software resources, and provides common services for
computer programs. Primary Goals of an Operating System include: To provide ease of use,
convineance and throughput. The main functions perform by operating system can be categorized as:
Process management, Resource Management, Stroge Management, Memory Management and Security
Management.

Why Linux
Linux is among the most popular operating systems. The main reasons for this popularity are: Free and
open source, Stable and Reliable, Secure, and Flexible.

Linux History
Linux was originally developed for personal computers based on the Intel x86 architecture, but has
since been ported to more platforms than any other operating system. In the early 1990s, Finnish
computer science student Linus Torvalds began hacking on Minix, a small, Unix−like operating system
for personal computers then used in college operating systems courses. He decided to improve the main
software component underlying Minix, called the kernel, by writing his own. (The kernel is the central
component of any Unix−like operating system.) OnSeptember 1991, Torvalds published the first
version of this kernel on the Internet, calling it "Linux" (a play on both Minix and his own name).(7)
When Torvalds published Linux, he used the copyleft software license published by the GNU Project,
the GNU General Public License. Doing so made his software free to use, copy, and modify by
anyone−−provided any copies or variations were kept equally free. Torvalds also invited contributions

about:blank 4/128
10/12/24, 12:39 PM os lab manual comp

by other programmers, and these contributions came; slowly at first but, as the Internet grew,
thousands of hackers and programmers from around the globe contributed to his free software project.
The Linux software was immensely extended and improved so that the Linux−based system of today is
a complete, modern operating system, which can be used by programmers and non−programmers .

Popularity:
Because of the dominance of the Linux-based Android on smartphones, Linux also has
the largest installed base of all general-purpose operating systems. Although Linux is used by only
around 2.3 percent of desktop computers, the Chromebook, which runs the Linux kernel-based Chrome
OS, dominates the US K–12 education market and represents nearly 20 percent of sub
$300 notebook sales in the US. Linux is the leading operating system on servers (over 96.4% of the top
1 million web servers' operating systems are Linux), leads other big iron systems such as mainframe
computers, and is the only OS used on TOP500 supercomputers (since November 2017, having
gradually eliminated all competitors).
Linux also runs on embedded systems, i.e. devices whose operating system is typically built into
the firmware and is highly tailored to the system. This includes routers, automation controls, smart
home technology, televisions (Samsung and LG Smart TVs use Tizen and WebOS, respectively)
, automobiles (for example, Tesla, Audi, Mercedes-Benz, Hyundai, and Toyota all rely on
Linux), digital video recorders, video game consoles, and smartwatches. The Falcon 9's and the Dragon
2's avionics use a customized version of Linux.

Linux Distribution
Linux is open-source, free to use kernel. It is used by programmers, rganizations, profit and non-profit
companies around the world to create Operating systems to suit their individual requirements.To
prevent hacking attempts, many organizations keep their Linux operating systems private. Many others
make their variations of Linux available publicly so the whole world can benefit at large. These
versions/ types /kinds of Linux operating system are called Distributions. A list of most popular Linux
distributions is given below:

Deepin is a Linux desktop-oriented operating system derived from Debian,


supporting laptops, desktops, and all-in-ones. It aims to provide a beautiful, easy-to-
use, safe, and reliable operating system to global users.
It is one of the most popular Desktop Distributions available out there. It launched
in 2006 and is now considered to be the fourth most used Operating system in the
computing world.

This Linux Distro is popular amongst Developers. It is an independently developed


system. It is designed for users who go for a do-it-yourself approach.

Slackware aims for design stability and simplicity

Another popular enterprise based Linux Distribution is Red Hat Enterprise.It has
evolved from Red Hat Linux which was discontinued in 2004. It is a commercial
Distro and very popular among its clientele.

about:blank 5/128
10/12/24, 12:39 PM os lab manual comp

This is the third most popular desktop operating system after Microsoft Windows
and Apple Mac OS. It is based on the Debian Linux Distribution, and it is known as
its desktop environment.
Another Linux kernel based Distro, Fedora is supported by the Fedora project, an
endeavor by Red Hat. It is popular among desktop users. Its versions are known for
their short life cycle.
In this course, we will use the Ubuntu distro. Ubuntu is a popular and to use graphical Linux distro. It
was developed and released by Canonical Ltd. in 2004. It is freely available and can be downloaded
from http://www.ubuntu.com/download/desktop.

Installing Ubuntu

Before discussing the options available to install Ubuntu, we discuss the basic system requirement.
It is recommended to Ubuntu should be installed on a system that has a 2 GHz dual core processor
with 2GB RAM and 25GB of free hard disk space. There are many ways to use Ubuntu. It can be
installed on a system as a stand-alone OS. Similarly, it can be installed as multi-boot system where it is
installed on a system that already has any other OS like windows. Further, it can also be used without
installing from a bootable USB. However, in this course we will run the Ubuntu on virtual machine. To
create virtual machine we will use Oracle VM Virtual-box. In the following, first we give an overview
of Virtual-Box and then discuss the installation process of Ubuntu on VM.

Installing Linux using Virtual Machine


This is a popular method to install a Linux operating system. The virtual installation offers you the
freedom of running on an existing OS already installed on your computer. This means if you have
Windows running, then you can just run Linux with a click of a button.Virtual machine software like
Oracle VM can install Linux on Windows in easy steps. Let us look at them.
The following diagram shows the steps required to install Ubuntu on VM

Download and Install Virtual Box: Download Virtual box using this link Depending on your
processor and OS, select the appropriate package. In our case, we have selectedwindows with AMD.

about:blank 6/128
10/12/24, 12:39 PM os lab manual comp

Once the download is complete, Open setup file and follow the steps below:
Click On next

Select you’re the directory to install VirtualBox and click on next

about:blank 7/128
10/12/24, 12:39 PM os lab manual comp

Select Desktop icon and click on next, now click on yes

Click On install-to-install Linux on Windows.

about:blank 8/128
10/12/24, 12:39 PM os lab manual comp

Now installation of the virtual box will start. Once complete, click on Finish Button to start Virtual Box

The virtual box dashboard looks like this

about:blank 9/128
10/12/24, 12:39 PM os lab manual comp

Download Ubuntu:
Visit this link to download Ubuntu.

You can select 32/64-bit versions as per your choice.


Create a Machine in Virtual Box: Open Virtual box and click on new button

about:blank 10/128
10/12/24, 12:39 PM os lab manual comp

In next window, give the name of your OS which you are installing in virtual box. And select OS
like Linux and version as Ubuntu 32 bit. And click on next

Now Allocate Ram Size To your Virtual OS. I recommended keeping 1024mb (1 GB) ram to run
Ubuntu better. And click on next

10

about:blank 11/128
10/12/24, 12:39 PM os lab manual comp

.
Now To run OS in virtual box we have to create virtual hard disk, click on create a virtual hard drive
now and click on create button. The virtual hard disk is where the OS installation files and
data/applications you create/install in this Ubuntu machine will reside

select VHD (virtual hard disk) option and click on next.

11

about:blank 12/128
10/12/24, 12:39 PM os lab manual comp

Click on dynamic allocated and click on next. This means that the size of the disk will increase
dynamically as per requirement.

Allocate memory to your virtual hard drive .8GB recommended. Click on create button.

12

about:blank 13/128
10/12/24, 12:39 PM os lab manual comp

Now you can see the machine name in left panel

So a Machine (PC) with 8GB Hardisk, 1GB RAM is ready.


How to Install Ubuntu
Select the Machine and Click on Start

13

about:blank 14/128
10/12/24, 12:39 PM os lab manual comp

Select the Folder Option

14

about:blank 15/128
10/12/24, 12:39 PM os lab manual comp

Select the Ubuntu iso file

Click Startup

You have an option to Run Ubuntu WITHOUT installing. In this tutorial will install Ubuntu

15

about:blank 16/128
10/12/24, 12:39 PM os lab manual comp

Click continue

.
Select option to erase the disk and install Ubuntu and click on install now. This option installs Ubuntu
into our virtual hard drive which is we made earlier. It will not harm your PC or Windows installation

16

about:blank 17/128
10/12/24, 12:39 PM os lab manual comp

Select your location for setting up time zone, and click on continue

Select your keyboard layout, by default English (US) is selected but if you want to change then, you can
select in the list. And click on continue

17

about:blank 18/128
10/12/24, 12:39 PM os lab manual comp

Select your username and password for your Ubuntu admin account. This information has been needed
for installing any software package into Ubuntu and also for login to your OS. Fill up your details and
tick on login automatically to ignore login attempt and click on continue

Installation process starts. May take up to 30 minutes. Please wait until installation process completes.

18

about:blank 19/128
10/12/24, 12:39 PM os lab manual comp

After finishing the installation, you will see Ubuntu Desktop.

Writing Linux Commands

Command Line Interface

The Command Line Interface (CLI), is a non-graphical, text-based interface to the computer system,
where the user types in a command and the computer then successfully executes it. The Terminal is the
platform or the IDE that provides the command line interface (CLI) environment to the user. The CLI
terminal accepts the commands that the user types and passes to a shell. The shell then receives and
19

about:blank 20/128
10/12/24, 12:39 PM os lab manual comp

interprets what the user has typed into the instructions that can be executed by the OS (Operating
System). If the output is produced by the specific command, then this text is displayed in the terminal.
If any of the problems with the commands are found, then some error message is displayed.

We can open the terminal by typing Ctrl + Alt + T short-key or by right-clicking the mouse and
selecting the Open New Terminal option. The terminal window looks like given below.

Basic syntax of Linux Commands

A command is an instruction given by a user telling a computer to do something, such a run a single
program or a group of linked programs. Commands are generally issued by typing them in at the c
command line (i.e., the all-text display mode) and then pressing the ENTER key, which passes themto
the shell. A shell is a program that reads commands that are typed on a keyboard and then executes
(i.e., runs) them. Shells are the most basic method for a user to interact with the system.

Options and Arguments: This brings us to a very important point about how most commands work.
Commands are often followed by one or more options that modify their behavior, and further, by one or
more arguments,the items upon which the command acts. So most commands look kind of like this:

$command -optioins arguments


Most commands use options consisting of a single character preceded by a dash, for example, “-l”, but
many commands, including those from the GNU Project, also support long options, consisting of a
word preceded by two dashes. Also, many commands allow multiple short options to be strung
together.
Command History:Most Linux distributions remember the last 500 commands by default. Press the
down-arrow key and the previous command disappears.

Some Basic Linux Commands


1) Date Command: This command is used to display the current data and time.
$date or $date +%ch

20

about:blank 21/128
10/12/24, 12:39 PM os lab manual comp

Common Options:
a = Abbreviated weekday. A = Full weekday.
b = Abbreviated month.
B = Full month.
c = Current day and time.
C = Display the century as a decimal number. d = Day of the month.
D = Day in „mm/dd/yy‟ format
h = Abbrevated month day.

2) cal Command: This command is used to display the calendar of the year or the
particularmonth of calendar year.
$cal <year> or $cal <month> <year>

3) who Command: It is used to display who are the users connected to our computer currently.
$who –option‟s

Common Options:
H–Display the output with headers
b–Display the last booting date or time or when the system was lastly rebooted

4) whoami Command: Display the details of the current working directory.


$whoami

5) clear Command: It is used to clear the screen.


$clear

6) man Command: It help us to know about the particular command and its options & working. It is
like„help‟ command in windows .
$man <command name>

7) df Command: is used to see the current amount of free space on your disk drives
$df

8) free Command: Likewise, to display the amount of free memory, enter the free command.
$free

9) exit Command: We can end a terminal session by either closing the terminal emulator window, or
by enteringthe exit command at the shell prompt
21

about:blank 22/128
10/12/24, 12:39 PM os lab manual comp

$exit

2) Solved Lab Activities


Sr.No Allocated Time Level of Complexity CLO Mapping
1 10 Medium CLO-5
2 10 Medium CLO-5

Activity 1:
In this activity, you are required to perform tasks given below:
• Display the current date
• Display the calendar for the current month
• Display the calendar of 2012
• Display the calendar of Feb 2015
Solution:

22

about:blank 23/128
10/12/24, 12:39 PM os lab manual comp

Activity 2:
In this activity, you are required to perform tasks given below:

• Display the amount of free storage on your machine


• Display the amount of free memory on your machine
• Display the user name of the current user
• Open the man of date free command
Solution:

23

about:blank 24/128
10/12/24, 12:39 PM os lab manual comp

3) Graded Lab Tasks


Note: The instructor can design graded lab activities according to the level of difficult and complexity
of the solved lab activities. The lab tasks assigned by the instructor should be evaluated in the same
lab.

Lab Task 1:
In GUI open the Libre Office writer tool create a document that contains information about your
favorite place. Try the following short- keys while formatting the document.

24

about:blank 25/128
10/12/24, 12:39 PM os lab manual comp

Lab No. 02
Working with Navigation, and File & Directory Handing
Commands

Objective:
This lab will introduce the Directory and File related commands to you. We will start with the some
basic but important commands used to navigate through the Linux file system. Then we will discuss
Directory and File related commands. Finally, we will introduce the I/O redirection in Linux

Activity Outcomes:
On completion of this lab students will be able to:
• Navigation through Linux file system using CLI
• Working with directories in Linux using CLI
• Handling Files in Linux using CLI
• Using I/O redirection in Linux.

Instructor Notes
As pre-lab activity, read Chapter 1 to 6 from the book “The Linux Command Line”, William E. Shotts,
Jr.

1) Useful Concepts
Linux organizes its files in a hierarchical directory structure. The first directory in the file system is
called the root directory. The root directory contains files and subdirectories, which contain more files
and subdirectories and so on and so on. If we map out the files and directories in Linux, it would look
like an upside-down tree. At the top is the root directory, which is represented by a single slash (/).
Below that is a set of common directories in the Linux system, such as bin, dev, home, lib , and tmp , to
name a few. Each of those directories, as well as directories added to the root, can contain
subdirectories.

Navigation
The first thing we need to learn is how to navigate the file system on our Linux system. In this section
we will introduce the commands used for navigation in Linux system.

Print Working Directory


The directory we are standing in is called the current working directory. To display the current working
directory, we use the pwd (print working directory) command. When we first log in to our system our
current working directory is set to our home directory. Suppose, a user is created with name me on
machine Ubuntu; we display its current working directory as given below:

25

about:blank 26/128
10/12/24, 12:39 PM os lab manual comp

Listing The Contents Of A Directory


To list the files and directories in the current working directory, we use the ls command. Suppose, a
user me is in its home directory; to display the contents of current working directory can be displayed as
follows:

Besides the current working directory, we can specify the directory to list, like so:

Or even specify multiple directories. In this example we will list both the user's home directory
(symbolized by the “~” character) and the /usr directory:

The following options can also be used with ls command

Options Long-options Description


-a - - all List all files, even those with names
-d - - directory Ordinarily, if a directory is specified, ls will list the contents
of the directory, not the directory itself. Use this option in
conjunction with the -l option to see details about the
directory rather than its contents.
-h - - human- In long format listings, display file sizes in human readable
readable format rather than in bytes.
-r - - reeverse Display the results in reverse order. Normally,ls displays its
results in ascending alphabetical order.
-S - Sort results by file size.
-t Sort by modification time
-l Display results in long format.

Changing the Current Working Directory


To change your working directory, we use the cd command. To do this, type cd followed by the
pathname of the desired working directory. A pathname is the route we take along the branches of the
tree to get to the directory we want. Pathnames can be specified in one of two different ways; as
absolute pathnames or as relative pathnames. An absolute pathname begins with the root directory
26

about:blank 27/128
10/12/24, 12:39 PM os lab manual comp

and follows the tree branch by branch until the path to the desired directory or file is completed. On the
other hand a relative pathname starts from the working directory.
Suppose, a user me is in its home directory and we want to go into the Desktop directory, then it can be
done as follows:

The “..” operator is used to go to the parent directory of the current working directory. In continuation
of the above example, suppose we are in the Desktop directory and we have to go to the Documents
directory. To this task, first we will go the parent directory of Desktop (i.e. me, home directory of the
user) that contains the Documents directory then we will go into the Documents directory as given

below.

Working With Directories


In this Section, we introduce the most commonly used commands related to Directories.

Creating a Directory
In Linux, mkdir command is used to create a directory. We pass the directory name as the argument to
the mkdir command. Suppose, the user me is in its home directory and we want to create a new
directory named mydir in the Desktop directory. To do this, first we will change the current directory
to Desktop and then we will create the new directory. It is shown below:

Multiple directories can also be created using single mkdir command as given below:

Copying Files and Directories


cp command is used to copy files and directories. The syntax to use cp command is givenbelow:

Here, item1 and item2 may be files or directories. Similarly, multiple files can also be copiedusing

single cp command.
The common options that can be used with cp commands are:

Optio Long Option Explanation


n

27

about:blank 28/128
10/12/24, 12:39 PM os lab manual comp

-a --archive Copy the files and directories and all of their attributes
-i --interactive Before overwriting an existing file, prompt the user for
confirmation
-r --recursive Recursively copy directories and their contents
-u -update When copying files from one directory to another, only copy files
that either don't exist, or are newer

Moving and Renaming Files and Directories


mv command is used to move files and directories. This command can also be used to rename files and
folder. To rename files and directories, we just perform the move operation with old name and new
name. As a result, the files or directory is created again with a new name. The syntax to use mv
command is given below:

Similarly, multiple files can be moved to a directory as given below

Common options, used with mv command are:

Option Long Option Explanation


-i --interactive Before overwriting an existing file, prompt the user for
confirmation
-u -update When moving files from one directory to another, only copy files
that either don't exist, or are newer

Removing and Files and Directories


To remove or delete a files and directories, rm command is used. Empty directories can also be deleted
using rmdir command but rm can be used for both empty and non-emptydirectories as well as for files.
The syntax is given below:

The common options, used with rm command are:

Option Long Option Explanation


-i --interactive Before deleting an existing file, prompt the user for confirmation.
-r --recursive Recursively delete directories.

Working with Files


28

about:blank 29/128
10/12/24, 12:39 PM os lab manual comp

In this section, we will introduce the file related commands.

Creating and Empty Text File


In Linux, there are several ways to create an empty text file. Most commonly the touch command is
used to create a file. We can create a file with name myfile.txt using touch command as given below:

Another, way to create a file in Linux is the cat command.

Similarly, a file can be created using some editors. For example, to create a file using gedit editor

Reading the File Contents


cat command can also be used to read the contents of a file.

Another option to view the contents of a text file is the use of less command.
Similarly, an editor can also be used to view the contents of a file.

Appending text files


cat command is also used to append a text file. Suppose we want to add some text at theend of

myfile.txt

Now, type the text and enter ctrl+d to copy the text to myfile.txt.

Combining multiple text files


Using cat command, we can view the contents of multiple files. Suppose, we want to viewthe

contents of file1, file2 and file3, we can use the cat command as follows:

Similarly, we can redirect the output of multiple files to file instead of screen using cat command.
Suppose, in the above example we want to write the contents of file1, file2 and file3 into another file
file4 we can do this as shown below:
29

about:blank 30/128
10/12/24, 12:39 PM os lab manual comp

Determining File Type


To determine the type of a file we can use the file command. The syntax is given below:

Redirecting I/O
Many of the programs that we have used so far produce output of some kind. This output often consists
of two types. First, we have the program's results; that is, the data the program is designed to produce,
and second, we have status and error messages that tell us how the program is getting along. If we look
at a command like ls, we can see that it displays its results and its error messages on the screen.
Keeping with the Unix theme of “everything is a file,” programs such as ls actually send their results to
a special file called standard output (often expressed as stdout) and their status messages to another file
called standard error (stderr). By default, both standard output andstandard error are linked to the screen
and not saved into a disk file. In addition, many programs take input from a facility called standard
input (stdin) which is, by default, attached to the keyboard. I/O redirection allows us to change where
output goes and whereinput comes from. Normally, output goes to the screen and input comes from the
keyboard, but with I/O redirection, we can change that.
Redirecting Standard Output
I/O redirection allows us to write the output on another file instead of standard output i.e. screen. To do
this, we use the redirection operator i.e. <. For example, we want to write the output of ls command in a
text file myfile.txt instead of screen. This can be done as given below:
If we write the output of some other program to myfile.txt using > operator, its previous contents will

be overwritten. Now, if want to append the file instead of over-writing we can use the << operator.
Redirecting Standard input
Redirecting input enables us to take input from another file instead of standard input i.e. keyboard. We
have already discussed this in previous section while discussing cat command where we used the text
file as input instead of keyboard and wrote it to another file.
Pipelines
The ability of commands to read data from standard input and send to standard output is utilized by a
shell feature called pipelines. Using the pipe operator “|” (vertical bar), the standard output of one
command can be piped into the standard input of another

2) Solved Lab Activities


Sr.No Allocated Time Level of Complexity CLO Mapping
1 15 Medium CLO-5
2 15 Medium CLO-5
3 15 Medium CLO-5

30

about:blank 31/128
10/12/24, 12:39 PM os lab manual comp

Activity 1:
In this activity, you are required to perform tasks given below:
• Display your current directory.
• Change to the /etc directory.
• Go to the parent directory of the current directory.
• Go to the root directory.
• List the contents of the root directory.
• List a long listing of the root directory.
• Stay where you are, and list the contents of /etc.
• Stay where you are, and list the contents of /bin and /sbin.
• Stay where you are, and list the contents of ~.
• List all the files (including hidden files) in your home directory.
• List the files in /boot in a human readable format.

Solution:
• pwd
• cd /etc
• cd ..
• cd /
• ls
• ls -l
• ls /etc
• ls /bin /sbin
• ls ~
• ls -al ~
• ls -lh /boot

Activity 2:
Perform the following tasks using Linux CLI
• Create a directory “mydir1” in Desktop Directory. Inside mydir1 create another directory
“mydir2”.
• Change your current directory to “mydir2” using absolute path
• Now, change you current directory to Documents using relative path
• Create mydir3 directory in Documents directory and go into it
• Now, change your current directory to mydir2 using relative path
Solution:
• cd /home/Ubuntu/Desktop (suppose the user name is ubuntu)
mkdir mydir1
cd mydir1
mkdir mydir2
• cd /home/ubuntu/Desktop/mydir1/mydir2
• cd ../../../Desktop
• mkdir mydir3
cd mydir3
• cd ../../Desktop/mydir1/mydir2

31

about:blank 32/128
10/12/24, 12:39 PM os lab manual comp

Activity 3:
Considering the directories created in Activity 2, perform the following tasks
• Go to mydir3 and create an empty file myfile using cat command
• Add text the text “Hello World” to myfile
• Append myfile with text “Hello World again”
• View the contents of myfile

Solution:
• cd /home/Documents/mydir3 (suppose the user name is ubuntu)
cat >myfile
• cat > myfile
type: Hello World
type: ctrl + d
• cat >> myfile
type: Hello World again
type: ctrl + d
• cat myfile

3) Graded Lab Tasks


Note: The instructor can design graded lab activities according to the level of difficult and
complexity of the solved lab activities. The lab tasks assigned by the instructor should be
evaluated in the same lab.

Task 1:
Considering the above activities (given solved actvites section), perform the following tasks
• move myfile to mydir1
• copy myfile to mydir2
• copy mydir2 on Desktop
• delete mydir1 (get confirmation before deleting)
• Rename myfile to mynewfile

Task 2:
This activity is related to I/O redirection
• Go to Desktop directory
• Write the long-listing of contents of Desktop on an empty file out-put-file
• View contents of out-put-file

Task 3:
Considering the lab activities, perform the following tasks
• Go to Desktop directory
• write the contents of mynewfile to newfile
• view the output of both mynewfile and newfile on screen
• write the combined output of mynewfile and newfile to a third file out-put-file

Task 4:
Long list all files and directories in your system and write out-put on a text-file.

32

about:blank 33/128
10/12/24, 12:39 PM os lab manual comp

33

about:blank 34/128
10/12/24, 12:39 PM os lab manual comp

Lab No. 03
Controlling Access to Files, and Managing Packages using
Commands
Objective:
This lab will introduce the basic concept of file access permissions and package management in Linux
to you.

Activity Outcomes:
On completion of this lab students will be able to:
• Reading and setting file permissions.
• Setting the default file permissions.
• Performing package management tasks.

Instructor Notes
As pre-lab activity, read Chapter 09 and 14 from the book “The Linux Command Line”, William E.
Shotts, Jr.

1) Useful Concepts
File Permissions
Linux is a multi-user system. It means that more than one person can be using the computer at the same
time. While a typical computer will likely have only one keyboard and monitor, it can still be used by
more than one user. For example, if a computer is attached to a network or the Internet, remote users
can log in via ssh (secure shell) and operate the computer. In fact, remote users can execute graphical
applications and have the graphical output appear on a remote display. In a multi-user environment, to
ensure the operational accuracy, it is required to protect the users from each other. After all, the actions
of one user could not be allowed to crash the computer, nor could one user interfere with the files
belonging to another user.

id command
In the Linux security model, a user may own files and directories. When a user owns a file or directory,
the user has control over its access. Users can, in turn, belong to a group consisting of one or more users
who are given access to files and directories by their owners. In addition to granting access to a group,
an owner may also grant some set of access rights to everybody, which in Linux terms is referred to as
the world.

User accounts are defined in the /etc/passwd file and groups are defined in the /etc/group file. When

user accounts and groups are created, these files are modified along with /etc/shadow which holds
information about the user's password.
34

about:blank 35/128
10/12/24, 12:39 PM os lab manual comp

Option Explanation
-g Print only the effective group id
-G Print all Group ID’s
-n Prints name instead of number.
-r Prints real ID instead of numbers.
-u Prints only the effective user ID.

Reading, Writing, and Executing


Access rights to files and directories are defined in terms of read access, write access, and execution
access. If we look at the output of the ls command, we can get some clue as to how this isimplemented:

The first ten characters of the listing are the file attributes. The first of these characters is the file type.
Here are the file types you are most likely to see:
Attribute File Type
- A regular file.
d A directory.
l A symbolic link.
c A character special file. This file type refers to a device that handles data as a
stream of bytes, such as a terminal or modem.
b A block special file. This file type refers to a device that handles data in blocks,
such as a hard drive or CD-ROM drive.

The remaining nine characters of the file attributes, called the file mode, represent the read, write, and
execute permissions for the file's owner, the file's group owner, and everybody else.

User Group World


rwx rwx rwx

Attribute Files Directories


r Allows a file to be opened and Allows a directory's contents to be listed if
read. theexecute attribute is also set.

35

about:blank 36/128
10/12/24, 12:39 PM os lab manual comp

w Allows a file to be written Allows files within a directory to be


created, deleted, and renamed if the execute
attribute is also set.
x Allows a file to be treated as a Allows a directory to be entered,
program and executed. e.g., cddirectory.

For example: -rw-r--r- A regular file that is readable and writable by the file's owner. Members ofthe
file's owner group may read the file. The file is world-readable.

Reading File Permissions


The ls command is used to read the permission of a file. In the following example, we have used ls
command with -l option to see the information about /etc/passwd file. Similarly, we can read the current
permissions of any file.

Change File Mode (Permissions)


To change the mode (permissions) of a file or directory, the chmod command is used. Beware that only
the file’s owner or the super-user can change the mode of a file or directory. chmod supports two
distinct ways of specifying mode changes: octal number representation, or symbolic representation.
With octal notation we use octal numbers to set the pattern of desired permissions. Since each digit in
an octal number represents three binary digits, these maps nicely to the scheme used to store the file
mode.

Octal Binary File Mode


0 000 ---
1 001 --x
2 010 -w-
3 011 -wx
4 100 r--
5 101 r-x
6 110 rw-
7 111 rwx

In the following example, we first go to the Desktop directory using cd command. In Desktopdirectory,
we create a text file “myfile.txt” using touch command and read its current permissions using ls
command.
36

about:blank 37/128
10/12/24, 12:39 PM os lab manual comp

about:blank 38/128
10/12/24, 12:39 PM os lab manual comp

about:blank 39/128
10/12/24, 12:39 PM os lab manual comp

about:blank 40/128
10/12/24, 12:39 PM os lab manual comp

about:blank 41/128
10/12/24, 12:39 PM os lab manual comp

about:blank 42/128
10/12/24, 12:39 PM os lab manual comp

about:blank 43/128
10/12/24, 12:39 PM os lab manual comp

about:blank 44/128
10/12/24, 12:39 PM os lab manual comp

about:blank 45/128
10/12/24, 12:39 PM os lab manual comp

about:blank 46/128
10/12/24, 12:39 PM os lab manual comp

about:blank 47/128
10/12/24, 12:39 PM os lab manual comp

about:blank 48/128
10/12/24, 12:39 PM os lab manual comp

about:blank 49/128
10/12/24, 12:39 PM os lab manual comp

about:blank 50/128
10/12/24, 12:39 PM os lab manual comp

about:blank 51/128
10/12/24, 12:39 PM os lab manual comp

about:blank 52/128
10/12/24, 12:39 PM os lab manual comp

about:blank 53/128
10/12/24, 12:39 PM os lab manual comp

about:blank 54/128
10/12/24, 12:39 PM os lab manual comp

about:blank 55/128
10/12/24, 12:39 PM os lab manual comp

about:blank 56/128
10/12/24, 12:39 PM os lab manual comp

about:blank 57/128
10/12/24, 12:39 PM os lab manual comp

about:blank 58/128
10/12/24, 12:39 PM os lab manual comp

about:blank 59/128
10/12/24, 12:39 PM os lab manual comp

about:blank 60/128
10/12/24, 12:39 PM os lab manual comp

about:blank 61/128
10/12/24, 12:39 PM os lab manual comp

about:blank 62/128
10/12/24, 12:39 PM os lab manual comp

about:blank 63/128
10/12/24, 12:39 PM os lab manual comp

about:blank 64/128
10/12/24, 12:39 PM os lab manual comp

about:blank 65/128
10/12/24, 12:39 PM os lab manual comp

about:blank 66/128
10/12/24, 12:39 PM os lab manual comp

about:blank 67/128
10/12/24, 12:39 PM os lab manual comp

about:blank 68/128
10/12/24, 12:39 PM os lab manual comp

about:blank 69/128
10/12/24, 12:39 PM os lab manual comp

about:blank 70/128
10/12/24, 12:39 PM os lab manual comp

about:blank 71/128
10/12/24, 12:39 PM os lab manual comp

about:blank 72/128
10/12/24, 12:39 PM os lab manual comp

about:blank 73/128
10/12/24, 12:39 PM os lab manual comp

about:blank 74/128
10/12/24, 12:39 PM os lab manual comp

about:blank 75/128
10/12/24, 12:39 PM os lab manual comp

about:blank 76/128
10/12/24, 12:39 PM os lab manual comp

about:blank 77/128
10/12/24, 12:39 PM os lab manual comp

about:blank 78/128
10/12/24, 12:39 PM os lab manual comp

about:blank 79/128
10/12/24, 12:39 PM os lab manual comp

about:blank 80/128
10/12/24, 12:39 PM os lab manual comp

about:blank 81/128
10/12/24, 12:39 PM os lab manual comp

about:blank 82/128
10/12/24, 12:39 PM os lab manual comp

about:blank 83/128
10/12/24, 12:39 PM os lab manual comp

about:blank 84/128
10/12/24, 12:39 PM os lab manual comp

about:blank 85/128
10/12/24, 12:39 PM os lab manual comp

about:blank 86/128
10/12/24, 12:39 PM os lab manual comp

about:blank 87/128
10/12/24, 12:39 PM os lab manual comp

about:blank 88/128
10/12/24, 12:39 PM os lab manual comp

about:blank 89/128
10/12/24, 12:39 PM os lab manual comp

about:blank 90/128
10/12/24, 12:39 PM os lab manual comp

about:blank 91/128
10/12/24, 12:39 PM os lab manual comp

about:blank 92/128
10/12/24, 12:39 PM os lab manual comp

about:blank 93/128
10/12/24, 12:39 PM os lab manual comp

about:blank 94/128
10/12/24, 12:39 PM os lab manual comp

about:blank 95/128
10/12/24, 12:39 PM os lab manual comp

about:blank 96/128
10/12/24, 12:39 PM os lab manual comp

about:blank 97/128
10/12/24, 12:39 PM os lab manual comp

about:blank 98/128
10/12/24, 12:39 PM os lab manual comp

about:blank 99/128
10/12/24, 12:39 PM os lab manual comp

about:blank 100/128
10/12/24, 12:39 PM os lab manual comp

about:blank 101/128
10/12/24, 12:39 PM os lab manual comp

about:blank 102/128
10/12/24, 12:39 PM os lab manual comp

about:blank 103/128
10/12/24, 12:39 PM os lab manual comp

about:blank 104/128
10/12/24, 12:39 PM os lab manual comp

about:blank 105/128
10/12/24, 12:39 PM os lab manual comp

about:blank 106/128
10/12/24, 12:39 PM os lab manual comp

about:blank 107/128
10/12/24, 12:39 PM os lab manual comp

about:blank 108/128
10/12/24, 12:39 PM os lab manual comp

about:blank 109/128
10/12/24, 12:39 PM os lab manual comp

about:blank 110/128
10/12/24, 12:39 PM os lab manual comp

about:blank 111/128
10/12/24, 12:39 PM os lab manual comp

about:blank 112/128
10/12/24, 12:39 PM os lab manual comp

about:blank 113/128
10/12/24, 12:39 PM os lab manual comp

about:blank 114/128
10/12/24, 12:39 PM os lab manual comp

about:blank 115/128
10/12/24, 12:39 PM os lab manual comp

about:blank 116/128
10/12/24, 12:39 PM os lab manual comp

about:blank 117/128
10/12/24, 12:39 PM os lab manual comp

about:blank 118/128
10/12/24, 12:39 PM os lab manual comp

about:blank 119/128
10/12/24, 12:39 PM os lab manual comp

about:blank 120/128
10/12/24, 12:39 PM os lab manual comp

about:blank 121/128
10/12/24, 12:39 PM os lab manual comp

about:blank 122/128
10/12/24, 12:39 PM os lab manual comp

about:blank 123/128
10/12/24, 12:39 PM os lab manual comp

about:blank 124/128
10/12/24, 12:39 PM os lab manual comp

about:blank 125/128
10/12/24, 12:39 PM os lab manual comp

about:blank 126/128
10/12/24, 12:39 PM os lab manual comp

about:blank 127/128
10/12/24, 12:39 PM os lab manual comp

about:blank 128/128

You might also like