Unit 1 OS
Unit 1 OS
OPERATING SYSTEMS
UNIT – 1
A computer system is divided into two categories: Hardware and Software.
Hardware refers to the physical and visible components of the system such as a monitor, CPU,
keyboard and mouse.
Software, on the other hand, refers to a set of instructions which enable the hardware to perform
a specific set of tasks.
1
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
3
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
6. Error Detection and Handling: Detects faults in hardware and software and prevents
failures.
7. Efficiency: Optimizes system resource utilization and minimizes response time.
User View:
The user view depends on the system interface that is used by the users. The different types of
user view experiences can be explained as follows −
If the user is using a personal computer, the operating system is largely designed to make
the interaction easy. Some attention is also paid to the performance of the system, but there is no
need for the operating system to worry about resource utilization. This is because the personal
computer uses all the resources available and there is no sharing.
If the user is using a system connected to a mainframe or a minicomputer, the operating
system is largely concerned with resource utilization. This is because there may be multiple
terminals connected to the mainframe and the operating system makes sure that all the resources
such as CPU, memory, I/O devices etc. are divided uniformly between them.
If the user is sitting on a workstation connected to other workstations through networks,
then the operating system needs to focus on both individual usage of resources and sharing
though the network. This happens because the workstation exclusively uses its own resources but
it also needs to share files etc. with other workstations across the network.
If the user is using a handheld computer such as a mobile, then the operating system
handles the usability of the device including a few remote operations. The battery level of the
device is also taken into account.
There are some devices that contain very less or no user view because there is no interaction
with the users. Examples are embedded computers in home devices, automobiles etc.
System View:
4
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
From the System point of view the operating system is the program which is most intermediate
with the hardware.
The system views the operating system as a resource allocator. There are many resources such
as CPU time, memory space, file storage space, I/O devices etc. that are required by processes
for execution. It is the duty of the operating system to allocate these resources judiciously to the
processes so that the computer system can run as smoothly as possible.
The operating system can also work as a control program. It manages all the processes and
I/O devices so that the computer system works smoothly and there are no errors. It makes sure
that the I/O devices work in a proper manner without creating problems.
Operating system services are fundamental functionalities provided by an operating system (OS)
to support the execution of application software.
An Operating System provides services to both the users and to the programs. It provides
programs, an environment to execute. It provides users, services to execute the programs in a
convenient manner.
Following are the services provided by operating systems to both users and to the programs:
Users
• User Interface
• Program Execution
• I/O Operations
• File-System Manipulation
• Communication
• Error Detection
System/Programs
• Resource Allocation
• Accounting
• Protection and Security
5
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
1. User Interface
An interface allows users to interact with the system more easily and intuitively. Many operating
systems provide user interfaces, such as command-line interfaces (CLI), graphical user interfaces
(GUI) and Batch Based Interface.
A Command-Line Interface (CLI) is a text-based user interface used to interact with software and
operating systems. Through a CLI, users can input text commands to perform specific tasks
A GUI usually consists of all the graphical icons displayed on a computer screen, visual indicators
like widgets, texts, labels, and text navigation. Thus, a user can directly perform actions with a
click of the mouse or keyboard.
A Batch-based interface in computing refers to a method of interaction between the user and the
computer system or software where commands or a series of commands are prepared in advance
and then executed all at once, rather than being entered and executed one at a time.
2. Program Execution
The OS loads a program into memory and then executes that program. It also makes sure that once
started that program can end its execution, either normally or forcefully. The major steps during
program management are:
3. I/O Operations
I/O operations are required during the execution of a program. To maintain efficiency and
protection of the program, users cannot directly govern the I/O devices instead the OS allows to
6
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
read or write operations with any file using the I/O devices and also allows access to any required
I/O device when required.
A program is read and then written in the form of directories and files. These files can be stored
on the storage disk for the long term.
Following are the major activities of an operating system with respect to file management.
Program needs to read a file or write a file.
The operating system gives the permission to the program for operation on file.
Permission varies from read-only, read-write, denied and so on.
Operating System provides an interface to the user to create/delete files.
Operating System provides an interface to the user to create/delete directories.
Operating System provides an interface to create the backup of file system.
5. Communication systems
Processes need to swap information among themselves. These processes can be from the same
computer system or different computer systems as long as they are connected through
communication lines in a network. This can be done with the help of OS support using shared
memory or message passing. The OS also manages routing, connection strategies, and the problem
of contention and security.
6. Error Detection
Errors may occur in any of the resources like CPU, I/O devices, or memory hardware. The OS
keeps a lookout for such errors, corrects errors when they occur, and makes sure that the system
works uninterruptedly.
Resource management in operating systems (OS) is a critical function that involves managing the
hardware and software resources of a computer system. These resources include the CPU (central
processing unit), memory (both RAM and storage), and networking components.
7
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Memory Management:
Memory management involves allocating and managing the system's primary memory or RAM.
The OS must track which parts of memory are in use and by whom, allocate memory to processes
when they need it, and deallocate it when they are done. Techniques such as paging and
segmentation are used to manage memory efficiently and protect the memory space of processes
from each other.
Network Resource Management:
For systems connected to a network, the OS manages the network configuration and
communication protocols to enable data exchange between computers. This includes managing
network interfaces, routing, bandwidth allocation, and implementing security measures for
network connections.
8. Accounting
This keeps a check of which resource is being used by a user and for how long it is being used.
This is usually done for statistical purposes.
8
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
In order to ensure the proper execution of the operating system, we must be able to distinguish
between the execution of operating-system code and user defined code. The approach taken by
most computer systems is to provide hardware support that allows us to differentiate among
various modes of execution. At the very least, we need two separate modes of operation: user
mode and kernel mode (also called supervisor mode, system mode, or privileged mode)
User mode
When the computer system run user applications like creating a text document or using any
application program, then the system is in the user mode. When the user application requests for a
service from the operating system or an interrupt occurs or system call, then there will be a
transition from user to kernel mode to fulfil the requests.
The mode bit is set to 1 in the user mode. It is changed from 1 to 0 when switching from user mode
to kernel mode.
Kernel Mode
When the system boots, hardware starts in kernel mode and when operating system is loaded, it
start user application in user mode. To provide protection to the hardware, we have privileged
instructions which execute only in kernel mode. If user attempt to run privileged instruction in user
mode then it will treat instruction as illegal and traps to OS.
Some of the privileged instructions are:
1. Handling Interrupts
9
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
At system boot time, the hardware starts in kernel mode. Thus, whenever the operating
system gains control of the computer, it is in kernel mode.
The operating system is then loaded and starts user applications in user mode. The system
always switches to user mode (by setting the mode bit to 1) before passing control to a user
program.
When the computer system is executing on behalf of a user application, the system is in
user mode. However, when a user application requests a service from the operating system
(via a system call), it must transition from user to kernel mode to fulfil the request.
Or
Whenever a trap (Traps are occurred by the user program to invoke the functionality of the
OS. Assume the user application requires something to be printed on the screen, and it would
set off a trap, and the operating system would write the data to the screen.) or interrupt occurs,
the hardware switches from user mode to kernel mode (that is, changes the state of the
mode bit to 0).
Timer
At every point, we must ensure that the operating system maintains control over the CPU.
We can’t allow a user program to get stuck in an infinite loop or to fail to call system services
and never return control to the operating system. Now to accomplish this goal, we can use
10
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
a timer. A timer can be set to interrupt the computer after a specified period. The period may
be fixed (1/60 sec) or variable (1ms to 1s).
Operating System (OS) structure refers to the way an operating system is designed and organized
to manage hardware and software resources, provide services to users and applications, and ensure
efficient and secure operation of a computer system. There are several different approaches to
operating system structure, each with its own advantages and disadvantages.
Different types of structures implementing Operating Systems as mentioned below.
1. Simple Structure
2. Layered Structure
3. Micro Kernel Structure
4. Modular Structure
5. Hybrid Structure
Simple Structure
It is the simplest Operating System Structure and is not well defined. It can only be used for small
and limited systems. In this structure, the interfaces and levels of functionality are well separated,
hence programs can access I/O routines which can cause unauthorized access to I/O routines.
This structure is implemented in MS-DOS operating system. The MS-DOS operating System is
made up of various layers, each with its own set of functions.
11
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Layered Structure
In this type of structure, OS is divided into layers or levels. The hardware is on the bottom
layer (layer 0), while the user interface is on the top layer (layer N). These layers are arranged in
a hierarchical way in which the top-level layers use the functionalities of their lower-level
levels.Example:Linux
The following are some of the key characteristics of a layered operating system structure:
Each layer is responsible for a specific set of tasks. This makes it easier to understand,
develop, and maintain the operating system.
Layers are typically arranged in a hierarchy. This means that each layer can only use the
services provided by the layers below it.
Layers are independent of each other. This means that a change to one layer should not
affect the other layers.
12
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Micro-Kernel structure
Kernel is the core part of an operating system that manages system resources. It also acts as a
bridge between the application and hardware of the computer. It is one of the first programs
loaded on start-up (after the Bootloader).
A microkernel is a type of operating system kernel that is designed to provide only the most
basic services required for an operating system to function, such as memory management and
13
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
process scheduling. Other services, such as device drivers and file systems, are implemented as
user-level processes that communicate with the microkernel via message passing. Ex: Mac OS
Modular Structure
In a modular operating system structure, the operating system is divided into a set of independent
modules. Each module is responsible for a specific task, such as memory management, process
scheduling, or device drivers. Modules can be loaded and unloaded dynamically, as needed. EX:
Solaris
14
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Hybrid Structure
Hybrid Structure as the name suggests consists of a hybrid of all the Structure explained so far and
hence it has properties of all of those architectures which makes it highly useful in present-day
operating systems. The hybrid-Structure consists of three layers.
1) Hardware abstraction layer: It is the interface between the kernel and hardware and is present
at the lowest level.
2) Microkernel Layer: This is the old microkernel that we know and it consists of CPU
scheduling, memory management, and inter-process communication.
3) Application Layer: It acts as an interface between the user and the microkernel. It contains
functionalities like a file server, error detection, I/O device management, etc.
15
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Advantages:
1. Since it is a hybrid of other structures it allows various structures to provide their services
respectively.
2. It is easy to manage because it uses a layered approach.
3. The number of layers is relatively lesser.
4. Security and protection are relatively improved.
Disadvantage:
1. It increases overall complexity of system by implementing both structure (monolithic and
micro) and making the system difficult to understand.
Linux Architecture
Linux is similar to other operating systems you may have used before, such as Windows, macOS
(formerly OS X), or iOS. Like other operating systems, Linux has a graphical interface, and the same
types of software you are accustomed to, such as word processors, photo editors, video editors, and
so on.
Linux was created in 1991 by Linus Torvalds, a then-student at the University of Helsinki. Torvalds
built Linux as a free and open-source alternative to Minix, another Unix clone that was predominantly
used in academic settings. He originally intended to name it “Freax,” but later renamed it as “Linux”
after a combination of Torvalds’ first name and the word Unix.
The diagram illustrates the structure of the Linux system, according to the layers concept.
16
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
The Linux architecture is largely composed of elements such as the Applications, Shell, Kernel,
Hardware layer, System Utilities and Libraries.
Applications
Applications are the programs that the user runs on top of the architecture. The applications are the
user space element that includes database applications, media players, web browsers, and
presentations.
Shell
Shell is the interface that interacts with humans and processes the commands that are given for the
execution. One can call it an interpreter because it takes the command from the keyboard and makes
it understandable to the kernel.
Shells are categorized into two sections:
Command Line Shell
Graphical User Shell
The command line shell is the user interface where the user types commands in a text form. When
the user provides the command in the terminal, the shell interprets the commands for the kernel. The
shell also has some built-in commands that help the user to navigate, manage, and change the file
system
The graphical user shell is the user interface using the system’s peripheral components like a mouse,
and keyboard. It is beneficial for users who are not familiar with commands. These shells are used to
make the desktop environment easier.
17
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Kernel
In Linux operating systems, the kernel is the core component that acts as the bridge between computer
hardware and applications. It is responsible for managing the system's resources and allowing
software and hardware to communicate with each other.
System Utilities and Libraries
The system utilities and libraries provide a wide range of functions to manage the system. Low-level
hardware complexity to high-level user support is served by the system utilities and libraries.
System Utilities: It is the program that performs the task which is given by the users and
manages the system. (Antivirus, Winrar, Winzip etc)
System Libraries: Functions through which the system interacts with the kernel and handles
all the functionalities without the kernel privileges
Hardware
Hardware layer of Linux is the lowest level of operating system track. It is plays a vital role in
managing all the hardware components. It includes device drivers, kernel functions, memory
management, CPU control, and I/O operations. This layer generalizes hard complexity, by
providing an interface for software by assuring proper functionality of all the components.
Some Linux distributions are: MX Linux, Manjaro, Linux Mint, elementary, Ubuntu, Debian,
Solus, Fedora, openSUSE, Deepin.
18
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
1. Portable: Linux OS can perform different types of hardware and the kernel of Linux supports
the installation of any type of hardware environment.
2. Hierarchical file system: Linux OS affords a typical file structure where user files or system
files are arranged.
3. Open source: Linux operating system source code is available freely and for enhancing the
capability of the Linux OS, several teams are performing in collaboration.
4. Multiprogramming: Linux OS can be defined as a multiprogramming system. It means more
than one application can be executed at the same time.
5. Security: Linux OS facilitates user security systems with the help of various features of
authentication such as controlled access to specific files, password protection, or data
encryption.
6. Multi-user: Linux OS can also be defined as a multi-user system. It means more than one
user can use the resources of the system such as application programs, memory, or RAM at
the same time.
7. Shell: Linux operating system facilitates a unique interpreter program. This type of program
can be applied for executing commands of the operating system. It can be applied to perform
various types of tasks such as call application programs and others.
Linux Commands
echo command
The echo command in Linux and Unix-like operating systems is a built-in command used primarily
to display messages or output text to the terminal or into a file.
Syntax
echo [option] [string]
1.How to input a text to get the output using the echo command?
x=100
Input: echo “The value of x =$x”
19
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Output: Linux
Commands
Note: The Linux "echo" command provides the "-e" option, which instructs the command to
interpret backslash escape sequences in the specified text. By utilizing the '\n' sequence, we can
add a new line and print the subsequent text on a new line below the previous one. This enables
us to format the output and make it more readable by separating it into distinct lines.
PATH
PATH is an Environment variable in Linux. Environment variables are dynamic values that affect
the processes or programs on a computer. It essentially tells the shell which directories to search
through to find the executable files (programs or scripts) that match the command names entered
20
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
by the user. This search process occurs in the order the directories are listed within the PATH
variable.
Output
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games
The PATH variable contains a list of directories separated by colons (:).When a command is
entered, the shell searches for an executable file with the command's name starting from the
first directory listed in the PATH variable and proceeds to the next if it's not found. This
continues until either the executable is found or the list is exhausted. If the executable is not
found in any of the directories listed in PATH, the shell typically returns a "command not
found" error.
A directory can be added to PATH in two ways: at the start or the end of a path.
Adding a directory (/the/file/path for example) to the start of PATH will mean it is checked first:
export PATH=/the/file/path:$PATH
echo $PATH
Output:
the/file/path:usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games
Adding a directory to the end of PATH means it will be checked after all other directories:
export PATH=$PATH:/the/file/path
echo $PATH
Output:
usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/ the/file/path
Multiple directories can be added to PATH at once by adding a colon: between the directories:
export PATH=$PATH:/the/file/path:/the/file/path2
echo $PATH
21
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Output:
usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lo
cal/games:/ the/file/path:/the/file/path2
man
The "man" is a short term for manual page. In Unix like operating systems such as Linux, man
is an interface to view the system's reference manual. A user can request to display a man page
by simply typing man followed by a space and then argument. Here its argument can be a
command, utility or function. A manual page associated with each of these arguments is
displayed.
By default, man looks in all the available sections of the manual and shows the first match (even
if the page exists in several sections). Providing a section number instructs the man command to
look in a specific section.
There are nine sections of the manual:
1. General commands: Commands used in the terminal.
2. System calls: Functions the kernel provides.
22
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
printf
printf command is used to output a given string, number or any other format specifier. The
command operates the same way as printf in C, C++, and Java programming languages.
23
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Example
$ printf"%s""Hello, Welcome to KMIT”
Output: Hello, Welcome to KMIT
script
script command in Linux is used to make typescript or record all the terminal activities. After
executing the script command, it starts recording everything printed on the screen including the
inputs and outputs until exit. By default, all the terminal information is saved in the
file typescript, if no argument is given.
script
We can specify a filename as an argument to save the output to a different file:
script my_session.log
To end the recording session, type exit or press Ctrl-D. This will return you to your normal
terminal session and stop recording.
Example:
script my_session.log
echo "Hello, World!"
ls
exit
passwd
The passwd command in Linux and Unix-like operating systems is used to change the password
of a user account. It can be used by both the system administrators to change other users' passwords
and by individual users to update their own passwords.
Basic Usage
For a user to change their own password, they would simply type:
passwd
Upon execution, the system prompts the user to enter their current password (for verification),
followed by the new password, and then to retype the new password for confirmation.
uname
The uname command in Linux and Unix-like operating systems is used to display system
information. It provides details about the kernel name, version, and other system information. By
default, without any options, uname will print the kernel name.
24
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Options
The uname command supports several options that can be used to display specific system
information:
-a, --all: Print all available system information (kernel name, nodename, kernel release,
kernel version, machine, processor, hardware platform, and operating system).
-s, --kernel-name: Print the kernel name.
-n, --nodename: Print the network node hostname.
-r, --kernel-release: Print the kernel release.
-v, --kernel-version: Print the kernel version.
-m, --machine: Print the machine hardware name (e.g., x86_64).
-p, --processor: Print the processor type or "unknown".
-i, --hardware-platform: Print the hardware platform or "unknown".
-o, --operating-system: Print the operating system.
Example
Displaying the Kernel Name
Simply typing uname without any options will display the kernel name
Example output might be Linux for a Linux system.
who
The who command in Linux and Unix-like operating systems is used to display information
about users who are currently logged into the system. It provides a list of users, the terminals
they are logged in from, the login time, and sometimes the host from which they are accessing
the system.
Basic Usage
To run the command, simply type:
who
This will display a list that typically includes the username, terminal name , the date and time
of login, and the remote host name or IP address from which the user is accessing the system.
Options
The who command supports several options that can alter its output or provide additional
information:
-a, --all: Show all information, combining the effects of many other options.
25
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
-m: Same as who am i, showing information about the current terminal. It's equivalent
to running who with the $USER variable.
-q, --count: Display only the names and number of users currently logged on.
-H, --heading: Include column headers in the output.
-r, --runlevel: Show the current runlevel. This is useful in multi-user
environments and for system administrators.
-u, --users: Show the current login name, terminal line, login time, idle time and
the exit status of a process. The idle time is particularly useful for finding out how long
a terminal has been inactive.
Date
The date command in Linux and Unix-like operating systems is used to display or set the system's
date and time. By default, running the date command without any options will display the current
date and time according to the system's settings.
Basic Usage
To display the current date and time, simply type:
date
Output: Tue Mar 9 12:34:56 PST 2021
Options
The date command supports several options to format the output, set the system's date and time,
and more. Some commonly used options include:
+%FORMAT: Allows you to specify the output format of the date/time. For example, date
+"%Y-%m-%d %H:%M:%S" would output the date and time in the format YYYY-MM-
DD HH:MM:SS.
-u, --utc, --universal: Displays or sets the Coordinated Universal Time (UTC).
we can pass the strings like "yesterday", "monday", "last monday" "next monday", "next month",
"next year," and many more.
Consider the below commands:
1. date -d now
2. date -d yesterday
3. date -d tomorrow
4. date -d "next monday"
26
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
To change the system clock manually, use the set command. For example, to set the date and time
to 5:30 PM, May 13, 2010, type:
pwd
The pwd command in Linux and Unix-like operating systems stands for "Print Working
Directory." It is used to display the full pathname of the current working directory. The current
working directory is the directory in which the user is currently operating in the shell.
Basic Usage
To use the command, simply type:
pwd
Upon execution, pwd outputs the absolute path of the current working directory to the terminal.
This path is a full path from the root of the filesystem (/) to the directory you are currently in.
Example
If you are currently in the home directory of a user named user, running pwd might output
something like:/home/user
cd
In Linux, the cd command stands for "change directory." It's a shell command used to change the
current working directory in the command line interface. The cd command is one of the most
frequently used commands in Linux, as navigating between directories is a common task.
If you want to go to a new directory, you can write cd followed by the absolute path of the
directory – cd /home/username/Music.
27
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Command Description
cd to go to the home folder
cd.. to move one directory up
cd- move to your previous directory
ls
The ls command in Linux is used to list the contents of a directory. It's one of the most commonly
used commands in Linux for navigating the filesystem and viewing the files and subdirectories
contained within a directory. By default, ls will list the contents of the current working directory.
ls command syntax
ls [Options] [File]
Some of the common option tags that you can use with the ls command:
Option Description
ls –S sorts and lists all the contents in the specified directory by size
touch
The touch command in Linux is used primarily to create new, empty files and to change the
timestamps of existing files. It's a versatile utility for file handling, particularly useful for
developers and system administrators for various tasks such as file manipulation, script creation,
and timestamp management.
touch newfile.txt
mv
The mv command in Linux is used for moving or renaming files and directories. It is a versatile
29
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
command that is frequently used for organizing files, updating their locations, or changing their
names. The basic syntax for the mv command is:
mv [options] source destination
Here are the primary ways to use the mv command:
Moving Files
To move a file from one location to another, you specify the current path of the file as the source
and the desired path as the destination.
mv /path/to/source/file.txt /path/to/destination/
This command moves file.txt from its current location to a different directory.
Renaming Files
To rename a file, you use mv with the current filename as the source and the new filename as the
destination, within the same directory.
mv oldfilename.txt newfilename.txt
This command renames oldfilename.txt to newfilename.txt.
rm
The rm (remove) command in Linux is used to delete files and directories from the filesystem.
It's a powerful tool that must be used with caution, as once a file is deleted using rm, it typically
cannot be recovered easily.
Removing Files: To remove a single file, use the rm command followed by the filename:
rm filename.txt
This command deletes filename.txt from the filesystem.
30
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Removing Multiple Files: You can also delete multiple files at once by listing each file as an
argument:
rm file1.txt file2.txt file3.txt
Removing Directories: To remove a directory and its contents, you need to use the -r (or --
recursive) option, which tells rm to remove directories and their contents recursively.
rm -r directoryname
This command deletes directoryname and everything within it, including all files and
subdirectories.
To modify the command, add the following options:
-i – prompts a confirmation before deletion.
-f – allows file removal without a confirmation.
-r – deletes files and directories recursively.
mkdir
The mkdir command in Linux is used to create new directories. It stands for "make directory,"
and it allows users to create one or multiple directories at once.
To create a single directory, use the mkdir command followed by the name of the directory
you want to create:
mkdir newdirectory
This command creates a new directory named newdirectory in the current working directory.
31
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
rmdir
The rmdir command in Linux is used to remove empty directories. It stands for "remove
directory," and its primary purpose is to delete directories that are no longer needed. However, it's
important to note that rmdir will only remove a directory if it is empty. If the directory contains
files or subdirectories, rmdir will not delete it and will instead show an error message.
To remove a single empty directory, use the rmdir command followed by the name of the
directory you want to delete:
rmdir directoryname
This command removes the directory named directoryname if it is empty.
32
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
The tar command in Linux is a highly versatile tool used for creating and manipulating archive
files. The name "tar" stands for "Tape Archive," reflecting its original purpose for writing data to
tape drives. Over time, it has become a standard utility for file archiving and compression in Unix-
like operating systems. tar archives multiple files and directories into a single file (often called a
tarball), which can be optionally compressed using various compression algorithms. Here's how
to use the tar command with some of its most common options:
Creating an Archive
To create a .tar archive, use the -c option (for create), -f followed by the archive file name, and
then list the files and directories you want to archive:
tar -cf archive_name.tar file1 directory1 file2
This command creates an archive named archive_name.tar containing file1, directory1, and
file2.
Extracting an Archive
To extract the contents of a .tar archive, use the -x option (for extract) and -f followed by the
archive file name:
tar -xf archive_name.tar
This command extracts the contents of archive_name.tar into the current working directory.
33
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
gzip
gzip command compresses files. Each single file is compressed into a single file. The
compressed file consists of a GNU zip header and deflated data. If given a file as an argument,
gzip compresses the file, adds a “.gz” suffix, and deletes the original file.
gzip [Options] [filenames]
This syntax allows users to compress a specified file. Now, let’s delve into some practical
examples to illustrate the usage of the gzip command.
Options Description
Forcefully compress a file even if a compressed version with the same name
-f already exists.
Compress a file and keep the original file, resulting in both the compressed and
-k original files.
Display the name and percentage reduction for each file compressed or
-v decompressed.
34
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
Example:
gzip mydoc.txt
This command will create a compressed file of mydoc.txt named as mydoc.txt.gz and delete the
original file.
The basic syntax of the gzip command for decompressing a file is as follows:
gzip -d filename.gz
This command decompresses the specified gzip file, leaving the original uncompressed file
intact.
By default, gzip removes the original file after compression. To retain the original file, use the -
k option:
gzip -k example.txt
This command compresses “example.txt” and keeps the original file intact.
In cases where the compressed file already exists, the -f option forcefully overwrites it:
gzip -f example.txt
This command compresses “example.txt” and overwrites any existing “example.txt.gz” file
Gzip can compress multiple files simultaneously by providing their names as arguments:
gzip file1.txt file2.txt file3.txt
This command compresses “file1.txt,” “file2.txt,” and “file3.txt” individually.
35
KR23 OPERATING SYSTEMS CSE/IT/CSM/CSD II/II
cat
The cat command in Linux is a short form for "concatenate." It is one of the most frequently used
commands in Unix and Linux operating systems for various purposes. The primary function of
cat is to read (display), combine, and copy text files. Here are some common uses and examples
of how the cat command can be utilized:
Display the Contents of a File
To display the contents of a file, simply use cat followed by the file name. For example:
cat filename.txt
This command will display the contents of filename.txt on the standard output (usually the
terminal).