Unix and Shell Programming_BCA3rdYear
Unix and Shell Programming_BCA3rdYear
Debashis Chowdhury
Netaji Subhash Engineering College
1
Topics
1. Introduction to UNIX
1.1 Introduction to UNIX: Overview and History ……………... 3
1.2 History of UNIX ……………………………………………….6
1.3 UNIX Architecture …………………………………………… 9
1.4 UNIX System Calls and POSIX Standards ………………16
1.5 Basic UNIX Commands (Internal and External) …………19
1.6 UNIX Utilities: Basic System Interaction Commands …. 22
1.7 Review Questions and Answers …………………………. 37
4. File attributes
4.1 Basic File Attributes ……………………………………... 78
4.2 Hard and Soft Links ………...………………………...…... 84
4.3 Review Questions and Answers …………………………. 92
5. Introduction to Shell
5.1 Introduction to Shell and Shell Types ……………….… 101
5.2 Pattern Matching and Regular Expressions …………... 103
5.3 Input/Output Redirection and Pipes ……………………. 106
5.4 Review Questions and Answers ……………………...….116
6. Process
6.1 Process Management …………………………………… 121
6.2 Review Questions and Answers …………………. 130
6.3
7. Customization
7.1 Customizing the Environment ………….…………….… 135
7.2 Review Questions and Answers ………………………. 140
8. Filters
8.1 Introduction to Filters ……………………………………. 144
8.2 Review Questions and Answers ……………………...… 156
9. Shell Script
9.1 Introduction to Shell Scripting…………………….……. 161
9.2
9.3 Review Questions and Answers ……………………...… 197
Chapter 01
Introduction to UNIX
Introduction
UNIX is a multi-user, multitasking operating system designed for efficiency, stability, and portability.
Developed in 1969 at AT&T Bell Labs by Ken Thompson, Dennis Ritchie, and others, UNIX has
become a foundation for many modern operating systems, including Linux, macOS, and BSD. It is
widely used in servers, workstations, embedded systems, and cloud computing due to its robust
security and performance.
1. Multi-User System
UNIX is a multi-user operating system, meaning multiple users can access the system and use
resources simultaneously without interfering with each other. It manages user access through
authentication mechanisms and file permissions.
How It Works:
• Each user has a unique user ID (UID) and belongs to a group with a group ID (GID).
• UNIX provides access control mechanisms to ensure that users only access their permitted
files and processes.
• Remote login (SSH, Telnet) allows users to connect to a UNIX system over a network.
Multiple users can log in to a UNIX server via SSH and run their programs independently.
3. Portability
UNIX is a portable operating system, meaning it can run on different hardware architectures with
minimal modification. This is because UNIX is primarily written in the C programming language, which
allows easy adaptation to various platforms.
How It Works:
• UNIX source code can be compiled on different machines with minimal changes.
• The use of system calls and a hardware abstraction layer makes UNIX adaptable.
UNIX can run on mainframes, servers, desktops, and embedded systems without major changes.
7. Process Management
Every task in UNIX runs as a process, which is managed by the system through process scheduling.
How It Works:
• Each process has a unique Process ID (PID).
• The ps command shows running processes.
• kill can terminate a process.
UNIX’s key features, such as multi-user support, multitasking, security, portability, and process
management, make it a powerful and versatile operating system. Its efficient design has influenced many
modern operating systems like Linux and macOS, ensuring its continued relevance in computing.
History of UNIX
The history of UNIX dates back to the late 1960s when researchers at AT&T’s Bell Labs started working
on an operating system that would be simple, portable, and efficient. Initially, UNIX emerged as a
response to the failure of the Multics project, which was a joint venture between MIT, Bell Labs, and
General Electric. In 1969, Ken Thompson and Dennis Ritchie at Bell Labs developed the first version
of UNIX on a PDP-7 minicomputer.
In 1973, UNIX was rewritten in C programming language, making it portable and easier to adapt to
different hardware systems. This innovation led to UNIX being widely adopted in universities, research
institutions, and later, commercial industries. The University of California, Berkeley (UCB) developed
Berkeley Software Distribution (BSD UNIX), introducing networking capabilities and system
enhancements that became fundamental to the modern internet.
During the 1980s, UNIX became commercialized, leading to two major versions: System V (by AT&T)
and BSD UNIX. Several technology companies, including IBM, HP, and Sun Microsystems, developed
their own UNIX versions, leading to fragmentation known as the "UNIX Wars." In response, the POSIX
standard (1988) was introduced to unify UNIX implementations.
The 1990s saw the emergence of Linux (1991) by Linus Torvalds, an open-source UNIX-like operating
system that became a strong alternative to proprietary UNIX. Additionally, BSD UNIX evolved into
FreeBSD, OpenBSD, and NetBSD. By the 2000s, UNIX-based systems like macOS and Linux
distributions dominated enterprise and cloud computing.
Today, UNIX remains influential in server environments, cloud computing, and embedded systems.
Proprietary UNIX versions like IBM AIX, HP-UX, and Oracle Solaris are still used in enterprise
applications, while Linux and BSD variants continue to power modern computing infrastructures.
UNIX has had a profound impact on the computing world, shaping modern operating systems,
networking, and open-source software development. From its origins at Bell Labs to the emergence of
Linux and BSD, UNIX continues to be a foundation for enterprise servers, cloud platforms, and
embedded systems.
History of Linux
• 1991: Linus Torvalds developed the Linux kernel as a personal project.
• 1992: Linux was released under the GNU GPL, allowing open-source development.
• 1993-1994: First Linux distributions (Slackware, Debian, and Red Hat) were created.
• 2000s: Linux gained popularity in enterprise servers, web hosting, and cloud computing.
• 2008: Google introduced Android, based on the Linux kernel, dominating the mobile OS market.
• Present: Linux powers supercomputers, IoT devices, servers, cloud platforms (AWS,
Google Cloud, Azure), and cybersecurity.
Linux is a powerful, secure, and highly customizable operating system that has revolutionized computing.
From enterprise servers to mobile devices and embedded systems, Linux continues to dominate due to
its open-source nature, stability, and adaptability.
UNIX follows a layered architecture, ensuring efficiency, modularity, and portability. It consists of
multiple layers, each responsible for handling specific functionalities. The core of UNIX is the kernel,
which interacts directly with hardware, while users interact with the system through commands and
applications.
Architecture of UNIX
• Process Management: Handles process creation, scheduling, and termination using techniques
like multiprogramming and time-sharing.
• Memory Management: Allocates and deallocates memory for processes, implements virtual
memory and paging.
• File System Management: Manages file operations, directories, permissions, and storage.
• Security and Access Control: Implements user authentication, file permissions, and encryption.
The UNIX architecture is designed with modularity, security, and flexibility. The kernel manages
system resources, while the shell provides an interface for user commands. Various utilities and
applications allow users to perform tasks efficiently. This layered structure makes UNIX a powerful,
stable, and scalable operating system, widely used in servers, cloud computing, and enterprise
environments.
Kernel in UNIX
The Kernel is the core component of UNIX, responsible for directly interacting with the hardware and
managing system resources such as CPU, memory, file system, and input/output devices. It acts as a
bridge between hardware and software, ensuring efficient execution of processes.
Functions of Kernel:
1. Process Management:
o Creates, schedules, and terminates processes.
o Implements multitasking and time-sharing.
o Uses process scheduling algorithms to allocate CPU time efficiently.
o Commands: ps, kill, nice, jobs
2. Memory Management
o Allocates and deallocates memory dynamically.
Shell in UNIX
The Shell is a command-line interpreter that acts as an interface between the user and the kernel. It
takes user commands, processes them, and passes them to the kernel for execution.
Functions of Shell:
1. Command Execution: Accepts user input, interprets commands, and executes them. Provides
command-line and scripting capabilities.
Commands: ls, cd, cp, rm, echo
2. Shell Scripting: Supports scripting for automating repetitive tasks. Includes variables, loops,
conditions, and functions.
Commands: sh script.sh, bash script.sh
3. Input/Output Redirection: Redirects input/output to files or other commands. Operators: >, <,
>>, |
Netaji Subhash Engineering College
14
Example: ls > file.txt (writes output to file.txt)
4. Pipelines and Filters: Allows chaining multiple commands to process data efficiently.
Example: ls -l | grep "file"
5. Environment Management: Manages environment variables (PATH, HOME, USER). Allows
users to customize shell behaviour.
Commands: export, env, set, unset
6. User Interaction and Process Control: Provides an interface for user login and interactive
command execution. Allows background and foreground process management.
Commands: jobs, fg, bg, kill
3. C Shell (csh)
The C Shell, developed by Bill Joy at the University of California, Berkeley, is known for its C-like
syntax, making it easier for C programmers to adapt. It introduced several innovations like command
history, aliases, and job control. However, its scripting capabilities are more limited and can behave
differently compared to Bourne-compatible shells.
Key Features:
• Syntax similar to C programming
• Command history and alias support
• Job control (running background tasks)
• Interactive use over scripting
• File extension for scripts: .csh
Summary Table
Role Core of UNIX, manages hardware and Command interpreter, user interface.
system resources.
Interaction Directly interacts with hardware. Acts as an interface between the user
and kernel.
Examples Linux Kernel, XNU (macOS), Minix Bash, sh, csh, ksh, zsh.
Kernel.
Process Control Create, terminate, manage fork(), exec(), exit(), wait(), kill()
processes.
File Management Open, close, read, write files. open(), read(), write(), close(),
unlink()
Purpose of POSIX
POSIX was developed to solve the issue of fragmentation among UNIX-based operating systems.
Different UNIX vendors had their own implementations, leading to portability challenges for applications.
POSIX standardizes APIs (Application Programming Interfaces), shell commands, utilities, and system
calls to provide a uniform interface.
o IBM AIX
o HP-UX
o Solaris (earlier versions)
o QNX (Real-time OS)
Advantages of POSIX
• Portability: Applications written for one POSIX-compliant OS can run on another with minimal
changes.
• Interoperability: Encourages a consistent development environment across different UNIX-like
systems.
• Code Reusability: Reduces redundancy in software development.
• Standardized Programming Interface: Simplifies the development of cross-platform
applications.
POSIX is an essential standard for ensuring compatibility and portability among UNIX-like operating
systems. By defining system calls, shell utilities, and APIs, POSIX enables developers to write software
that can run across different environments with minimal modifications.
1. Internal Commands
Internal commands are executed within the shell itself. They are faster since they do not require disk
access.
Common Internal Commands
2. External Commands
External commands are stored in specific directories (/bin, /usr/bin, /sbin, etc.) and executed when
called.
Find Searches for files in a directory hierarchy find /home -name "*.txt"
grep Searches text in files grep "error" logfile.txt
Tar Archives files into a tarball tar -cvf archive.tar file1 file2
Zip Compresses files into a .zip archive zip archive.zip file1 file2
Speed Faster (no disk access) Slightly slower (requires disk access)
Storage Location Built into the shell Located in /bin, /usr/bin, etc.
Understanding UNIX commands is essential for interacting with the system effectively. Internal
commands are built into the shell, while external commands exist as separate binaries. Mastering these
commands improves system navigation, file management, and overall efficiency in UNIX-based
environments.
Use basic UNIX utilities to interact with the system effectively. These utilities provide essential system
information, perform calculations, and allow users to display or manipulate text.
cal (Calendar)
The cal command displays a calendar for a specific month or year. By default, it shows the current month.
Syntax:
cal [month] [year]
Examples:
Command Output
cal Displays the current month’s calendar
cal 3 2025 Displays the calendar for March 2025
cal -y Shows the entire current year’s calendar
cal -m Monday Starts the week from Monday
Syntax:
date [options] [+format]
Examples:
Command Output
Date Displays the current system date and time
date '+%Y-%m-%d' Shows date in YYYY-MM-DD format
date '+%H:%M:%S' Displays the current time in HH:MM:SS format
date '+%A, %B %d, %Y' Prints full day, month, and year (e.g., "Wednesday, March 13,
2025")
date -s "2025-03-13 10:30:00" Sets the system date and time (root privileges required)
• Display the current date and time: • Display the date in YYYY-MM-DD format:
date date +"%Y-%m-%d"
Output: Output:
Tue Mar 13 14:30:15 IST 2025 2025-03-13
Output: Output:
2025 14:30:15
Example:
sudo date 031314302025
Syntax:
echo [options] [string]
Examples:
Command Output
Escape Sequence?
An escape sequence is a series of characters used to represent special characters that cannot be typed directly
or have special meaning in strings or terminal output.
• In UNIX and shell environments, escape sequences are used with echo, printf, shell scripts, and within
programs to control formatting or insert special characters.
Hello
World
\t Horizontal tab Hello\tWorld → Hello World
\\ Backslash \\ → \
Using Escape Sequences in Shell (with echo and printf) - echo with -e option (enables interpretation of
escapes)
• echo -e "Hello\nWorld"
• echo -e "Tabbed\ttext"
Output:
Tabbed text
Output:
Name: Alice
Age: 25
Syntax
printf "FORMAT_STRING" ARGUMENTS
Examples
Printing a Simple String
Netaji Subhash Engineering College
27
printf "Hello, World!\n"
Output:
Hello, World!
(\n is needed to add a new line, unlike echo which adds a newline automatically.)
Using Variables
name="Alice"
printf "Hello, %s!\n" "$name"
Output:
Hello, Alice!
Output:
Format Specifiers
Specifier Meaning
%s String
%d Integer
%f Floating point
%c Character
%x Hexadecimal
%o Octal
Output:
Decimal: 42, Hex: 2a, Octal: 52
Floating-Point Precision
printf "Price: %.2f\n" 99.456
Output:
Price: 99.46
Escape Sequences
Escape Sequence Meaning
\n New line
\t Tab
\r Carriage return
\\ Backslash
Output:
Item Price
Apple $1.00
Orange $0.80
Right-Align Numbers
printf "%5d\n" 42
Output:
Left-Align Strings
printf "%-10s %-10s\n" "Name" "Score"
printf "%-10s %-10d\n" "Alice" 90
printf "%-10s %-10d\n" "Bob" 85
Output:
Name Score
Alice 90
Bob 85
Features of bc
Basic Syntax
bc [options]
Modes of Using bc
5+3
2. Using Piping (echo with bc): Execute calculations directly from the command line:
echo "5+3" | bc
Output: 8
3. Using bc with a File: Store expressions in a file (calc.txt) and run them:
10 * 5
20 / 4
Execute:
bc < calc.txt
Arithmetic Operations
+ Addition 5+3 8
* Multiplication 6*7 42
Floating-Point Arithmetic
Output: 3.33
bc supports built-in functions when invoked with the -l (math library) option:
bc -l
Example:
echo "scale=4; sqrt(49)" | bc -l
Output: 7.0000
Output: 50
Example: Example:
echo "5 > 3" | bc echo "(5 > 3) && (4 < 6)" | bc
Output: Yes
For Loop in bc
echo "for (i=1; i<=5; i++) print i" | bc
Output: 12345
Examples
Output: Output:
11001 25
Important Note: Set ibase before obase because changing ibase affects how the rest of the input is
read.
Output: Output:
FF 255
5. Octal to Decimal 6. Decimal to Octal
$ bc $ bc
ibase=8 ibase=10
obase=10 obase=8
77 63
Output: Output:
63 77
Examples
Output:
11
Output:
FF
Output:
10100011
The bc command is a powerful calculator that can handle complex arithmetic, logical operations, and scripting. It
is widely used for calculations in shell scripting and system administration tasks.
Syntax
script [options] [filename]
Example:
$ script my_terminal.log
Script started, file is my_terminal.log
$ ls
$ echo "Hello, World!"
$ whoami
$ exit
Purpose of passwd
• Allows the root (administrator) to set or change passwords for any user.
Basic Syntax
passwd [options] [username]
• Only the root user can change passwords for other users.
• You'll be prompted to enter your current password, then a new password twice.
Example interaction:
Changing password for user alice.
Current password:
New password:
Retype new password:
Option Description
Examples
Syntax:
who [options]
Examples:
Command Output
who Displays a list of logged-in users
who -u Shows login time and idle time of users
who -b Displays the last system boot time
who -q Shows the number of users logged in
Syntax:
uname [options]
Examples:
Command Output
uname Displays the operating system name
uname -a Shows detailed system information
Syntax:
tty
Examples:
Command Output
tty Displays the terminal device name (e.g., /dev/pts/0)
These UNIX utilities are essential for interacting with the system. They help users manage time, display messages,
perform calculations, and retrieve system details. Mastering these commands enhances efficiency in system
administration and scripting.
Review Question
Review Questions
Short Answer Type Questions
1. What is UNIX?
UNIX is a powerful, multiuser, multitasking operating system originally developed in the 1970s
that supports portability, flexibility, and security.
2. What are the main components of UNIX architecture?
The main components are the Kernel, which interacts with hardware, and the Shell, which
interacts with the user.
3. What is the UNIX Kernel?
The Kernel is the core of the UNIX system, managing system resources, memory, process
scheduling, and hardware communication.
4. What is the UNIX Shell?
The Shell is a command-line interpreter that reads and executes user commands, acting as an
interface between the user and Kernel.
5. What are UNIX files?
In UNIX, everything is treated as a file, including hardware devices, directories, and even
running processes.
6. What is a UNIX process?
A process in UNIX is an instance of a running program, managed by the operating system
through process IDs.
7. What is a system call in UNIX?
A system call is a programmatic way in which a process requests a service from the Kernel, like
reading files or creating processes.
8. List any two features of UNIX.
UNIX is known for its portability and multitasking capabilities. It supports multiple users and has
powerful networking features.
9. What is POSIX?
POSIX stands for Portable Operating System Interface; it is a set of standards to maintain
compatibility across UNIX systems.
10. What does "single-user specification" mean in UNIX?
Single-user specification refers to running the system in a maintenance mode where only the
root user can access the system.
11. Differentiate between internal and external commands.
Internal commands are built into the Shell, while external commands exist as separate
executable files in the system.
12. What is the use of the cal command in UNIX?
The cal command is used to display a calendar of a specific month or year on the terminal
screen.
13. What does the date command do in UNIX?
The date command displays or sets the current system date and time in UNIX.
Chapter 02
UNIX File System
The UNIX file system is a hierarchical structure that organizes and manages files and directories
efficiently. It follows a tree-like structure, starting from the root (/) directory. UNIX treats everything as
a file, including directories, devices, and even processes.
Hierarchical Structure
/ (Root) The top-level directory of the UNIX file system. All other directories stem from here.
/bin Stores essential system binaries (executables) like ls, cp, mv, cat, etc.
/boot Contains bootloader files and kernel-related files required to boot the system.
/proc Virtual file system that provides system and process information.
/srv Data for system services like web and FTP servers.
• Regular files are the most common and fundamental type of files in the UNIX operating system. These files
contain user data such as text, images, programs, or binary data.
Regular files do not include directories or special device files. When you run the ls -l command, a regular file
is indicated by a dash (-) at the beginning of the permissions string.
For example:
-rw-r--r-- 1 user group 1234 Apr 7 10:00 notes.txt
Here, -rw-r--r-- shows that it is a regular file with read and write permissions for the owner, and read-only for
others.
Regular files are used to store and manage content that users create or interact with daily, such as documents,
scripts, source code, or media.
• Directory files in UNIX are special files that serve as containers or folders for other files and directories.
Rather than storing user data like regular files, a directory file maintains metadata—essentially a list of
filenames and pointers (inodes) to their actual data locations on the disk.
In UNIX, the file system is hierarchical, and directories help organize this structure. Each directory file
contains entries for:
When you list files using ls -l, a directory is identified by a d at the beginning of the permissions field:
drwxr-xr-x 2 user group 4096 Apr 7 10:00 documents
Here, d means it’s a directory, and the permissions indicate who can read (r), write (w), or execute (x) within
it. The execute permission on a directory means the ability to enter it (with cd) or traverse it to access files.
Directories are essential for organizing files, managing access, and enabling efficient file navigation and
system maintenance.
• Device files in UNIX, device files (also known as special files) are used to represent and interact with hardware
devices like hard drives, printers, terminals, and USBs — treating them as if they were regular files.
They act as interfaces between software and hardware, allowing users and programs to perform read/write
operations on devices using standard file operations.
o Suitable for storage devices like hard disks, SSDs, USB drives.
• A symbolic link (also known as a soft link) in UNIX is a special type of file that acts as a pointer or shortcut to
another file or directory. It stores the path to the target file rather than its actual data.
Think of it like a Windows shortcut: clicking it redirects you to the original file.
• If the original file is deleted, the symbolic link becomes broken (also called a dangling link).
• A hard link in UNIX is a directory entry that directly points to the same inode (i.e., the actual data on disk) as
another file. It creates an exact replica of the original file's reference in the file system — meaning both
filenames refer to the same data blocks.
Unlike a symbolic (soft) link, a hard link doesn’t point to a filename or path — it points to the file's inode. So
even if the original file is deleted, the hard link still retains the content.
• Both the original and the hard link share the same inode number.
Output:
123456 -rw-r--r-- 2 user group 50 Apr 7 10:00 original.txt
123456 -rw-r--r-- 2 user group 50 Apr 7 10:00 duplicate.txt
Both files have the same inode number (123456) and link count 2, meaning they are hard links to the
same data.
• FIFO stands for First-In-First-Out and is a special type of named pipe in UNIX used for inter-process
communication (IPC). It allows two unrelated processes to communicate by reading and writing to a shared
file-like interface.
Netaji Subhash Engineering College
46
Unlike unnamed (anonymous) pipes, which only work between parent and child processes and exist
temporarily in memory, named pipes (FIFOs) exist permanently in the filesystem and can be accessed like a
regular file using a name.
Example:
mkfifo mypipe
• Socket files in UNIX are special types of files used for inter-process communication (IPC), particularly for
processes that communicate either on the same machine or over a network. They allow bidirectional
communication, unlike pipes or FIFOs which are typically one-way.
A UNIX socket file exists in the file system and serves as an endpoint for communication between processes.
Example:
ls -l /var/run/docker.sock
Output:
srwxr-xr-x 1 root root 0 Mar 13 14:30 /var/run/docker.sock
UNIX provides a flexible but case-sensitive file naming convention. Here are the key rules and best practices:
• Characters Allowed: File and directory names can generally include alphanumeric characters (a-z, A-Z, 0-
9), as well as special characters like underscore (_), hyphen (-), and dot (.).
• Case Sensitivity: UNIX file systems are case-sensitive. This means that myfile, MyFile, and MYFILE are
treated as three distinct files. This is a crucial point to remember to avoid confusion.
• Maximum Length: While specific limits can vary between different UNIX-like systems and file system
types, there's usually a maximum length for file and directory names (e.g., 255 characters). It's good
practice to keep names reasonably short and descriptive.
• Reserved Characters (Avoid): Certain characters have special meanings in the shell and should generally
be avoided in file names to prevent unexpected behavior or the need for quoting/escaping. These
include:
o Space ( )
o Tab
o Newline
o Pipe (|)
o Semicolon (;)
o Asterisk (*)
o Backslash (\)
o Backquotes (`)
• Hidden Files: File or directory names that begin with a dot (.) are treated as hidden files. By default,
standard listing commands like ls do not display these files. You need to use the -a or -al option with ls to
view them. These are often used for configuration files and directories specific to applications or the
user's environment.
• Best Practices:
o Avoid spaces; use underscores or hyphens instead for readability (e.g., report_2023.txt or user-
manual.pdf).
o Stick to lowercase for general files to avoid case-related errors, especially when working across
different systems.
o Use extensions (e.g., .txt, .jpg, .py) to indicate the file type.
The UNIX file system is organized as a hierarchical tree structure, starting from a single root directory (/). This
structure naturally creates parent-child relationships between directories and files:
• Parent Directory: A directory that contains other directories or files is considered the parent of those
contained items. Every directory (except the root directory) has a parent directory.
• Child Directory/File: A directory or file located directly within another directory is considered a child of
that directory.
Think of it like a family tree. The root directory (/) is like the ultimate ancestor. Directories directly under / are its
children. If a directory /home contains a subdirectory /home/user1, then /home is the parent of user1, and
user1 is a child of /home. Similarly, if /home/user1 contains a file document.txt, then /home/user1 is the parent
of document.txt, and document.txt is a child of /home/user1.
HOME Variable
The HOME environment variable is a crucial concept in UNIX. The HOME environment variable stores the
absolute pathname of the user's home directory. When a user logs in, this variable is automatically set by the
system.
o User's Personal Space: The home directory is the default working directory for a user upon
login. It's intended as the user's personal space to store their files, configuration files, and
personal directories.
o Convenient Path Reference: Many commands and applications use the HOME variable as a
shortcut to refer to the user's home directory without needing to know the full path. For
example, cd without any arguments will typically take you to your home directory (which the
system knows from the HOME variable). Similarly, paths like ~/.bashrc or ~/Documents use the
tilde (~), which the shell expands to the value of the HOME variable.
o Configuration Files: Many application-specific configuration files are stored in hidden directories
or directly within the user's home directory (often starting with a dot, like .config, .ssh, etc.).
• Accessing the HOME Variable: You can see the value of the HOME variable using the echo command in
the shell:
echo $HOME
The output will be the absolute path to your home directory (e.g., /home/your_username on many Linux
systems).
inode Number
Every file and directory in a UNIX file system is associated with a unique identifier called an inode (index node)
number.
• Definition: An inode is a data structure on disk that stores metadata about a file or directory. This
metadata includes:
o Pointers to the data blocks on disk where the actual file content is stored.
• Significance:
o Unique Identification: The inode number uniquely identifies a file or directory within a specific
file system. Two files on the same file system will never have the same inode number.
o Name Independence: The inode number is independent of the file's name. A single file can have
multiple names (through hard links), but all those names will point to the same inode.
o How to View: You can see the inode number of a file or directory using the -i option with the ls
command:
ls -i myfile.txt
The output will show the inode number followed by the filename.
Absolute Pathname
An absolute pathname (also known as a full pathname) specifies the exact location of a file or directory starting
from the root directory (/).
• Characteristics:
o Always begins with a forward slash (/), indicating the root directory.
o Provides an unambiguous and unique path to a specific file or directory, regardless of the
current working directory.
o Traces the path down the directory hierarchy from the root to the target file or directory,
separating each directory level with a forward slash.
• Example: If you have a file named report.txt located in the directory /home/user1/documents, its
absolute pathname is /home/user1/documents/report.txt.
Relative Pathname
A relative pathname specifies the location of a file or directory relative to the current working directory.
• Characteristics:
o Uses the special directory entries . (dot) and .. (dotdot) to navigate the directory hierarchy
relative to the current position.
• Example: If your current working directory is /home/user1, and you want to access the file report.txt
located in /home/user1/documents, the relative pathname would be documents/report.txt.
• Navigating Up: If your current working directory is /home/user1/documents, and you want to refer to
the directory /home/user1 (the parent directory), you would use the relative pathname ... To refer to a
file config.txt in /home/user1, the relative path would be ../config.txt.
The special directory entries . (dot) and .. (dotdot) are fundamental for navigating the file system using relative
pathnames:
o For example, ./my_script.sh explicitly tells the shell to execute the my_script.sh file located in
the current directory. This is important for security reasons, as the system's PATH environment
variable might not include the current directory.
o For example, if you are in /home/user1/documents, the command cd .. will take you to
/home/user1.
Each file and directory in UNIX has associated permissions for different users.
Example:
ls -l
Output:
-rw-r--r-- 1 user user 1234 Mar 13 14:30 myfile.txt
Permission Breakdown
Position Meaning
-rw-r--r-- File Type & Permissions
Changing permissions:
chmod 755 myfile.txt
Changing ownership:
chown user:group myfile.txt
Summary
Concept Description
Conclusion
Understanding the UNIX file system structure and file types is crucial for managing files, processes, and system
resources efficiently. By leveraging this knowledge, users can navigate and control the UNIX environment
effectively.
Managing files and directories is a crucial skill in UNIX, allowing users to navigate the filesystem,
create, modify, and delete files, and organize directories efficiently. Below is a detailed
explanation of essential UNIX commands used for file and directory management.
• Common options:
▪ ls -l → Detailed view (permissions, owner, size, modification time).
▪ ls -a → Includes hidden files (starting with .).
▪ ls -lh → Displays human-readable file sizes.
▪ ls -R → Recursively lists subdirectories.
▪ ls -lt → Sorts files by modification time.
ls -lh
Output:
-rw-r—r-- 1 user user 2.0K Mar 10 12:34 file1.txt
drwxr-xr-x 2 user user 4.0K Mar 9 16:10 myfolder
Example:
mkdir myfolder
• rmdir (Remove Empty Directory): The rmdir command in UNIX is used to delete empty
directories from the file system.
Syntax:
rmdir [options] directory_name
Example:
rmdir myfolder
• rm (Remove Directory and Its Contents): The rm command in UNIX is used to delete files or
directories. When used with the -r option, it can remove entire directories along with their
contents, including subdirectories and files.
Syntax:
rm [options] file_or_directory_name
Examples:
• Remove a file:
rm file.txt
Common Options:
Option Description
-r Recursively delete directories and subdirectories
-f Force deletion without prompting
-i Prompt before each deletion
-v Verbose – shows what is being deleted
3. File Management Commands: File management commands in UNIX allow users to create, view,
modify, move, copy, and delete files. These commands are essential for day-to-day tasks in a UNIX
environment.
• touch (Create Empty File or Update Timestamp): Creates new empty files or updates the last
modified time of an existing file.
Syntax:
touch filename
Example:
touch project.txt
Syntax:
cp [options] source destination
Examples:
• Copy a file:
Netaji Subhash Engineering College
55
cp file.txt backup.txt
Option Description
-r Recursively copy directories
-i Prompt before overwrite
-f Force overwrite without prompting
-u Copy only if source is newer than destination
-v Verbose – show what is being copied
• To rename a directory:
mv olddir newdir
• To decompress:
gunzip file.txt.gz
Conclusion
Mastering file and directory management commands in UNIX helps users efficiently navigate and
manipulate files and directories. Understanding these commands improves productivity and control
over the system.
Review Questions
Review Questions
Short Answer Type Questions
1. What is a file system in UNIX?
A file system in UNIX is a method for storing, organizing, and managing files and directories in a
hierarchical tree structure.
2. List different types of files in UNIX.
UNIX supports regular files, directories, symbolic links, character device files, block device files,
named pipes, and sockets.
3. What is the significance of the / directory in UNIX?
The / directory, known as the root directory, is the top-level directory from which all other
directories branch.
4. What are valid characters in UNIX file names?
File names may include letters, numbers, dots (.), underscores (_), and hyphens (-), but not
slashes (/) or null characters.
5. Explain parent-child relationship in UNIX directories.
Each directory (except root) has a parent directory. A subdirectory created inside another is
considered its child.
6. What is the HOME variable in UNIX?
The HOME variable stores the absolute path of the user’s home directory, providing a default
location for login and file operations.
7. Define an inode number.
An inode number uniquely identifies a file or directory and stores metadata such as
permissions, owner, and location on disk.
8. What is an absolute pathname?
An absolute pathname starts from the root / and specifies the full path to a file or directory,
independent of current location.
9. What is a relative pathname in UNIX?
A relative pathname indicates the location of a file or directory with respect to the current
working directory.
10. What does the dot (.) signify in UNIX?
The single dot (.) refers to the current directory and is used in relative paths and execution of
files in the same directory.
11. What does double dot (..) represent in UNIX?
The double dot (..) represents the parent directory, useful for moving up in the file hierarchy or
referencing parent files.
12. How can you display the current working directory in UNIX?
The pwd (print working directory) command displays the full absolute path of the current
working directory.
13. What is the use of cd command in UNIX?
The cd command is used to change the current working directory to the specified path or to the
user’s home directory.
Netaji Subhash Engineering College
60
14. How do you create a new directory in UNIX?
The mkdir command is used to create one or more new directories in the specified location.
15. How do you remove an empty directory in UNIX?
The rmdir command deletes an empty directory. It will fail if the directory contains files or
subdirectories.
16. What does the ls command do in UNIX?
The ls command lists the contents of a directory, showing files and subdirectories optionally with
detailed information using flags.
17. What is the purpose of the /bin directory in UNIX?
The /bin directory contains essential binary executables like ls, cp, and mkdir, needed for
booting and basic operation.
18. What is stored in the /etc directory?
The /etc directory contains system-wide configuration files and shell scripts used during system
startup and service management.
19. What does the /home directory represent in UNIX?
The /home directory contains the personal directories of all non-root users, storing their files and
personal configurations.
20. What is /dev used for in UNIX file system?
The /dev directory holds special files representing devices, such as hard drives, terminals, and
USB devices, for hardware interaction.
Chapter 03
Ordinary file handling
File handling is a fundamental aspect of working with the UNIX operating system. In UNIX, everything is treated
as a file — whether it is a text file, a directory, a device, or a program. Among the different types of files, ordinary
files are the most commonly used. These are regular files that store data in a structured or unstructured format,
such as plain text, source code, binary executables, or logs.
Ordinary file handling refers to the set of operations that users and programs perform on regular files, including
creating, reading, writing, modifying, copying, renaming, and deleting files. UNIX provides powerful command-
line utilities (like cat, cp, mv, rm, more, less, touch, etc.) and system calls (such as open(), read(), write(), and
close()) to perform these operations.
• This command allows the user to create a new file named filename.
• The system enters input mode, and the user can type the content of the file.
• To save the file, press Ctrl + D.
Displaying the Contents of a File:
cat filename
• This combines file1 and file2 into a new file named mergedfile.
Example:
cp file1 file2
• This copies the contents of file1 into file2. If file2 exists, it will be overwritten.
Copying a Directory:
cp -r dir1 dir2
• The -r option (recursive) ensures that all files inside dir1 are copied to dir2.
Preserving File Attributes:
cp -p file1 file2
Example:
rm file1
• The -r (recursive) option ensures that all files and subdirectories inside the directory are
removed.
Forcing Deletion Without Confirmation:
Renaming a File:
mv oldname newname
Example:
wc file1
Features:
• Press Enter to move one line forward.
• Press Space to move one screen/page forward.
• Press q to quit.
Example:
more /etc/passwd
This command displays the contents of the /etc/passwd file one page at a time.
Common Options:
• -n number: Print multiple copies.
• -d printername: Specify a particular printer.
Example:
lp document.txt -- This sends document.txt to the default printer.
Example:
• file myscript.sh
• file image.png
Output:
image.png: PNG image data, 800 x 600, 8-bit/color RGB
Example:
cmp file1.txt file2.txt
If there is a difference:
file1.txt file2.txt differ: byte 10, line 2
Best for comparing binary files or checking if two files are exactly the same.
Sample Output:
apple
banana
cherry
Then:
diff file1.txt file2.txt
Explanation:
• Line 2 in file1.txt ("Welcome") was changed (c) to "Welcome back" in file2.txt.
Options:
• -c : Create an archive
• -v : Verbose (show progress)
• -f : Filename of archive
Example:
tar -cvf myarchive.tar file1.txt file2.txt
Example:
gzip data.txt
Example:
gunzip data.txt.gz
Example:
zip myzipfile.zip file1.txt file2.txt
Example:
unzip myzipfile.zip
Conclusion
File handling in UNIX is a foundational skill for both users and system administrators, enabling efficient
interaction with the file system. The set of commands discussed — including creating and displaying files
(cat), copying (cp), deleting (rm), renaming/moving (mv), and paging output (more) — form the basic
operations for managing file content and organization.
Review Question
Review Question
Short Answer Type Questions
1. What is the primary purpose of ordinary file handling in UNIX?
Ans: Ordinary file handling in UNIX provides the fundamental mechanisms for interacting with
regular data files, enabling users and applications to create, read, write, modify, and manage
persistent data stored on the system's storage devices.
2. Explain the basic function of the cat command.
Ans: The cat command is primarily used to display the contents of one or more files to the
standard output. For example, cat myfile.txt will display the text within the 'myfile.txt' file on your
terminal. It can also concatenate files.
3. How do you create an empty file using the cat command?
Ans: You can create an empty file using cat > newfile.txt. This redirects the standard input (which
is empty in this case) to a new file named 'newfile.txt'. If the file exists, its contents will be
overwritten.
4. What is the function of the cp command? Provide an example.
Ans: The cp command is used to copy files or directories. For instance, cp oldfile.txt newfile.txt
creates a duplicate of 'oldfile.txt' named 'newfile.txt' in the current directory.
5. How can you recursively copy a directory and its contents using cp?
Ans: To recursively copy a directory, including all its subdirectories and files, you use the -r (or --
recursive) option with the cp command. For example, cp -r sourcedir
destinationdir will copy the entire 'sourcedir' to 'destinationdir'.
6. What is the purpose of the rm command? Exercise caution while using it.
Ans: The rm command is used to remove (delete) files and directories. Be extremely cautious, as
deleted files are often irrecoverable. For example, rm unwanted.txt will permanently delete the
'unwanted.txt' file.
7. Explain how to remove a directory and its contents using rm?
Ans: To remove a directory and all its contents, including subdirectories and files, you need to use
the -r (recursive) and -f (force) options with the rm command: rm -rf directoryname. Use this
command with extreme caution as it bypasses prompts.
8. What are the two primary functions of the mv command?
Ans: The mv command serves two main purposes: renaming files or directories and moving files
or directories to a different location within the file system. For example, mv oldname.txt
newname.txt renames the file, and mv file.txt /home/user/documents/ moves it to the specified
directory.
9. How does the more command help in viewing file content?
Ans: The more command displays the content of a file page by page, allowing users to read
through large files without the entire content scrolling past quickly. You can navigate forward by
pressing the spacebar and quit by pressing 'q'. Example: more largefile.log.
10. What is the basic function of the lp command?
Netaji Subhash Engineering College
73
Ans: The lp command is used to send files to the printer for printing. For example, lp
document.pdf would typically send the 'document.pdf' file to the default printer configured on
the system.
11. How can you determine the type of a file in UNIX?
Ans: The file command is used to determine the file type based on its content rather than its
extension. For example, file mydocument might output something like "mydocument: ASCII text"
or "mydocument: ELF 64-bit LSB executable, x86-64".
12. Explain the function of the wc command with its basic options.
Ans: The wc (word count) command counts the number of lines, words, and characters in a file.
Common options include -l (lines), -w (words), and -c (characters). For example, wc -l myfile.txt
will only display the number of lines in 'myfile.txt'.
13. What does the cmp command do? How does it indicate differences?
Ans: The cmp command compares two files byte by byte. If differences are found, it reports the
byte and line number where the first difference occurs. If no differences are found, it displays
nothing. Example: cmp file1.txt file2.txt.
14. What is the purpose of the comm command? What assumption does it make about the input
files?
Ans: The comm command compares two sorted files line by line and outputs three columns: lines
unique to the first file, lines unique to the second file, and lines common to both files. It assumes
that the input files are sorted. Example: comm sorted_file1.txt sorted_file2.txt.
15. How does the diff command differ from the cmp command?
Ans: The diff command also compares two files but, unlike cmp, it provides a line-by-line report
of the differences, indicating what needs to be changed in the first file to make it identical to the
second file. Example: diff original.txt modified.txt.
16. What is the primary use case for the tar command?
Ans: The tar (tape archive) command is primarily used for creating archive files, often referred to
as "tarballs," which bundle multiple files and directories into a single file for easier storage,
backup, or distribution. It doesn't inherently compress the data. Example: tar -cvf myarchive.tar
documents/ images/.
17. Explain the function of the gzip command.
Ans: The gzip command is used to compress files, typically reducing their size. It replaces the
original file with a compressed version having a .gz extension. Example: gzip myfile.txt will create
myfile.txt.gz and remove myfile.txt.
18. How do you decompress a file compressed with gzip?
Ans: To decompress a .gz file, you use the gunzip command. For example, gunzip myfile.txt.gz will
decompress it back to myfile.txt. You can also use gzip -d myfile.txt.gz.
19. What is the purpose of the zip command? How does it differ from tar?
Ans: The zip command is used to create archive files in the .zip format, and it also includes built-
in compression. Unlike tar, which primarily archives, zip both archives and compresses files.
Example: zip myarchive.zip documents/ images/.
3. Discuss the implications and potential risks associated with using the rm -rf command. Provide
a scenario where it might be necessary and explain the precautions one should take.
Ans: The rm -rf command is a powerful but dangerous tool in UNIX. The -r option makes it
recursive, deleting directories and their contents, while -f forces the deletion without prompting
for confirmation. The primary risk is accidental and irreversible data loss if the command is
executed on the wrong directory or with a typo.
A scenario where it might be considered necessary is when you need to completely remove a
complex directory structure that contains numerous files and subdirectories, and you are
absolutely certain of the target. Precautions are paramount: double-check the target path,
consider using interactive mode (rm -ri) first to review what will be deleted, and ensure you have
recent backups of important data.
The -r option ensures that the command recursively includes all files and subdirectories within
myproject. project_backup.zip is the name of the resulting compressed archive.
To extract the contents of this zip file to a new directory, first create the directory: mkdir
extracted_project. Then, use the unzip command with the -d option to specify the destination
directory: unzip project_backup.zip -d extracted_project/. This will create
the myproject directory (if it doesn't exist within the zip) inside extracted_project and extract all
the archived files and subdirectories there.
5. Explain how you can use the cat command in conjunction with redirection (> and >>) to create
and append content to files.
Ans: The cat command, when used with redirection operators, allows you to create and modify
files. The > operator redirects the standard output to a file, overwriting the file if it exists or
creating it if it doesn't.
For example, cat > new_config.txt will wait for you to type input, and upon pressing
Ctrl+D, that input will be saved to 'new_config.txt', overwriting any previous content.
The >> operator appends the standard output to an existing file. If the file doesn't exist, it will be
created. For instance, echo "Additional setting" >> existing_config.txt will add the line "Additional
setting" to the end of 'existing_config.txt' without deleting its original content.
6. Discuss the advantages of using archive and compression tools like tar and gzip (or zip) when
managing files in UNIX.
Ans: Archive and compression tools offer several advantages for file management in UNIX.
Archiving with tar bundles multiple files and directories into a single file, simplifying tasks like
backup, distribution, and organization. It maintains directory structures and file permissions.
Compression tools like gzip and the built-in compression of zip reduce the storage space required
for files, which is crucial for efficient disk usage and faster data transfer over networks. Combining
archiving and compression (e.g., using tar with gzip to create .tar.gz files) provides the benefits of
both: a single manageable file that also consumes less disk space, making backups and transfers
more efficient.
7. Explain how you can use the file command to identify different types of files and why this can
be important in a UNIX environment.
Ans: The file command analyzes the content of a file to determine its type, rather than relying
solely on the filename extension. For example, a file named document.txt might actually be a PDF
document, and file document.txt would reveal this. This is important in UNIX because the
operating system heavily relies on the actual content of a file to determine how to handle it.
Executable files, text files, archives, and various data formats are identified by their internal
structure. Knowing the true file type is crucial for executing programs correctly, choosing the right
application to open a file, and ensuring data integrity.
Chapter 04
File attributes
In UNIX, every file and directory is associated with a set of attributes that define its properties and control how it
can be accessed or manipulated. These attributes provide essential information such as file type, permissions,
ownership, size, timestamps, and the number of links. Understanding file attributes is crucial for effective file
management and ensuring system security.
File attributes determine who can read, write, or execute a file, and which user or group owns it. System
administrators and users frequently use commands like ls -l and stat to view these attributes. For example,
permissions prevent unauthorized users from modifying important files, and timestamps help track when files
were last modified or accessed.
ls Command
The ls command is one of the most commonly used commands in UNIX and Linux. It is used to list the contents
of a directory, showing files and subdirectories
The primary command for viewing file and directory attributes is ls. Using different options with ls reveals
various details about these entities. The most common and informative option is -l (lowercase 'L'), which
provides a long listing format.
When you run ls -l, you'll see a line for each file or directory, containing several attributes:
• File Type and Permissions: The first field (e.g., -rw-r--r-- or drwxr-xr-x) indicates the file type (e.g., - for
regular file, d for directory, l for symbolic link) and the read, write, and execute permissions for the
owner, group, and others.
• Number of Hard Links: The second field shows the number of hard links to the file. For a directory, it
indicates the number of subdirectories plus two (for '.' and '..').
• Owner: The third field displays the username of the file's owner.
• Group: The fourth field shows the group name associated with the file.
• Size: The fifth field indicates the size of the file in bytes. For directories, this is the size of the directory
metadata, not the total size of its contents.
• Last Modification Time: The sixth and seventh fields show the date and time when the file's content was
last modified.
• -a: Shows all files, including hidden files (those starting with a '.').
• -h: Displays file sizes in a human-readable format (e.g., 1K, 234M, 2G).
Netaji Subhash Engineering College
79
• -t: Sorts the listing by modification time (newest first).
These attributes provide essential information about the ownership, permissions, size, and modification history
of files and directories, which are crucial for managing and understanding the UNIX file system.
Example Output:
-rwxr--r-- 1 user group 1234 Mar 18 12:00 myfile.txt
• -rwxr--r-- → Permissions
o - → File type (- for file, d for directory)
o rwx → Owner (User) has read, write, and execute permissions
o r-- → Group has only read permission
• Example:
chown alice myfile.txt
• Example:
chgrp staff myfile.txt
Special Permissions
Beyond standard permissions, UNIX provides special permission bits:
1. Set User ID (SUID)
• Allows a program to run as the file owner, not the user executing it.
• Set using:
chmod u+s filename
3. Sticky Bit
• Prevents deletion of files in a directory by users other than the owner.
• Commonly used in /tmp:
chmod +t /tmp
Conclusion
Managing file security in UNIX is essential for protecting data and ensuring only authorized access.
Understanding permissions (chmod), ownership (chown, chgrp), and default permissions (umask)
helps in securing files and directories efficiently. Advanced techniques like SUID, SGID, and Sticky Bit
provide further control over access rights.
1. Understanding Inodes
An inode (Index Node) is a data structure used by the UNIX file system to store metadata about a file.
Each file has a unique inode number that acts as an identifier.
1.1 Information Stored in an Inode
An inode contains:
• File type (regular file, directory, symbolic link, etc.)
• Permissions (read, write, execute)
• Owner (User ID - UID)
• Group (Group ID - GID)
• File size
• Timestamps (creation, modification, access)
• Number of hard links
• Pointers to data blocks (location of file content on disk)
Example:
ls -i myfile.txt
123456 myfile.txt
This output shows that myfile.txt has inode number 123456.
To view detailed inode information:
stat myfile.txt
2. Hard Links
A hard link is another name for a file that shares the same inode number. It points directly to the
original file's inode and data blocks.
Example:
ln myfile.txt myfile_hardlink.txt
Example output:
123456 -rw-r--r-- 2 user group 1024 Mar 18 12:00 myfile.txt
Both files have the same inode number (123456), confirming they are hard links.
2.4 Deleting a Hard Link
If one file is deleted:
rm myfile.txt
The data is still accessible via myfile_hardlink.txt. Only when all hard links are removed does the data
get erased.
Example:
ln -s myfile.txt myfile_symlink.txt
Example output:
123456 -rw-r--r-- 1 user group 1024 Mar 18 12:00 myfile.txt
Now, myfile_symlink.txt exists but doesn’t work. Listing its contents will show:
ls: cannot access 'myfile_symlink.txt': No such file or directory
Effect if Original File is Deleted File remains intact Link becomes broken
Storage Space Shares existing file’s storage Requires additional space for the link
Using stat
Sample output:
File: file.txt
Size: 2048 Blocks: 8 IO Block: 4096 regular file
Access: 2025-05-19 10:00:00
Modify: 2025-05-18 21:30:00
Change: 2025-05-18 21:35:00
Examples:
a. Create a new empty file:
touch newfile.txt
Common Examples:
a. Find all .txt files in current directory:
find . -name "*.txt"
Conclusion
Understanding inodes and linking mechanisms is essential for efficient file management in UNIX.
• Hard links create another reference to a file using the same inode.
• Soft links create a new file that points to the original file's path.
Each has unique advantages and use cases, making them powerful tools in UNIX file
management.
• The stat and ls -l commands are useful to inspect file access, modification, and metadata
change times.
• The touch command is versatile for creating empty files and modifying file timestamps.
• The find command is a powerful utility to search and manage files efficiently based on various
criteria such as name, type, access/modification time, and permissions.
Netaji Subhash Engineering College
90
Review Question
Review Questions
Short Answer Type Questions
28. What is the meaning of the first character in the output of ls -l?
Ans: The first character in the ls -l output indicates the file type: "-" for regular file, "d" for directory, "l"
for symbolic link, "c" for character special file, and "b" for block special file.
1. Explain file attributes in Unix, listing and briefly describing at least five common attributes.
Ans: In Unix, file attributes are metadata that describe and control access to a file. They are stored in the
file's inode and provide essential information beyond the file's content. Here are five common
attributes:
o Permissions: These determine who can access the file and how (read, write, execute).
Represented as three sets of three characters (e.g., rwxr-xr--).
o Owner: The user ID of the file's owner, who typically has the most control over the file. The
chown command is used to change ownership.
o Group: The group ID associated with the file, allowing group members to share access based on
group permissions. The chgrp command changes group ownership.
o Size: The amount of data the file contains, usually measured in bytes. The ls -l command displays
the file size.
o Timestamps: Include the last access time (when the file was last read), last modification time
(when the file's content was changed), and last status change time (when the inode was last
modified). The stat and touch commands are relevant here.
o Access Control: The owner has primary control over the file's permissions, determining who else
can read, write, or execute it.
o Privileges: The owner can modify the file's attributes, including permissions and group
ownership.
o Accountability: Ownership provides a way to track which user is responsible for a particular file,
aiding in system administration and security.
For example, if user alice owns a file data.txt, she can use chmod to restrict access to only herself,
ensuring her data's privacy.
o Read (r):
▪ For files: Allows a user to view the file's contents (e.g., using cat).
▪ For directories: Allows a user to list the files and subdirectories within it (e.g., using ls).
o Write (w):
▪ For files: Allows a user to modify the file's contents (e.g., using a text editor).
▪ For directories: Allows a user to create new files or delete existing ones within the
directory.
o Execute (x):
▪ For directories: Allows a user to enter the directory using the cd command, granting
access to its contents and subdirectories.
Permissions are applied to three categories: user (owner), group, and others. For instance, chmod 755
mydir gives the owner full permissions, while the group and others can only read and execute the
directory.
4. Describe the process of changing file permissions in Unix, explaining both relative and absolute
permission methods with examples.
Ans: The chmod command changes file permissions.
o Relative Permissions: Use symbolic operators (+, -, =) and letters (r, w, x, u, g, o).
o Absolute Permissions: Use octal numbers. Each digit represents permissions for user, group, and
others.
▪ chmod 755 script.sh: Owner: rwx (7), Group: r-x (5), Others: r-x (5).
▪ chmod 644 data.txt: Owner: rw- (6), Group: r-- (4), Others: r-- (4).
5. Explain how to change file ownership and group ownership in Unix, providing examples using the
chown and chgrp commands.
Ans: Changing File Ownership (chown): The chown command changes the user who owns a file. You
must be the superuser (root) or the current owner of the file to change its ownership.
▪ chown root:staff /var/log/messages: Changes the owner to root and the group to staff.
Changing Group Ownership (chgrp): The chgrp command changes the group associated with a file. You
must be the superuser or a member of the new group and the file's owner.
▪ chgrp -R www-data /var/www/html/: Changes the group of all files and directories
recursively in /var/www/html/ to www-data.
6. Describe the Unix file system and the role of inodes in organizing files.
Ans: The Unix file system is a hierarchical structure, resembling an inverted tree, with the root directory
(/) at the top. It organizes files and directories in a structured manner, allowing for efficient storage and
retrieval.
o An inode is a data structure that stores all the metadata about a file, except its name and data.
This metadata includes:
▪ Permissions
▪ Size
▪ File type
o Directories contain entries that map file names to inode numbers. When you access a file by its
name, the system looks up the inode number in the directory, then retrieves the file's attributes
from the inode.
o This separation of metadata (inode) from the filename allows for features like hard links, where
multiple names can point to the same inode (and thus, the same data).
7. Explain the concepts of hard links and soft links (symbolic links) in Unix, highlighting their differences
with examples.
Ans: Both hard and soft links create additional ways to access a file, but they function differently:
o Hard Links:
▪ A hard link is a directory entry that points directly to the same inode as the original file.
It's like giving the same file another name.
▪ If you change the contents of the file through one hard link, the changes are visible
through all other hard links.
▪ Hard links share the same inode, so they have the same attributes (permissions, owner,
etc.).
Example:
▪ A soft link is a special file that contains a pointer to the name of another file or directory.
It's more like a shortcut.
▪ If the original file is deleted, the soft link becomes broken (dangling), as it points to a
name that no longer exists.
▪ Soft links can point to directories and files on different file systems.
▪ Soft links have their own inode, which stores the path to the original file.
Example:
▪ rm original.txt
▪ ls -l shows soft links with an "l" at the beginning of the permissions and displays the link
pointing to the original file.
o Read (r): Allows users to list the files and subdirectories within the directory using commands
like ls. Without read permission, you cannot see what's inside the directory.
o Write (w): Allows users to create new files or delete existing files within the directory. Note that
having write permission on a directory does not mean you can modify the contents of the files
within that directory, unless you also have write permission on those individual files.
o Execute (x): Allows users to enter the directory using the cd command. This is essential for
accessing files and subdirectories within the directory, even if you have read permission. Execute
permission on a directory is often referred to as "traverse" permission.
For example, if a directory has permissions rwxr-x---, the owner can list, create/delete, and enter it; the
group can list and enter it; and others cannot access it at all. Without execute permission on a directory,
you cannot cd into it, regardless of read permissions.
o Default Permissions:
o umask:
▪ umask sets the permissions that are removed from the default permissions. It's a mask
of bits that are turned off.
Example:
▪ Default file permission (666) - umask (022) = Actual file permission (644, rw-r--r--)
▪ Default directory permission (777) - umask (022) = Actual directory permission (755,
rwxr-xr-x)
Commands:
▪ umask 022
▪ touch newfile.txt
▪ mkdir newdir
10. Describe how to list and change file modification and access times in Unix, including the use of the ls,
stat, and touch commands.
Unix maintains several timestamps for files:
o Modification Time (mtime): Time the file's content was last changed.
o Change Time (ctime): Time the file's metadata (permissions, ownership) was last changed.
o Listing Times:
▪ touch filename: Updates both atime and mtime to the current time. If the file doesn't exist,
it creates an empty file.
touch myfile.txt # Updates atime and mtime
▪ touch -t YYYYMMDDHHMM filename: Sets both atime and mtime to a specific time.
touch -t 202407241200 myfile.txt # Sets time to July 24,
2024, 12:00 PM
Chapter 05
Shell
A shell is a command-line interpreter that allows users to interact with the operating system. It provides an
interface between the user and the system's kernel, enabling users to execute commands, run scripts, and
automate tasks.
• File and Process Management: Allows users to navigate files, execute processes, and manage resources.
UNIX offers multiple shell types, each with unique features. These shells can be categorized into Bourne Shell
Family and C Shell Family.
Shells in this family are descendants of the original Bourne Shell (sh), known for their scripting capabilities.
Shell Type Description
Bourne Shell (sh) Original UNIX shell, known for scripting but lacks user-friendly features.
Bash (Bourne Again Default shell in Linux, an enhanced version of sh, supports command history, auto-completion,
Shell) and scripting improvements.
Korn Shell (ksh) Developed by David Korn, adds scripting enhancements and performance improvements.
Z Shell (zsh) Combines features of bash and ksh, supports advanced auto-completion, themes, and plugins.
C Shell (csh) Introduced by BSD, supports command aliasing, job control, and a C-like syntax.
TENEX C Shell Enhanced version of csh, includes command-line editing, history, and better scripting
(tcsh) capabilities.
Conclusion
A shell is a crucial part of the UNIX/Linux operating system, enabling users to execute commands and automate
tasks. Different shell types offer unique features, with bash being the most commonly used, while zsh and ksh
provide additional enhancements. Understanding shell differences helps in selecting the right shell for specific
needs.
Pattern matching is a technique used in text processing to search for specific sequences of characters (patterns)
in a file or input stream. In UNIX, pattern matching is commonly performed using regular expressions with tools
like grep and egrep.
A Regular Expression (RegEx) is a pattern that defines a search criteria. It is used for string matching and text
filtering in UNIX commands.
grep (Global Regular Expression Print) is used to search for a specific pattern in files or input streams.
Basic Syntax:
grep [OPTIONS] "pattern" filename
Examples of grep
2. Case-insensitive search
grep -i "error" logfile.txt
egrep (Extended grep) supports Extended Regular Expressions (ERE), which include advanced patterns. It is
functionally similar to grep -E.
Syntax of egrep
egrep [OPTIONS] "pattern" filename
Examples of egrep
1. Anchors (^ and $)
Netaji Subhash Engineering College
105
• ^pattern → Match at the beginning of a line.
Example:
grep "^Hello" file.txt # Lines starting with "Hello"
grep "end$" file.txt # Lines ending with "end"
Symbol Meaning
. Matches any single character
[abc] Matches any one of a, b, or c
[^abc] Matches any character except a, b, or c
[a-z] Matches any lowercase letter
[0-9] Matches any digit
Example:
egrep "gr.y" file.txt # Matches "gray", "grey", etc.
egrep "[A-Z]" file.txt # Matches any uppercase letter
Symbol Meaning
* Matches 0 or more times
+ Matches 1 or more times
? Matches 0 or 1 time
{n} Matches exactly n times
{n,} Matches n or more times
{n,m} Matches between n and m times
Example:
egrep "go*d" file.txt # Matches "gd", "god", "good", "goood"
egrep "go+d" file.txt # Matches "god", "good", "goood" but not "gd"
egrep "colou?r" file.txt # Matches "color" and "colour"
Conclusion
Pattern matching using grep and egrep is essential for text processing in UNIX. While grep supports basic regular
expressions, egrep provides extended pattern matching capabilities. These tools help in searching, filtering, and
extracting information efficiently from text files.
In Unix/Linux systems, redirection is a powerful feature that allows users to change the standard input and output
sources of commands. By default, programs read input from the keyboard (standard input) and display output on
the screen (standard output). However, with redirection, you can reroute input from files and send output to files
or other devices instead of the screen. This enables users to save command results, read data from files, manage
error messages, and automate tasks more efficiently. Redirection is an essential tool for shell scripting,
programming, and system administration.
Redirection:
Redirection is the process of changing the default input or output source of a command in a Unix/Linux shell. It
allows a command to read input from a file instead of the keyboard (standard input), or write output to a file
instead of displaying it on the screen (standard output). Redirection also includes managing error messages by
sending them to files or combining them with standard output. It is commonly used to automate tasks, store
results, and streamline command-line workflows.
• Standard Input (stdin): Input taken from the keyboard by default (file descriptor 0).
• Standard Output (stdout): Output displayed on the screen by default (file descriptor 1).
• Standard Error (stderr): Error messages displayed on the screen (file descriptor 2).
Streams allow programs to process data efficiently, whether it's coming from the user, another program, or a file.
These streams are automatically opened when a program starts and are identified by file descriptors. A file
descriptor is a non-negative integer that uniquely identifies an open file or input/output resource used by a
process. It acts as a handle for accessing files, devices, sockets, or streams.
1. Standard Input(stdin):
Standard Input, commonly referred to as stdin, is one of the three default data streams used in Unix/Linux
systems for handling input and output.
Standard Input (stdin) is the default stream through which a program receives input data. It is identified by
the file descriptor 0. By default, input is taken from the keyboard unless redirected from a file or another
source.
o By default, stdin is connected to your keyboard. When a command expects input (like read, or when
you run sort without specifying a file), it's typically reading from what you type.
Example:
cat # The 'cat' command, without arguments, reads from stdin
This is what I'm typing.
^D # Press Ctrl+D (or Cmd+D on macOS) to signal the end of input
This is what I'm typing.
In this example, the cat command reads the lines you typed on the keyboard (stdin) and then echoes
them back to the terminal (stdout). The ^D signifies the "end of file" marker for stdin, telling cat that
there's no more input.
By default, when you run a command that expects input and you don't specify a source, it waits for you to
type something on your keyboard and press Enter. Each line you enter becomes part of the standard input for
that command.
2. Standard Output(stdout):
Standard Output, commonly called stdout, is one of the three primary data streams in Unix/Linux systems
used for communication between a program and the outside world.
Standard Output (stdout) is the default stream where a program writes its regular output data. It is identified
by the file descriptor 1. By default, stdout sends output to the terminal (screen).
o By default, stdout is connected to your terminal screen. When a command successfully completes its
task and produces output (like the listing from ls or the sorted output from sort), it's usually sent to
stdout, which then displays it on your screen.
o Stdout can be redirected using the > (overwrite) or >> (append) operators to save the output to a file
instead of displaying it on the screen.
o Stdout is a write-only stream for the command. It can only send data.
Example:
ls -l
total 4
-rw-r--r-- 1 user user 12 May 13 02:35 unsorted_list.txt
-rw-r--r-- 1 user user 25 May 13 02:37 sorted_list.txt
The ls -l command lists files and directories in a long format. Its standard output is directly displayed on
your terminal.
3. Standard Error(stderr):
Standard Error, abbreviated as stderr, is one of the three default data streams in Unix/Linux systems, used
specifically for handling error messages and diagnostics.
Standard Error (stderr) is the default stream where a program writes its error messages. It is identified by the
file descriptor 2. By default, stderr sends output to the terminal (screen), just like stdout, but it operates
independently.
o By default, stderr is also connected to your terminal screen. This ensures that even if the regular output
is redirected to a file, you'll still see any error messages on your screen, alerting you to potential
problems.
o Stderr can be redirected using the 2> (overwrite) or 2>> (append) operators to save error messages to
a separate file. This is crucial for debugging and monitoring automated processes.
o Like stdout, stderr is a write-only stream for the command.
Example:
cat non_existent_file.txt
You won't see the error message on your terminal. Instead, it is redirected to a file named error_log.txt.
The content of error_log.txt will be:
cat: non_existent_file.txt:
This will put both the normal output from existing_file.txt and the error message about
non_existent_file.txt into the combined_log.txt file.
Here, > redirects stdout to output_and_errors.log, and then 2>&1 redirects stderr to the
same location where stdout is currently going (which is now the file). The order is important here.
• <<< (Here String): This allows you to provide a single line of input to a command.
This is equivalent to echo "This is a single line of input." | command, but without the explicit pipe.
• Separation of Concerns: Keeping normal output and error messages separate makes it easier to process
the intended results of a command without being cluttered by error messages.
• Automation and Scripting: In scripts, you might want to save the regular output of a command to a file
for further processing while logging any errors to a separate file for debugging.
• Piping: When you use pipes (|) to connect the output of one command to the input of another, you
typically only want to pipe the standard output, not the error messages. Errors should usually be displayed
or logged separately.
These are essential concepts in shell scripting that help you manage special characters and execute commands
within other commands.
Escaping: Escaping is a way to remove the special meaning of a character and treat it literally. The backslash \ is
used as the escape character.
Examples:
o echo "The variable is \$VAR": \$ prevents the shell from expanding the variable VAR.
Quoting: Quoting is a way to group characters and control how the shell interprets them.
Types:
Purpose:
Examples:
echo 'This is a literal string with $VAR' : The variable $VAR is not
expanded; it is taken literally.
echo "This string expands $VAR": The variable $VAR is expanded to its
value.
echo "Files in current directory: *": The * will expand to the list of
files.
myvar="hello world"
echo "The value of myvar is $myvar" #output: The value of myvar is hello
world
echo 'The value of myvar is $myvar' #output: The value of myvar is $myvar
Command Substitution: Command substitution allows you to execute a command and insert its output into
another command or string.
Syntax:
To use the output of one command as an argument to another command, or to assign it to a variable.
o find . -name "$(ls | grep .txt)" : finds all the files with .txt
extension in the current directory.
PIPE
In Unix/Linux systems, a pipe is a command-line feature that allows the output of one command to be passed
directly as input to another command. Represented by the symbol |, pipes enable users to chain multiple
commands together, creating a seamless flow of data between them. This mechanism helps avoid the need for
temporary files, making command execution more efficient and script-friendly. Pipes are especially useful for
filtering, transforming, and analyzing data using standard utilities like grep, sort, awk, uniq, and wc. By
combining simple tools with pipes, users can solve complex tasks in a clean, modular way.
• A pipe (|) is a shell operator that connects the standard output (stdout) of one command to the standard
input (stdin) of another. It is used to pass data between commands without creating intermediate files.
Pipes (|) allow you to connect the standard output of one command directly to the standard input of another
command. This creates a powerful way to chain commands together to perform complex operations in a
sequential manner.
Syntax:
The output of command1 becomes the input of command2, the output of command2 becomes the
input of command3, and so on. The data flows from left to right through the pipeline.
Example:
Let's say you want to find all files in the current directory that contain the word "report". You can achieve this by
combining the ls command with the grep command using a pipe:
1. ls -l: Lists files and directories in a long format (output goes to stdout).
2. grep "report": Reads input from stdin and prints lines containing "report" to stdout.
ls -l | grep "report"
Another example:
Let's say you want to count the number of lines in the names.txt file:
2. wc -l: Reads input from stdin and counts the number of lines (output to stdout).
cat names.txt | wc -l
The content of names.txt is piped to wc -l, which then counts the lines and prints the count to your terminal.
• Efficiency: Pipes avoid the need to create intermediate files to store the output of one command before
using it as input for another. The data flows directly in memory.
• Flexibility: You can combine simple, specialized commands to perform complex tasks that no single
command can achieve.
• Readability: Well-constructed pipelines can be more readable and easier to understand than long,
complex commands with multiple options.
• Understand the Input and Output of Commands: Before using redirection or pipes, make sure you
understand what a command expects as input and what it produces as output (both stdout and stderr).
Consult the command's manual page (man command) for details.
• Filter and Process Data Incrementally: Pipes allow you to process data step by step. Use commands like
grep, sed, awk, sort, uniq, etc., in your pipelines to filter, transform, and analyze data effectively.
• Handle Errors: Be mindful of standard error. If a command in a pipeline fails, the error messages will
usually still go to your terminal. If you need to capture or redirect errors from a pipeline, you might need
to redirect stderr explicitly at the end of the pipeline or use tools designed for more complex error
handling.
• Keep Pipelines Concise and Readable: While you can create very long pipelines, it's often better to break
down complex tasks into smaller, more manageable steps. Use temporary variables or intermediate files
if a pipeline becomes too convoluted.
• Use Redirection for Saving and Organizing Output: Use output redirection to save the results of
commands to files for later analysis, reporting, or configuration.
• Combine Redirection and Pipes: You can use both redirection and pipes in the same command. For
example, you can pipe the output of several commands to a file:
In summary, I/O redirection and pipes are powerful tools that allow you to control the flow of data in your
command-line environment. By understanding how they work and using them effectively, you can significantly
enhance your productivity and automate various tasks with ease.
Key Concepts
• Variable Assignment: You assign a value to a variable using the following syntax:
• Variable Naming:
o Variable names can contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
o By convention, shell variables are often written in uppercase (e.g., MY_VAR, HOME). This helps
distinguish them from shell commands and scripts.
• Accessing Variable Values: To retrieve the value stored in a variable, you precede the variable name with
a dollar sign ($):
• echo $variable_name
For example:
NAME="John Doe"
Types of Variables
1. Local Variables: These variables are defined within the current shell instance. They are only accessible
within the script or the current shell session where they are defined. Example:
my_var="This is local"
2. Environment Variables: These variables are available to the current shell and any child processes that it
creates. They are used to store information about the system, the user, and the shell environment. You
can make a local variable an environment variable using the export command:
export my_var
▪ PATH: A list of directories where the shell searches for executable programs.
3. Shell Variables (Special Variables): These are special variables that are set by the shell itself. They provide
information about the shell's state and behaviour. Examples:
Examples
#!/bin/bash
# Local variable
message="Hello, world!"
echo $message
# Environment variable
export GREETING="Welcome"
echo $GREETING
# Shell variables
Key Points
• Variables are a powerful tool for storing and manipulating data in shell scripts.
• Understanding the difference between local, environment, and shell variables is crucial for writing correct
and portable scripts.
• Environment variables play a vital role in configuring the behavior of programs and the shell itself.
Review Question
Review Questions
Short Answer Type Questions
1. What is a shell in Unix?
Ans: A shell is a command-line interpreter that provides an interface between the user and the
operating system, allowing users to execute commands and manage system operations. It interprets user
input and translates it into instructions for the kernel.
For example, if you type ls -l *.txt, the shell first displays the prompt (e.g., $). It then expands *.txt to a list
of matching files (e.g., file1.txt file2.txt). The shell then executes ls -l file1.txt file2.txt, shows the output,
and returns to the prompt.
2. Explain the differences between the Bourne shell (sh), the Bourne Again SHell (bash), and the C Shell
(csh).
Ans: The Bourne shell (sh) was one of the earliest Unix shells, known for its simplicity and efficiency. It
lacks some interactive features. The Bourne Again SHell (bash) is an enhanced version of sh, widely used
in Linux, offering more features like command-line editing, history, and scripting capabilities. The C Shell
(csh) was developed at Berkeley and provides features like aliases and job control, with a syntax
resembling the C programming language. Bash is generally preferred for its compatibility and rich feature
set.
3. How does pattern matching with wildcards simplify file manipulation in the shell? Provide examples.
For example, to delete all files ending with .log, you can use rm *.log. The * matches any sequence of
characters before .log. To list all single-character files starting with 'a', use ls a?; the ? matches any single
character. This makes working with multiple files much more efficient.
4. Describe the importance of escaping and quoting in shell programming, and illustrate with examples.
Ans: Escaping and quoting are crucial for controlling how the shell interprets special characters. Escaping
(using \) treats the next character literally, preventing its special meaning. Quoting (using single or double
quotes) suppresses or partially suppresses the special meaning of characters within the quotes.
For example, to search for the string $100 in a file, grep \$100 myfile.txt escapes the $ to prevent variable
substitution. To prevent any interpretation, use single quotes: echo '$PATH' prints $PATH literally. Double
quotes allow variable expansion: echo "The value of PATH is $PATH".
5. Explain the concept of redirection in the shell, including standard input, standard output, and
standard error.
Ans: Redirection allows you to change the default sources and destinations of data streams. Standard
input (stdin) is the source of data for a command (usually the keyboard). Standard output (stdout) is where
a command's normal output is sent (usually the terminal). Standard error (stderr) is where a command's
error messages are sent (also usually the terminal).
For example, ls > filelist.txt redirects the stdout of ls to filelist.txt, creating a file with the directory listing.
grep "error" logfile.txt 2> errorlog.txt redirects stderr to errorlog.txt, capturing errors. cat < input.txt reads
stdin from input.txt.
/dev/tty represents the user's terminal. It's used to ensure that output is displayed directly on the user's
terminal, even if standard output has been redirected. For example, a script might use echo "Enter input:"
> /dev/tty to prompt the user for input, regardless of where stdout is redirected.
7. Explain how pipes (|) and the tee command can be used to create powerful command chains in Unix.
Ans: Pipes connect the stdout of one command to the stdin of another, allowing you to process data
through a series of commands. The tee command allows you to split the output, sending it to both the
terminal and a file.
For example, ls -l | grep "^d" | wc -l lists directories, filters them with grep, and counts the number of lines
(directories). command | tee output.txt displays the output on the terminal and saves it to output.txt.
For example, echo "The current date is $(date)" substitutes the output of the date command into the
string. Another example: files=$(ls *.txt); echo "Text files: $files" first stores the list of text files in the files
variable, then prints the variable.
For example, a script might use read -p "Enter your name: " name to get user input and store it in the
name variable. Then, it could use echo "Hello, $name!" to greet the user. Variables are essential for
creating dynamic and interactive scripts.
10. Write a short shell script that demonstrates several concepts discussed, including variables,
redirection, and pipes.
#!/bin/bash
# Set a variable
LOG_FILE="script_log.txt"
Chapter 06
Process
A process in UNIX is an instance of a computer program that is being executed. Think of a program as a blueprint
and a process as the actual house built from that blueprint, with people (data) moving in and out and activities
(instructions) happening.
Key concepts:
• Program vs. Process: A program is a static file containing instructions. A process is a dynamic entity,
actively running those instructions.
• Address Space: Each process gets its own private virtual address space. This isolated memory region
prevents one process from interfering with the memory of another, contributing to system stability. This
address space typically includes sections for the program code (text), data (initialized and uninitialized),
stack (for function calls and local variables), and heap (for dynamic memory allocation).
• Resources: When a process is created, the operating system allocates various resources to it, such as
memory, file descriptors (for accessing files and network connections), CPU time, and more.
• Process Control Block (PCB): The operating system maintains a data structure for each active process,
called the Process Control Block. This PCB stores crucial information about the process, including its
current state, priority, memory allocation, open files, and the program counter (indicating the next
instruction to be executed).
The ps (process status) command is your window into the currently running processes on the system. It provides
a snapshot of these processes and their attributes.
ps
This typically shows information about the processes associated with the current terminal.
o u: Displays detailed information about each process, including the user who owns it.
o -f: Provides a full listing, showing more details like the parent process ID (PPID).
• PID (Process ID): A unique numerical identifier assigned to each process by the kernel.
• CPU (%CPU): The percentage of the processor time that the process is currently using.
• MEM (%MEM): The percentage of the system's physical memory that the process is using.
• VSZ (Virtual Size): The total amount of virtual memory used by the process.
• RSS (Resident Set Size): The amount of physical RAM occupied by the process.
• TTY: The controlling terminal associated with the process (if any). A ? usually indicates no controlling
terminal.
• STAT (State): A code indicating the current state of the process (more on this later).
• TIME: The total amount of CPU time the process has accumulated.
You can also use options to filter the output based on specific criteria, like user (-u), process ID (-p), or command
name (-C). For example:
ps -u your_username
ps -p 1234
ps -C firefox
UNIX systems run numerous background processes essential for their operation. These are often called daemons.
They perform tasks like managing network connections, handling printing, scheduling jobs, and more.
The ps aux or ps -ef commands, as mentioned earlier, are crucial for displaying these system processes. You'll
often see users like root, systemd, syslog, etc., associated with these processes. These processes typically don't
have a controlling terminal (their TTY will be ?).
Tools like top and htop provide a dynamic, real-time view of system processes and resource utilization, making it
easier to monitor system activity.
The creation of a new process in UNIX typically involves the following steps:
1. Forking: A running process (the parent process) makes a copy of itself using the fork() system call. This
creates a new process (the child process) that is almost an exact duplicate of the parent, including its
memory space, open files, and current execution point. The key difference is that the parent process
receives the PID of the newly created child, while the child process receives a return value of 0 from the
fork() call.
3. Waiting (Optional): The parent process might need to wait for the child process to complete its execution.
This is done using the wait() or waitpid() system calls. These calls allow the parent to be notified
when a child process terminates and to retrieve its exit status.
This fork-exec model is fundamental to how new programs are started in UNIX. For instance, when you type a
command in your shell and press Enter, the shell forks a child process, and then the child process uses exec() to
run the command you entered.
Shell Creation Steps (init -> getty -> login -> shell)
The process of getting a login shell when you boot up a UNIX-like system involves a sequence of process creations:
1. init (PID 1): The init process is the very first process started by the kernel during the boot sequence. It's
the ancestor of all other processes on the system. Its primary role is to initialize the system and start other
essential services.
2. getty (or agetty, mingetty): init starts one or more getty (or similar) processes. These processes are
responsible for managing terminal lines (both physical and virtual). A getty process:
3. login: When you enter your username at the login prompt, the getty process executes the login program.
The login program:
o Authenticates your username and password against the system's user database (e.g.,
/etc/passwd, /etc/shadow).
o Finally, it executes the user's default shell (usually specified in /etc/passwd, like bash, zsh, sh).
This sequence ensures that you get an interactive shell session after the system boots up and you log in.
Process State
The STAT column in the output of ps indicates the current state of a process. Here are some common process
states:
• R (Running): The process is currently running on the CPU or is ready to run and waiting to be assigned to
a CPU.
• S (Sleeping): The process is waiting for an event to occur, such as the completion of an I/O operation or a
signal. It's in a wait queue and not consuming CPU time.
• D (Disk Sleep) or Uninterruptible Sleep: The process is waiting for a disk I/O operation to complete. This
state is often uninterruptible, meaning it cannot be woken up by signals.
• T (Stopped): The process has been stopped, usually by a signal (e.g., SIGSTOP or SIGTSTP) from the user
or the system. It can be resumed later with a SIGCONT signal.
• Z (Zombie): The process has terminated, but its entry in the process table still exists. This happens
because the parent process has not yet acknowledged the child's termination (by calling wait() or
waitpid()). Zombie processes consume minimal resources but should eventually be reaped by their
parent to free up their PID.
• I (Idle): (Less common in standard ps output, often seen in kernel-level process listings) The process is
idle or waiting for a more significant event.
• +: A trailing + in the STAT column often indicates that the process is in the foreground process group of
its controlling terminal.
• <: A preceding < indicates a high-priority process (nice value < 0).
Zombie State
As mentioned above, a zombie process (also known as a defunct process) is a process that has completed its
execution but whose entry in the process table has not yet been removed by its parent process.
When a child process terminates, the kernel sends a SIGCHLD signal to its parent. The parent process is then
expected to call a wait() or waitpid() system call to retrieve the child's exit status and clean up its entry in the
process table.
If the parent process doesn't call wait() (perhaps it has terminated itself or is poorly written), the zombie process
will remain in the system until the parent process terminates or the system is rebooted. While zombies don't
consume much in terms of CPU or memory, a large number of zombie processes can exhaust the system's process
ID limit.
You can identify zombie processes in the output of ps by the Z state in the STAT column. The <defunct> tag might
also appear in the COMMAND column.
In a UNIX shell, you can run commands in the background so that they don't tie up your terminal. To do this, you
append an ampersand (&) to the end of the command:
long_running_command &
• The shell immediately returns to the prompt, allowing you to continue working.
• Output from the background process might still appear on your terminal.
However, there's a potential issue: if you close your terminal, any background jobs started from that terminal
will typically receive a SIGHUP (hang up) signal, which usually causes them to terminate.
• If the command produces any output that would normally go to the terminal, nohup redirects it to a file
named nohup.out in the current directory (or the user's home directory if the current directory is not
writable).
nohup is particularly useful for long-running tasks that you want to continue even after you log out.
The nice command allows you to adjust the priority of a process. In UNIX, each process has a nice value, which
ranges from -20 (highest priority) to 19 (lowest priority). The default nice value is typically 0.
A lower nice value gives the process a higher scheduling priority, meaning it gets more CPU time relative to other
processes. Conversely, a higher nice value gives the process a lower priority, making it "nicer" to other processes
by consuming less CPU time.
You can also use a positive integer without -n. For example, nice 5 command is equivalent to nice -n 5 command.
To change the nice value of an already running process, you can use the renice command. You need the process
ID (PID) of the process you want to renice:
renice 12 -p 1234 # Set the nice value of process with PID 1234 to 12
Signals are asynchronous notifications sent to a process by the operating system or other processes to indicate
that a particular event has occurred. Signals can be used to control the behavior of processes, including
terminating them.
The kill command is used to send signals to processes. The basic syntax is:
kill [options] <PID> [...]
The most commonly used signal is SIGTERM (signal number 15), which politely asks a process to terminate. Most
well-behaved processes will catch this signal and perform any necessary cleanup before exiting.
kill 1234 # Sends SIGTERM to process with PID 1234
If a process doesn't terminate after receiving SIGTERM, you can use the SIGKILL signal (signal number 9), which
forcefully terminates the process immediately. This signal cannot be caught or ignored by the process.
kill -9 1234
kill -KILL 1234
Caution: Using SIGKILL should be a last resort, as it doesn't give the process a chance to clean up, which can
potentially lead to data loss or system inconsistencies.
• SIGHUP (1): Hangup signal, often sent when the controlling terminal is closed.
• SIGTSTP (20): Terminal stop signal, usually generated by pressing Ctrl+Z, which suspends the process.
When you suspend a running job in the shell (e.g., using Ctrl+Z, which sends SIGTSTP), it stops executing and is
placed in the background. You can then use the bg command to resume a suspended job in the background:
bg %job_id
Here, %job_id is a job identifier. If you have only one suspended job, you can often just use %. You can see the
job IDs using the jobs command.
To bring a background job back to the foreground, where it will have control of your terminal, you use the fg
command:
fg %job_id
Again, if there's only one background job, you can often just use fg.
• Job ID: A number (e.g., [1], [2]) used to refer to the job.
• Status: The current state of the job (e.g., Running, Stopped, Done).
Example output:
[1] + Running long_running_command &
[2] - Stopped another_command
In this example, long_running_command is running in the background (job ID 1), and another_command is
stopped (job ID 2). The + indicates the current default job (often the most recently stopped or started
background job), and - indicates the previous default job.
Suspend Job
As mentioned earlier, you can suspend a foreground job by pressing Ctrl+Z. This sends the SIGTSTP signal to the
process, causing it to stop execution and return control to the shell. The job then becomes a stopped background
job, which you can later resume with bg or bring to the foreground with fg.
Kill a Job
You can kill a background job using the kill command along with the job ID. Remember to prefix the job ID with a
percent sign (%):
kill %1 # Sends SIGTERM to job with ID 1
kill -9 %2 # Sends SIGKILL to job with ID 2
The at and batch commands allow you to schedule commands to be executed at a later time.
at: The at command lets you specify an exact time and date for the command to be executed.
at 8:00 tomorrow
This will prompt you to enter the command(s) you want to run at 8:00 AM the next day. Press Ctrl+D to indicate
the end of the commands.
• now + 5 minutes
• 10:30 PM
• noon
• next Monday
To view your pending at jobs, use atq. To delete a job, use atrm <job_number>, where <job_number> is the
number listed by atq.
batch: The batch command allows you to schedule jobs to be executed when the system load average falls below
a certain threshold. This is useful for less urgent tasks that you want to run when the system is less busy.
To submit a job for batch execution, you can pipe commands to the batch command:
./long_analysis_script.sh | batch
The system will then execute these commands when the load average is sufficiently low. You can view pending
batch jobs with atq (they might be listed with a different queue identifier) and remove them with atrm.
These commands (at and batch) are powerful tools for automating tasks and managing system resources
efficiently.
Review Question
Review Questions
Short Answer Type Questions
4. What is the fundamental system call used by a parent process to create a new child process in UNIX?
Briefly explain its outcome.
Answer: The fork() system call is used. Upon successful execution, it creates a new process (the child)
that is a nearly identical copy of the parent. The parent receives the child's PID, while the child receives a
return value of 0.
5. After a fork() call, what system call is often used by the child process to execute a different program?
Answer: The exec() family of system calls (e.g., execve, execlp) is used. These calls replace the child
process's memory space with the code and data of the new program, allowing it to run a different
executable.
6. Outline the primary responsibilities of the init process (PID 1) during system startup.
Answer: The init process is the first process started by the kernel. Its main responsibilities include
initializing the system environment, starting essential system services and daemons, and managing
terminal logins via getty processes.
7. What is the role of the getty process in the shell creation sequence?
Answer: The getty process listens for connections on terminal lines, configures the terminal, and
presents the initial login prompt to the user, acting as an intermediary between the terminal and the
login program.
10. How do you run a command in the background using the shell, and what is a potential drawback?
Answer: You append an ampersand (&) to the end of the command (e.g., long_command &). A potential
11. What is the purpose of the nohup command when running background jobs? Provide an example.
Answer: The nohup command prevents a background job from being terminated by a SIGHUP signal
when the user logs out or closes the terminal. Example: nohup ./long_script.sh & will run long_script.sh
in the background, immune to hangups.
12. What does the nice command do, and what is the range of nice values?
Answer: The nice command adjusts the priority of a process by altering its nice value. The nice value
ranges from -20 (highest priority) to 19 (lowest priority), with a default of 0. Higher values mean the
process is "nicer" to others.
13. How can you reduce the priority of an already running process with PID 5678?
Answer: You can use the renice command: renice 10 -p 5678. This will set the nice value of the process
with PID 5678 to 10, reducing its priority.
14. What is a signal in UNIX, and what command is used to send signals to processes?
Answer: A signal is an asynchronous notification sent to a process to indicate an event. The kill
command is used to send signals to processes, identified by their PID.
15. What is the difference between the SIGTERM and SIGKILL signals?
Answer: SIGTERM (signal 15) politely requests a process to terminate, allowing it to perform cleanup.
SIGKILL (signal 9) forcefully terminates a process immediately without giving it a chance to clean up.
16. How can you send a running foreground job to the background without terminating it?
Answer: You can press Ctrl+Z while the job is in the foreground. This sends a SIGTSTP signal, suspending
the job and placing it in the background (stopped). You can then use bg to resume it in the background.
17. What command is used to bring a background job with job ID 2 back to the foreground?
Answer: The fg command is used. To bring job ID 2 to the foreground, you would type fg %2.
18. How can you list all the background jobs associated with your current shell session?
Answer: The jobs command displays a list of the active background jobs, showing their job IDs, status
(e.g., Running, Stopped, Done), and the command that was executed.
19. What is the purpose of the at command? Provide a simple example to run backup.sh at 6 PM today.
Answer: The at command schedules commands to be executed at a specified time in the future.
Example: echo "./backup.sh" | at 6:00 PM. This will execute the backup.sh script at 6:00 PM on the
current day.
20. How does the batch command differ from the at command in scheduling job execution?
Answer: The batch command schedules jobs to run when the system's load average falls below a certain
threshold, making it suitable for non-time-critical tasks that should run when the system is less busy,
unlike at, which executes at a specific time.
1. Explain the process creation cycle in detail, including the roles of fork() and exec() system calls. Provide
a scenario where this model is crucial.
Answer: The process creation cycle typically begins with the fork() system call, where a parent process
duplicates itself to create a child process. The child inherits most of the parent's state. Subsequently, the
child often uses an exec() system call to replace its memory space with a new program. This two-step
process is crucial for command execution in the shell. When you type a command, the shell forks a child,
and then the child uses exec() to run the command, allowing the shell to remain active and handle other
commands.
2. Describe the sequence of steps involved in the shell creation process from system boot-up (init) to a
user receiving a shell prompt. Explain the function of each process in this chain.
Answer: The shell creation begins with init (PID 1), the first process. init starts getty (or a similar
program) on terminal lines. getty listens for connections, configures the terminal, and presents the
"login:" prompt. Upon entering credentials, getty executes the login program, which authenticates the
user. If successful, login sets up the user's environment and finally executes the user's default shell (e.g.,
bash). This chain ensures a secure and managed login process, culminating in an interactive shell for the
user.
3. Elaborate on the different states a UNIX process can be in, providing a brief description of each and
the typical reasons for a process being in that state.
Answer: UNIX processes can be in various states. Running (R) means the process is currently executing
on the CPU or ready to run. Sleeping (S) indicates the process is waiting for an event like I/O completion.
Disk Sleep (D) is similar but specifically waiting for disk I/O and is usually uninterruptible. Stopped (T)
signifies the process has been paused, often by a signal like SIGTSTP. Zombie (Z) means the process has
terminated but its resources haven't been cleaned up by the parent. Understanding these states helps in
diagnosing system performance and process behavior.
4. Discuss the implications of zombie processes on a UNIX system and explain how they arise. What
steps can be taken to mitigate issues caused by them?
Answer: Zombie processes, while consuming minimal resources, retain their PID in the process table. If a
parent process fails to call wait() on its terminated children, these zombies accumulate, potentially
exhausting the system's limit on the number of processes that can be created. They arise due to poor
programming or when parent processes terminate prematurely without reaping their children.
Mitigation involves ensuring parent processes properly handle child termination using wait() or
waitpid(). In cases of orphaned zombies, the init process eventually adopts and reaps them.
5. Explain the use of the & operator and the nohup command for managing background jobs. Highlight
the scenarios where each would be most appropriate and the key differences between them.
Answer: The & operator sends a command to run in the background, allowing the user to continue using
the terminal. However, these jobs are still tied to the terminal session and can be terminated by a
SIGHUP upon logout. Example: find / -name "*.log" &. nohup is used to make a background job immune
to SIGHUP signals, ensuring it continues running even after the user logs out. Output is typically
redirected to nohup.out. Example: nohup ./long_process.sh &. Use & for short background tasks within
a session; use nohup for long-running tasks that need to persist beyond the current session.
7. Explain the concept of signals in UNIX process management. Describe at least three commonly used
signals and how they can be used to control or terminate processes using the kill command.
Answer: Signals are asynchronous notifications sent to processes to indicate events. SIGINT (2), typically
generated by Ctrl+C, interrupts a foreground process. Example: kill -2 5678. SIGTERM (15) politely
requests a process to terminate, allowing cleanup. Example: kill 9876. SIGKILL (9) forcefully terminates a
process immediately. Example: kill -KILL 4321. Signals are a fundamental mechanism for inter-process
communication and process control.
8. Detail the process of moving a job between the foreground and background in a UNIX shell. Include
the commands used and the signals involved when suspending and resuming jobs.
Answer: A foreground job can be suspended by pressing Ctrl+Z, which sends the SIGTSTP signal,
stopping the process and returning control to the shell. The jobs command lists suspended and
background jobs. To resume a suspended job in the background, use bg %job_id. To bring a background
job to the foreground, use fg %job_id. These commands allow users to manage interactive and long-
running tasks efficiently without needing multiple terminal windows.
9. Compare and contrast the at and batch commands for scheduling future command execution. Discuss
the scenarios where using one might be more advantageous than the other, providing illustrative
examples.
Answer: Both at and batch schedule commands for later execution. at executes commands at a specific
time and date. Example: echo "date > logfile" | at 3 AM tomorrow. This is useful for time-critical tasks.
batch executes commands when the system load average is low. Example: ./resource_intensive_task |
batch. This is advantageous for tasks that can wait for system resources to be less utilized, preventing
performance impact during peak times. at is time-driven, while batch is resource-driven.
10. Describe a scenario where you might need to use a combination of backgrounding a process (&),
making it immune to hangups (nohup), and adjusting its priority (nice or renice). Explain the rationale
behind using each of these techniques in your scenario.
Answer: Imagine running a long-term data analysis script (analyze_data.py) on a remote server via SSH.
You anticipate the analysis will take several hours and you might lose your SSH connection. To ensure the
script completes uninterrupted, you would use nohup ./analyze_data.py & to run it in the background
and make it immune to SIGHUP. Furthermore, to prevent this CPU-intensive task from impacting other
users on the shared server, you might reduce its priority using nice 10 ./analyze_data.py &. If you later
notice it's still consuming too many resources, you could use renice 15 -p <PID_of_analyze_data.py> to
further lower its priority. This combination ensures the task runs reliably in the background without
negatively impacting system performance or being terminated by connection loss.
Chapter 07
Customization
Environment variables are dynamic named values that can affect the way running processes will behave on a
computer. They provide a way to pass configuration information to applications. Think of them as global settings
that programs can look up to determine things like where to find files, what kind of terminal you're using, or
even the prompt you see.
• Name-Value Pairs: Each environment variable has a name and an associated value, like
PATH=/usr/local/bin:/usr/bin:/bin.
• Inheritance: When a process creates a new process (its child), the child inherits a copy of the parent's
environment variables. This allows settings to propagate down the process tree.
• Global Scope (within a session): Once set within a shell session, an environment variable is generally
accessible by any command executed within that session or any child processes it creates.
• Configuration: They are often used to configure application behavior without needing to modify the
application's code directly.
• printenv or env: To display a list of all currently set environment variables and their values.
• echo $<VARIABLE_NAME>: To display the value of a specific environment variable (e.g., echo $HOME).
• export VARIABLE_NAME=value: To set or modify an environment variable and mark it for export,
meaning it will be passed on to child processes.
• VARIABLE_NAME=value: To set or modify an environment variable for the current shell. To make it
available to child processes, you'll usually need to export it.
1. HOME: Specifies the absolute path to the current user's home directory. This is often where personal
configuration files and data are stored.
o Usage: Many applications and shell commands (like cd without arguments) use $HOME to
quickly navigate to the user's home directory.
2. PATH: A colon-separated list of directories that the shell searches through when you enter a command
without specifying its full path.
o Example: PATH=/usr/local/bin:/usr/bin:/bin:/opt/myprogram/bin
3. LOGNAME or USER: Stores the username of the current user. LOGNAME is often set by the login
process, while USER might be set by other means. They usually hold the same value.
o Example: If you logged in as "john", both LOGNAME and USER would likely be set to "john".
o Usage: Applications can use these variables to identify the user running them. Your shell prompt
might also include this information.
4. TERM: Specifies the type of terminal or terminal emulator you are using (e.g., xterm, vt100, screen,
tmux).
o Example: TERM=xterm-256color
o Usage: Applications, especially those that manipulate the terminal display (like text editors such
as vim or nano), use $TERM to determine the terminal's capabilities and how to properly format
output and handle input.
5. PWD: Holds the absolute path of the current working directory. This is the directory you are currently
"in" when you use the shell.
o Example: If you are in the "documents" directory within your home directory, PWD might be
/home/john/documents.
o Usage: The shell updates $PWD automatically as you navigate the file system using cd. Many
commands and scripts rely on the current working directory.
6. PS1 (Primary Prompt String): Defines the appearance of your primary shell prompt – the prompt you
see when the shell is ready to accept a command.
o Usage: You can customize $PS1 extensively to include information like the username, hostname,
current directory, time, and even colors, making your command line more informative and
visually appealing.
7. PS2 (Secondary Prompt String): Defines the appearance of the secondary shell prompt, which is
displayed when the shell expects more input to complete a command (e.g., after you've started a multi-
line command or an incomplete statement).
o Usage: If you enter a command that requires more input (like a for loop without its done), the
shell will display $PS2 on subsequent lines until the command is complete. You can customize
this as well, though it's less common than customizing $PS1.
You can view the current values of these variables using echo:
echo $HOME
echo $PATH
echo $LOGNAME
Netaji Subhash Engineering College
137
echo $TERM
echo $PWD
echo "$PS1"
echo "$PS2"
You can experiment with changing PS1 to personalize your shell prompt. For example:
export PS1="MyShell> "
Your prompt will now look like MyShell>. Remember that changes made directly in the shell are usually temporary
and will only last for the current session. To make them permanent, you typically need to add the export
commands to your shell's configuration file (e.g., .bashrc for Bash, .zshrc for Zsh).
Aliases: Aliases are shortcuts or alternative names that you can define for commands. They allow you to replace
long or frequently used commands with shorter, more convenient names or to add default options to commands.
• Shell Feature: Aliases are a feature of the shell itself (like Bash, Zsh, etc.).
• Temporary (by default): Aliases defined directly in the shell are usually only active for the current
session.
• alias short_name='long command with options': To define a new alias. The alias name should not
contain spaces, and the command it represents should be enclosed in single quotes.
Now, instead of typing ls -l to get a detailed listing, you can just type ll.
These aliases add the -i (interactive) option to rm, cp, and mv, which can help prevent accidental file deletions or
overwrites.
Now, typing update will run both the package list update and the package upgrade commands.
To make aliases persistent across shell sessions, you need to add the alias commands to your shell's
configuration file (e.g., .bashrc for Bash, .zshrc for Zsh). These files are typically executed when you start a new
shell session.
The command history is a feature of the shell that keeps a record of the commands you have entered in the
current and previous shell sessions. This allows you to easily recall and reuse commands, saving you typing time
and effort.
• Storage: The shell typically stores your command history in a file in your home directory (e.g.,
.bash_history for Bash, .zsh_history for Zsh).
• Recall: You can access your command history using various methods:
o Up and Down Arrow Keys: Pressing the up arrow key usually cycles backward through your
history, showing you the previously entered commands. The down arrow key cycles forward.
o history command: Typing history displays a numbered list of your recent commands. You can
often use options with history to control the output (e.g., history 10 to see the last 10
commands).
o Reverse Search (Ctrl+R): In many shells (like Bash and Zsh), pressing Ctrl+R allows you to search
backward through your history for a command containing specific text. You can press Ctrl+R
repeatedly to cycle through matches.
o Bang (!) Commands: The ! character followed by certain specifiers allows you to execute
commands from your history:
▪ !n: Executes the command with the number n from the history list.
▪ !-n: Executes the nth command from the end of the history list.
▪ !string: Executes the most recent command that starts with string.
• Customization: You can often customize aspects of your command history, such as the number of
commands to store, whether to save commands immediately after execution, and whether to ignore
certain commands. These settings are usually configured in your shell's configuration file (e.g., .bashrc,
.zshrc).
The command history is an invaluable tool for productivity on the command line, allowing you to quickly repeat,
modify, and reuse past commands without retyping them from scratch.
Review Question
Review Questions
Short Answer Type Questions
3. What role does the PATH environment variable play in executing commands?
Ans: The PATH environment variable is a colon-separated list of directories that the shell searches
through when you enter a command without specifying its full path. Customizing the PATH allows users
to easily execute their own scripts or installed programs located in non-standard directories without
typing the complete path each time.
11. How can you view the current values of all environment variables in your Unix session?
Ans: You can view the current values of all environment variables in your Unix session by using the
command env or printenv. These commands will display a list of all the defined environment
variables and their corresponding values.
12. Explain how to set a new environment variable temporarily in your current shell session.
Ans: To set a new environment variable temporarily in your current shell session, you can use the syntax
VARIABLE_NAME=value. For example, MYVAR=hello will set the environment variable MYVAR to "hello"
for the duration of the current session. This variable will not be persistent across new sessions.
13. How can you make an environment variable persistent across multiple login sessions?
Ans: To make an environment variable persistent, you typically need to add its definition to one of the
shell's configuration files, such as .bashrc, .zshrc, or .profile in your home directory. After adding the line
(e.g., export MYVAR=hello), you need to source the file (e.g., source ~/.bashrc) or log out and log back in
for the changes to take effect.
14. Give an example of how you might use an alias to customize a common command.
Ans: For example, you might create an alias for the ls -l command to always include human-readable file
sizes and show hidden files by defining the alias alias l='ls -lah'. Now, simply typing l in the terminal will
execute ls -lah.
15. How do you list the currently defined aliases in your Unix shell?
Ans: You can list all the currently defined aliases in your Unix shell by simply typing the command alias
and pressing Enter. This will display a list of all active aliases and their corresponding command
substitutions.
16. Explain how to remove a previously defined alias in your current shell session.
Ans: To remove a previously defined alias in your current shell session, you can use the unalias
command followed by the name of the alias you want to remove. For example, unalias l would remove
the alias we defined earlier for ls -lah.
17. What is command history in Unix, and how can you access previously executed commands?
Ans: Command history in Unix is a feature that records the commands you have previously executed in
the shell. You can access this history using the history command, or by using the up and down arrow
keys to navigate through the previously entered commands.
18. How can you re-execute the last command you entered in the Unix shell?
Ans: You can re-execute the last command you entered in the Unix shell using a few shortcuts: !! or by
pressing the up arrow key once and then pressing Enter.
19. How can you execute the command at a specific position in your command history?
Ans: You can execute a command at a specific position in your command history by using the !n syntax,
where n is the line number of the command as displayed by the history command. For example, if
history shows command number 15 as ls -l, you can execute it again by typing !15.
20. Describe a scenario where customizing the PS1 environment variable can be particularly useful.
Ans: Customizing the PS1 environment variable is particularly useful in scenarios where you frequently
work on multiple servers or within different virtual environments. By including the hostname or the
name of the active environment in your prompt, you can quickly and easily identify the system you are
1. Explain in detail how environment variables contribute to the flexibility and customization capabilities
of the Unix operating system. Provide at least two distinct examples of common environment variables
and how their modification can alter the behaviour of commands or applications.
Ans: Environment variables are fundamental to Unix's flexibility, acting as dynamic configuration
parameters that influence how processes execute. They allow users and the system to communicate
configuration information to applications without altering their core code. For instance, modifying the
PATH variable allows users to extend the shell's search directories for executables. If a user has custom
scripts in ~/bin, adding this directory to PATH (export PATH=$PATH:~/bin) enables direct execution of these
scripts by name. Another example is the EDITOR variable. Many utilities, like git or crontab, use this
variable to determine the default text editor. By setting export EDITOR=vim, the user ensures that vim is
launched whenever these tools require text editing, personalizing their workflow.
2. Discuss the importance of the HOME and PATH environment variables in a multi-user Unix environment.
How do these variables ensure that different users have personalized experiences and can execute
commands effectively?
Ans: In a multi-user Unix environment, HOME and PATH are crucial for providing personalized and
functional experiences for each user. The HOME variable uniquely identifies each user's personal
workspace, ensuring that their files and configurations are kept separate and accessible only to them.
When a user logs in, they are automatically placed in their home directory. The PATH variable allows each
user to have their own set of preferred executable locations. System administrators typically set a default
PATH, but individual users can customize it to include directories containing their own scripts or locally
installed software, without affecting other users' ability to execute standard system commands. This
separation ensures both privacy and the ability for individual workflow customization.
Chapter 08
Filter
In Unix, a filter is a command that processes data from standard input (stdin), performs a specific operation on
that data, and then writes the processed data to standard output (stdout). Filters are fundamental tools in Unix-
like operating systems, designed to work in a pipeline. A pipeline is a sequence of commands where the output
of one command becomes the input of the next command, connected by the pipe symbol (|). This modular
approach allows users to combine simple commands to perform complex data manipulations.
Filters are essential because they promote the Unix philosophy of "do one thing and do it well." By chaining
these small, specialized tools, users can efficiently manipulate text and data.
Prepare file for printing (pr): The pr command formats files for printing. It adds headers, footers, page numbers,
and can paginate the file, making it suitable for printing on paper. Example:
pr myfile.txt
This command formats data.txt with 60 lines per page, adds the header "My Report," and sends the output to
the default printer using the lp command.
The head command is used to display the first few lines of a file or standard input. By default, it shows the first
10 lines.
Syntax:
head [OPTION]... [FILE]...
Common Options:
• -q, --quiet, --silent: Suppress printing of headers when multiple files are specified.
Examples:
The tail command is the counterpart to head. It displays the last few lines of a file or standard input. By default,
it shows the last 10 lines.
Syntax:
tail [OPTION]... [FILE]...
Common Options:
• -f, --follow[={name|descriptor}]: Output appended data as the file grows. This is useful
for monitoring log files in real-time.
• -q, --quiet, --silent: Suppress printing of headers when multiple files are specified.
Examples:
The cut command is used to extract specific columns (fields) or characters from each line of a file or standard
input. It's particularly useful for working with delimited data.
Syntax:
cut OPTION... [FILE]...
• -d DELIM: Use DELIM as the field delimiter instead of the default tab.
• -s, --only-delimited: Do not print lines not containing delimiters (when used with -f).
LIST Format:
LIST can be a single number, a range (e.g., n-m), or a comma-separated list of numbers or ranges.
Examples:
Assume you have a file data.csv with the following content (comma-separated):
Name,Age,City
Alice,30,New York
Bob,25,London
Charlie,35,Paris
Output:
Name
Alice
Bob
Charlie
2. Extract the names and cities (first and third fields):
cut -d ',' -f 1,3 data.csv
Output:
Name,City
Alice,New York
Bob,London
Charlie,Paris
3. Extract the age and city (second and third fields):
cut -d ',' -f 2-3 data.csv
Output:
Age,City
30,New York
25,London
35,Paris
The paste command is used to merge corresponding lines of one or more files. By default, it joins the lines with a
tab character.
Syntax:
paste [OPTION]... [FILE]...
Common Options:
• -s, --serial: Paste all lines of each file in turn, one after the other, instead of in parallel.
Examples:
and ages.txt:
30
25
35
The sort command is used to sort the lines of a text file or standard input. By default, it sorts lexicographically
based on the entire line.
Syntax:
sort [OPTION]... [FILE]...
Common Options:
• -u, --unique: With -c, check that only a single instance of each line appears. Without -c, output
only the first of an equal run.
Examples:
The uniq command filters adjacent matching lines from a file or standard input. It only removes duplicates that
appear consecutively. Therefore, it's often used in conjunction with sort to remove all duplicate lines.
Syntax:
uniq [OPTION]... [INPUT [OUTPUT]]
Common Options:
• -c, --count: Prefix each output line with the count of occurrences.
• -d, --repeated: Only output duplicate lines, one for each group.
Netaji Subhash Engineering College
150
• -D, --all-repeated[=METHOD]: Print all duplicate lines. METHOD can be separate (default),
prepend, or append.
• -f NUM, --skip-fields=NUM: Avoid comparing the first NUM fields. Fields are separated by whitespace.
Examples:
The real power of these filters comes when you combine them using the pipe (|) operator. This allows you to
perform complex data transformations in a concise and readable way.
Example:
Suppose you have a log file web_access.log where each line contains information about a web request, including
the IP address as the first field (space-separated). You want to find the top 5 most frequent IP addresses.
cut -d ' ' -f 1 web_access.log | sort | uniq -c | sort -nr | head -n 5
1. cut -d ' ' -f 1 web_access.log: Extracts the first field (IP address) from each line, using space as the
delimiter.
2. sort: Sorts the list of IP addresses alphabetically (which is fine for counting).
3. uniq -c: Counts the occurrences of each unique IP address. The output will be like: 12 192.168.1.100, 5
10.0.0.5.
4. sort -nr: Sorts the output numerically (-n) in reverse order (-r), based on the counts (which are at the
beginning of each line).
5. head -n 5: Takes the first 5 lines of the sorted output, giving you the top 5 most frequent IP addresses.
This example demonstrates how you can chain these simple filters together to achieve a more complex data
analysis task.
The tr command translates or deletes characters. It can replace one set of characters with another or remove
specific characters from the input. Example:
tr 'a-z' 'A-Z' < myfile.txt
This command translates all lowercase letters in myfile.txt to uppercase. The < redirects the input of tr from the
file.
tr -d '0-9' < data.txt
• options: These are optional flags that modify grep's behavior (e.g., -i for case-insensitive, -n for line
numbers, -v to invert the match).
• pattern: This is the search pattern, which can be a simple string or a regular expression. It's often best to
enclose the pattern in single quotes to prevent the shell from interpreting special characters.
• file...: This specifies the file(s) to search. If no file is specified, grep reads from standard input.
This will display all lines in /var/log/syslog that contain the word "error".
Case-insensitive search:
grep -i "Error" /var/log/syslog
This will display all lines in /var/log/messages that do not contain "info".
This will show lines containing "warning" in app.log and prefix each matching line with its line number.
This will search for "failed" in all files ending with .log in the current directory.
This pipes the long listing of files to grep, which then filters for lines containing "myfile".
BRE is the default regular expression syntax used by grep when no specific option is provided (i.e., just grep). In
BRE, some special characters (metacharacters) require a backslash (\) to be interpreted as special. If they are not
escaped, they are treated as literal characters.
• * (Asterisk): Matches zero or more occurrences of the preceding character or regular expression.
o grep "a*b" file.txt will match "b", "ab", "aab", "aaab", etc.
o grep "^start" file.txt will match lines that begin with "start".
o grep "end$" file.txt will match lines that end with "end".
o grep "[^0-9]" file.txt will match lines not containing any digit. (The ^ inside brackets negates the
set).
• \< and \> (Word boundaries): Match the beginning and end of a word, respectively.
o grep "\<the\>" file.txt will match the whole word "the", not "there" or "other".
• \(...\) (Grouping): Groups regular expressions. In BRE, you need to escape the parentheses.
o grep " \(ab\)*c" file.txt will match "c", "abc", "ababc", etc. (Zero or more occurrences of "ab"
followed by "c").
• \{n\} \{n,\} \{n,m\} (Quantifiers): In BRE, you need to escape the curly braces.
o \{n,\}: Matches at least n occurrences. grep "a\{2,\}" file.txt matches "aa", "aaa", etc.
o \{n,m\}: Matches between n and m occurrences (inclusive). grep "a\{1,3\}" file.txt matches "a",
"aa", "aaa".
Extended Regular Expressions (ERE) provide additional features and make some common operations simpler by
not requiring backslashes for certain metacharacters that do require them in BRE.
Historically, egrep was a separate command specifically designed to use ERE. However, modern grep
implementations include the functionality of egrep via the -E option. So, egrep is essentially a deprecated
command that is now an alias or a hard link to grep -E.
• ? (Question mark): Matches zero or one occurrence of the preceding character or regular expression.
(Special in ERE, requires \? in BRE)
• + (Plus sign): Matches one or more occurrences of the preceding character or regular expression.
(Special in ERE, requires \+ in BRE)
o grep -E "a+b" file.txt will match "ab", "aab", "aaab", etc. (at least one 'a').
• | (Pipe): Acts as an OR operator, matching either the expression before or after the pipe. (Special in ERE,
requires \| in BRE)
• ( ) (Grouping): Parentheses are special for grouping. (Special in ERE, requires \(...\) in BRE)
o grep -E "(word1|word2) phrase" file.txt will match "word1 phrase" or "word2 phrase".
• {n} {n,} {n,m} (Quantifiers): Curly braces are special for quantifiers. (Special in ERE, requires \{...\} in
BRE)
Review Questions
Review Questions
Short Answer Type Questions
2. How can you display the first 10 lines of a file using UNIX commands?
The head command is used to display the first 10 lines of a file by default. Syntax: head filename.
3. Which command displays the last few lines of a file, and how is it used?
The tail command shows the last 10 lines of a file by default. Usage: tail filename.
11. How do you use cut to display only the first field of a colon-separated file?
Use: cut -d ":" -f1 filename. This extracts only the first field using ":" as delimiter.
19. How do you merge two files line by line using paste?
Use paste file1 file2 to merge lines from both files side by side with tab as default delimiter.
20. How can you count repeated lines in a file using uniq?
Use uniq -c filename after sorting. It prefixes lines by the number of occurrences.
o cut -d "," -f2 filename.csv displays the second field from a comma-separated file.
Output: UNIX
Example 2: Delete spaces
echo "a b c" | tr -d ' '
Output: abc
o +: one or more
o ?: zero or one
Matches filenames like file1.txt, file22.txt, etc. It is useful for advanced pattern matching in
scripts and filters.
Chapter 09
Shell scripts
A shell script is a text file containing a series of commands intended for execution by the Unix/Linux shell (like
Bash, sh, etc.). Shell scripting allows users to automate repetitive tasks, execute commands in sequence, create
control structures (like loops and conditionals), and build complex programs using simple scripting techniques.
Shell scripts are widely used for system administration, automation, monitoring, and batch processing. Since
they are interpreted rather than compiled, shell scripts are easy to write and modify.
2. Start the script with the shebang (#!/bin/bash) to specify the shell.
nano is a simple, user-friendly command-line text editor for Unix and Linux systems. It’s often used by beginners
because it’s easy to learn and doesn’t require advanced knowledge of keyboard shortcuts like vi or vim. nano is
especially useful for editing configuration files and writing shell scripts.
Example:
nano myscript.sh
Here are some essential nano commands (use Ctrl key represented as ^):
Shortcut Description
Note: All commands are listed at the bottom of the nano screen while editing.
4. Make it executable:
chmod +x hello.sh
• Replace: Press Ctrl + \, enter the word to find, press Enter, enter the replacement word.
Disadvantages of nano
Introduction to vi Editor
The vi editor (short for visual editor) is a powerful text editor available by default on almost every Unix and Linux
system. It’s lightweight, fast, and versatile, making it a favorite among system administrators and programmers.
Though it has a steeper learning curve than nano, its efficiency and features make it a valuable tool once mastered.
The improved version of vi is called vim (Vi IMproved), which offers additional features like syntax highlighting,
undo levels, and plugin support.
Modes in vi Editor
Mode Purpose
Opening vi
vi filename
Basic vi Commands
h Move left
l Move right
j Move down
k Move up
0 Start of line
$ End of line
G Go to end of file
gg Go to beginning of file
nG Go to line number n
Example Workflow in vi
1. Open file:
vi demo.sh
Advantages of vi
Disadvantages
A simple shell script is just a text file containing one or more commands. When executed, the shell reads and
runs each command in sequence.
Example:
Let's create a script named hello.sh that prints a greeting and the current date.
• #!/bin/bash: This is called a "shebang" and tells the system which interpreter to use for executing the
script (in this case, Bash).
• # This is a simple shell script: Lines starting with # are comments and are ignored by the shell.
• echo: This command is used to print text to the console.
• $(date): This is command substitution. The date command is executed, and its output replaces $(date).
Output:
Interactive shell scripts prompt the user for input during execution, making them more dynamic and user-
friendly.
Example:
Let's create a script named user_info.sh that asks for the user's name and age.
o read name: The read command waits for user input and stores it in the variable name.
Shell scripts can accept arguments directly from the command line when they are executed. These arguments
are accessed using special variables called "positional parameters."
• $1, $2, $3, ...: The first, second, third, and subsequent arguments.
Let's create a script named greet_user.sh that takes a name and a greeting message as arguments.
#!/bin/bash
echo "Script name: $0"
echo "Number of arguments: $#"
echo "First argument (name): $1"
echo "Second argument (message): $2"
echo "$2, $1!"
echo "All arguments as a single string: $*"
echo "All arguments as separate strings:"
for arg in "$@"; do
echo "- $arg"
done
3. Make the script executable:
chmod +x greet_user.sh
Output:
Script name: ./greet_user.sh
Number of arguments: 2
First argument (name): Alice
Second argument (message): Good morning
Good morning, Alice!
All arguments as a single string: Alice Good morning
All arguments as separate strings:
- Alice
- Good morning
Logical operators allow you to combine commands based on their exit status (0 for success, non-zero for failure).
• && (Logical AND): The second command executes only if the first command succeeds.
• || (Logical OR): The second command executes only if the first command fails.
Example:
#!/bin/bash
FILE="non_existent_file.txt"
EXISTING_FILE="hello.sh"
# Example of &&
# This will try to create a directory and then list its content
echo "--- Using && ---"
mkdir my_new_dir && ls my_new_dir
# This will fail to list non_existent_dir and then not proceed with echo
echo "--- Failed && ---"
ls non_existent_dir && echo "This won't be printed"
# Example of ||
# This will try to remove a file; if it fails (e.g., file doesn't exist), it will echo a message
echo "--- Using || ---"
rm $FILE || echo "$FILE does not exist or could not be removed."
# This will successfully remove existing_file and then not proceed with
echo
echo "--- Successful || ---"
rm $EXISTING_FILE || echo "$EXISTING_FILE was not removed."
o rm hello.sh will likely succeed (if hello.sh exists), so the echo statement after || won't run.
Conditional statements allow your script to make decisions and execute different blocks of code based on certain
conditions.
a. if statement
The if statement evaluates a condition and executes a block of code if the condition is true.
Syntax:
if condition; then
# code to execute if condition is true
elif another_condition; then
# code to execute if another_condition is true
else
# code to execute if no condition is true
fi
Conditions:
Conditions are often tested using the test command or square brackets [].
• File operators:
• String operators:
• Arithmetic operators:
Example (if):
Let's create a script named file_checker.sh that checks if a file exists and is a directory.
#!/bin/bash
read -p "Enter a file or directory path: " path
if [ -e "$path" ]; then
echo "'$path' exists."
if [ -f "$path" ]; then
echo "'$path' is a regular file."
elif [ -d "$path" ]; then
echo "'$path' is a directory."
else
echo "'$path' is neither a regular file nor a directory."
fi
else
echo "'$path' does not exist."
fi
o "$path": Quoting variables is important to prevent word splitting and globbing issues, especially
when paths contain spaces.
Output (example):
Enter a file or directory path: /etc/passwd
'/etc/passwd' exists.
'/etc/passwd' is a regular file.
Enter a file or directory path: /home
'/home' exists.
'/home' is a directory.
b. case statement
The case statement is useful for handling multiple possible values of a single variable or expression. It's often
cleaner than a long series of if-elif-else statements.
Syntax:
case expression in
pattern1)
# code for pattern1
;;
pattern2)
# code for pattern2
;;
*)
# default code (if no other pattern matches)
;;
esac
Example (case):
Let's create a script named menu.sh that presents a simple menu to the user.
case $choice in
1)
echo "Current date and time: $(date)"
;;
2)
echo "Files in current directory:"
ls -l
;;
3)
echo "Disk space usage:"
df -h
;;
4)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid choice. Please enter a number between 1 and 4."
;;
Esac
o *): The default case, matching any value not covered by previous patterns.
As seen in the if section, test and [] are used for evaluating conditions. The [[]] (double square brackets) provide
more advanced features and are specific to Bash (not POSIX shell).
o Allows C-style logical operators (&&, ||) within the brackets, simplifying complex conditions.
o No need for quotes around variables within [[]] unless they contain shell special characters.
Example:
#!/bin/bash
name="Alice"
age=25
filename="my_document.txt"
• $?: This special variable holds the exit status of the last executed command.
Output:
--- Using [] ---
Alice is over 20.
--- Using [[]] ---
Alice is over 20 (using double brackets).
--- Using regular expression with [[]] ---
my_document.txt is a text file.
--- Checking exit status of commands ---
root:x:0:0:root:/root:/bin/bash
User 'root' found in /etc/passwd.
Shell scripts primarily treat values as strings. For arithmetic operations, you need specific tools.
a. expr command
The expr command evaluates an expression and prints the result to standard output. It's an older method and
has some limitations.
Example (expr):
#!/bin/bash
num1=10
num2=5
This is the preferred and more modern way to perform arithmetic in Bash. It allows C-style arithmetic
expressions directly within the shell.
Syntax: variable=$((expression))
Example ($(())):
sum=$((num1 + num2))
echo "Sum (using \$()): $sum"
difference=$((num1 - num2))
echo "Difference (using \$()): $difference"
quotient=$((num1 / num2))
echo "Quotient (using \$()): $quotient"
increment=$((num1++)) # Post-increment
echo "Incremented num1 (post): $increment, num1 is now: $num1"
decrement=$((--num2)) # Pre-decrement
echo "Decremented num2 (pre): $decrement, num2 is now: $num2"
Output:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Sum (using $()): 15
Difference (using $()): 5
Product (using $()): 50
Quotient (using $()): 2
Remainder (using $()): 0
Incremented num1 (post): 10, num1 is now: 11
Decremented num2 (pre): 4, num2 is now: 4
While expr is primarily for arithmetic, it can also perform some basic string operations like finding length,
extracting substrings, and finding the index of a substring. However, for more advanced string manipulation, Bash's
built-in string manipulation features are generally preferred.
# String length
length=$(expr length "$my_string")
echo "Length of '$my_string': $length"
Output:
Length of 'Hello World': 11
Substring (1-5): Hello
Index of 'oW': 5
Match 'Hello': Hello
Match 'Goodbye':
Note: For modern Bash scripting, use built-in string manipulations like ${#string} for length, ${string:offset:length}
for substring, and [[ string =~ regex ]] for regular expression matching, as they are generally more efficient and
easier to read.
a. while loop
The while loop repeatedly executes commands as long as a condition remains true.
Syntax:
while condition; do
# code to execute
done
#!/bin/bash
count=5
while [ $count -gt 0 ]; do
echo "Countdown: $count"
count=$((count - 1)) # Decrement count
sleep 1 # Wait for 1 second
done
echo "Blast off!"
./countdown.sh
b. for loop
Let's create a script named fruits.sh that iterates through a list of fruits.
#!/bin/bash
fruits="Apple Banana Cherry Date"
for fruit in $fruits; do
echo "I like $fruit."
done
#!/bin/bash
echo "--- Numeric loop (1 to 5) ---"
for (( i=1; i<=5; i++ )); do
echo "Number: $i"
done
Output:
I like Apple.
I like Banana.
I like Cherry.
I like Date.
--- Iterating over files ---
Found shell script: basic_math.sh
Found shell script: check_file.sh
Found shell script: countdown.sh
Found shell script: expression_eval.sh
We briefly touched on positional parameters ($1, $2, etc.) in section 3. They are fundamental for making scripts
flexible and reusable. They allow your script to receive inputs directly when it's run from the command line.
Recall:
• $1, $2, $3, ...: The first, second, third, and subsequent arguments.
Let's create a script backup_files.sh that takes multiple files as arguments and backs them up to a specified
directory.
TARGET_DIR="$1"
shift # Shift positional parameters: $1 becomes $2, $2 becomes $3,
etc.
Netaji Subhash Engineering College
182
if [ ! -d "$TARGET_DIR" ]; then
echo "Error: Target directory '$TARGET_DIR' does not exist."
exit 1
fi
• shift: This command shifts the positional parameters to the left. After shift, $1 will contain the value that
was previously in $2, $2 will contain the value from $3, and so on. This is very useful when you want to
process the first argument differently (e.g., as a directory) and then iterate over the remaining
arguments (files).
• "$@": When used in a for loop, "$@" expands to separate arguments, which is crucial for handling
filenames with spaces correctly.
ls my_backups
Output:
file_a.txt file_b.log file with spaces.txt
1. Write shell script to rename all *.c file in your directory to *.cpp file.
#!/bin/bash
# Rename all .c files to .cpp in current directory
4. List only the name of the c files in your current directory, which have used the header file “math.h”.
#!/bin/bash
# List C files using "math.h" in the current directory
#!/bin/bash
sum=0
6. Display all the arguments along with the argument number that are provided as a command line
to the shell script. (note number of the argument may be greater than 9)
#!/bin/bash
rev=0
temp=$num
8. Write a shell script to count the no. of executable file and no. of subdirectories that exists in a
directory whose name is to given as command line argument to the script.
#!/bin/bash
dir=$1
# Output results
echo "In directory: $dir"
echo "Executable files: $exec_count"
echo "Subdirectories: $dir_count"
9. Write a shell script to delete all non-executable file of size 0 byte from a directory whose name is
to given as command line argument to the script.
#!/bin/bash
dir=$1
#!/bin/bash
filename="telephone_directory.txt"
while true
do
echo ""
echo "===== Telephone Directory ====="
echo "a. Add Record"
echo "b. Delete Record"
echo "c. Search Record"
echo "d. Exit"
echo "==============================="
echo -n "Enter your choice: "
read choice
case $choice in
a|A)
echo -n "Enter Name: "
read name
echo -n "Enter Phone Number: "
read phone
b|B)
echo -n "Enter name to delete: "
read del_name
grep -v "^$del_name :" $filename > temp.txt && mv temp.txt
$filename
echo "Record deleted (if it existed)."
;;
c|C)
echo -n "Enter name to search: "
read search_name
grep "^$search_name :" $filename
if [ $? -ne 0 ]; then
echo "Record not found."
fi
;;
d|D)
echo "Exiting..."
break
;;
*)
echo "Invalid choice. Please select a, b, c, or d."
;;
esac
done
#!/bin/bash
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 directory"
exit 1
fi
dir=$1
if [ ! -d "$dir" ]; then
echo "Error: $dir is not a directory"
exit 1
fi
13. Write a shell script to generate prime numbers between 100 and 200.
#!/bin/bash
is_prime() {
local num=$1
if [ $num -le 1 ]; then
return 1
fi
for ((i=2; i*i<=num; i++)); do
if (( num % i == 0 )); then
return 1
fi
done
return 0
}
14. Write a shell script to print the sum of all prime numbers between 1 to n.
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 n"
exit 1
fi
n=$1
is_prime() {
local num=$1
if [ $num -le 1 ]; then
return 1
fi
for ((i=2; i*i<=num; i++)); do
if (( num % i == 0 )); then
return 1
fi
done
return 0
}
sum=0
for ((i=2; i<=n; i++)); do
if is_prime $i; then
((sum += i))
fi
done
if [ $# -ne 1 ]; then
echo "Usage: $0 n"
exit 1
fi
n=$1
a=0
b=1
while [ $a -le $n ]; do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo
16. Write a shell script for checking a given year is a leap year or not.
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 year"
exit 1
fi
year=$1
#!/bin/bash
dir=$1
18. Write a shell script that accepts filenames as arguments. For every filename, it should check
whether it exits in the current directory and then cover its name to uppercase, but only if file with
the new name doesn’t exists.
#!/bin/bash
Review Questions
Review Questions
Short Answer Type Questions
1. Explain how to write a simple shell script and execute it with an example.
A simple shell script includes commands written in a text file. Example:
#!/bin/bash
echo "Hello, World!"
Save it as hello.sh, make it executable using chmod +x hello.sh, and run it using ./hello.sh. It will display
"Hello, World!".
When run, the script pauses for input and responds with a greeting. It's useful for dynamic user-driven
processes.
3. What are command line arguments in shell scripting? How are they used?
Arguments passed when executing the script can be accessed using $1, $2, etc. Example:
#!/bin/bash
echo "First argument: $1"
echo "Second argument: $2"
Running ./script.sh Hello World will output Hello and World respectively.
6. Explain the use of test and [] for expression evaluation with examples.
test or [] evaluates conditions:
if test $a -eq $b
or
if [ $a -eq $b ]
Both check if a equals b. It is used for comparisons, file existence, string evaluation, etc.
String:
name="ChatGPT"
len=`expr length "$name"`
echo $len
for loop:
for i in 1 2 3; do
echo $i
done
9. Explain positional parameters in shell scripting and how shift helps in processing them.
Positional parameters ($1, $2, etc.) hold script arguments. shift helps iterate:
while [ "$1" != "" ]; do
echo "Arg: $1"
shift
done
This loop prints all arguments one by one and moves them using shift.
10. How can you write a script to calculate the factorial of a number using a while loop?
#!/bin/bash
echo "Enter a number:"
read num
fact=1
while [ $num -gt 1 ]; do
fact=`expr $fact \* $num`
num=`expr $num - 1`
done
echo "Factorial is $fact"
This script calculates factorial by multiplying the number down to 1 using a loop.
Chapter 10
System Administration
System administration is a crucial field within information technology that involves the management,
maintenance, and operation of computer systems and networks. System administrators, often referred to as
sysadmins, are the backbone of any organization's IT infrastructure, ensuring that hardware and software run
smoothly, securely, and efficiently. Their responsibilities span a wide array of tasks, from installing and configuring
software to troubleshooting complex issues, managing user access, and implementing security measures. In
essence, sysadmins are problem-solvers who keep the digital gears turning, making sure that employees have the
resources they need to perform their jobs effectively.
This document will focus on system administration within a UNIX-like environment. UNIX, with its robust multi-
user and multitasking capabilities, forms the foundation for many mission-critical systems and servers worldwide.
Understanding its principles and command-line tools is fundamental for any aspiring or practicing system
administrator.
UNIX system administrators have a multifaceted role, encompassing a wide range of responsibilities that ensure
the optimal performance, security, and availability of UNIX-based systems. Here are some of their essential
duties:
o Installing and configuring the UNIX operating system (e.g., Linux distributions like Ubuntu, Red
Hat, CentOS; or commercial UNIX variants like Solaris, AIX, HP-UX).
o Setting up network services (DNS, DHCP, NFS, Samba, web servers, email servers).
o Regularly monitoring system health (CPU usage, memory, disk I/O, network traffic).
• Security Management:
o Implementing and managing backup strategies to ensure data integrity and availability.
• Documentation:
o Writing shell scripts (Bash, Python, Perl) to automate repetitive tasks and streamline
administrative processes.
Understanding the startup and shutdown procedures is critical for a UNIX system administrator. Improper
shutdown can lead to data corruption or system instability.
The UNIX boot process involves several stages, transitioning from low-level hardware initialization to the full
operation of the operating system. While the exact steps can vary slightly between different UNIX-like systems
(especially between SysVinit and systemd), the general flow is as follows:
1. BIOS/UEFI Initialization: The firmware (BIOS or UEFI) initializes hardware components, performs a
Power-On Self-Test (POST), and then locates the bootloader.
2. Bootloader Execution: The bootloader (e.g., GRUB, LILO) loads the kernel into memory.
3. Kernel Initialization: The kernel takes control, initializes devices, mounts the root filesystem, and starts
the init process (or systemd).
4. Init/Systemd:
o systemd (Modern systems): systemd is the primary init system in most modern Linux
distributions. It uses "units" (e.g., service units, target units) to manage the startup of services in
parallel, leading to faster boot times. It moves through different "targets" (similar to runlevels)
to reach a desired state.
• dmesg: Displays the kernel ring buffer messages, which contain information about hardware detection and
initialization during boot.
dmesg | less
• journalctl: (systemd-based systems) Used to query and display messages from the systemd journal, including
boot messages.
journalctl -b # Show messages from the current boot
journalctl -b -1 # Show messages from the previous boot
• chkconfig: (SysVinit-based systems, or for compatibility on some systemd systems) Used to manage services
that start at different runlevels.
chkconfig --list # List all services and their runlevel status
chkconfig httpd on # Enable httpd to start at default runlevels
System Shutdown
Proper shutdown procedures ensure that all processes are gracefully terminated, filesystems are unmounted
cleanly, and no data is lost.
• shutdown: The most common and recommended command for shutting down the system. It allows you to
specify a time for shutdown and send a warning message to users.
shutdown -h now # Halt (power off) the system immediately
shutdown -r now # Reboot the system immediately
shutdown -h +10 "System going down for maintenance in 10 minutes" #
Halt in 10 minutes
shutdown -c # Cancel a pending shutdown
▪ -h: Halt the system after shutdown.
▪ -r: Reboot the system after shutdown.
▪ now: Shuts down immediately.
Note: On modern systems, reboot and halt often internally call shutdown.
• Notify Users: Always notify users well in advance of a scheduled shutdown, especially on multi-user
systems.
• Save Work: Encourage users to save their work before the shutdown.
• Graceful Termination: The shutdown command sends signals to running processes to allow them to
terminate gracefully and save any open files.
User account management is a fundamental aspect of UNIX system administration. It involves creating,
modifying, and deleting user accounts, as well as managing their access rights, resources, and security.
1. Username:
o Used for logging in and identifying the user within the system.
2. Password:
3. User ID (UID):
o Typically, UIDs below 500 or 1000 are reserved for system accounts.
o Stored in /etc/passwd.
4. Group ID (GID):
5. Home Directory:
o Typically /home/username.
6. Login Shell:
o The command-line interpreter that starts when the user logs in.
o Common shells include Bash (/bin/bash), Zsh (/bin/zsh), Korn Shell (/bin/ksh), C Shell (/bin/csh).
o Configured in /etc/passwd.
7. Disk Quota:
o A mechanism to limit the amount of disk space a user or group can consume.
8. Terminal:
o The interface through which a user interacts with the system (e.g., a physical console, an SSH
client, a terminal emulator).
1. Managing Users:
2. Managing Groups:
• /etc/passwd: Contains basic user account information (username, password placeholder x, UID, GID,
GECOS field, home directory, shell).
Netaji Subhash Engineering College
208
cat /etc/passwd | grep newuser
• /etc/shadow: Contains encrypted user passwords and password expiration information. Requires root
privileges to view.
sudo cat /etc/shadow | grep newuser
• /etc/group: Contains group information (group name, password placeholder x, GID, list of members).
cat /etc/group | grep mygroup
• id: Displays the UID, GID, and supplementary groups of the current user or a specified user.
id # Show current user's IDs
id username # Show specified user's IDs
4. Disk Quotas:
• quotacheck: Scans a filesystem for disk usage and creates/updates quota files.
sudo quotacheck -cum /dev/sda1 # Check user quotas on /dev/sda1
• Security Best Practices: Always enforce strong password policies, regularly audit user accounts, and
promptly disable accounts of former employees.
• Least Privilege: Grant users only the necessary permissions to perform their tasks. Avoid giving root
access unnecessarily.
• Documentation: Maintain clear records of user accounts, their roles, and any special permissions.
By diligently performing these duties and understanding the intricacies of UNIX commands, a system
administrator ensures a stable, secure, and efficient computing environment for their organization.
Review Questions
Review Questions
Short Answer Type Questions
2. Name two key responsibilities of a UNIX system administrator regarding system security.
Ans: Two key responsibilities include implementing and maintaining robust security measures such as
configuring firewalls, managing access controls, and regularly patching vulnerabilities to protect against
unauthorized access and cyber threats.
16. Name the first process initiated during UNIX system startup.
Ans: The init (or systemd in modern systems) process is the first process initiated during UNIX system
startup. It is responsible for starting all other system processes.
17. What is the purpose of the /etc/rc.d directory (or similar) during startup?
Ans: The /etc/rc.d directory (or /etc/init.d or /etc/systemd/system in modern systems) contains scripts
that control the startup and shutdown of various system services and daemons.
20. Why is it important to shut down a UNIX system gracefully rather than just cutting power?
Ans: Graceful shutdown allows the system to properly unmount file systems, stop services, and save any
pending data to disk, preventing data corruption and ensuring system integrity.
1. Describe the essential duties of a UNIX system administrator in detail, covering at least four key areas.
Ans: A UNIX system administrator's duties are multifaceted. Firstly, system installation and configuration
involve installing the OS, configuring hardware, and setting up network services. Secondly, user and group
management includes creating, modifying, and deleting user accounts, assigning permissions, and
managing group memberships. Thirdly, system monitoring and performance tuning require constant
vigilance over resource utilization, identifying bottlenecks, and optimizing configurations to ensure
smooth operation. Finally, security management is paramount, involving implementing firewalls,
intrusion detection systems, regularly patching vulnerabilities, and enforcing access controls to protect
sensitive data and prevent unauthorized access.
3. Detail the process of gracefully shutting down a UNIX system using the shutdown command, including
common options and their effects.
Ans: Gracefully shutting down a UNIX system using the shutdown command is crucial to prevent data
loss. The general syntax is shutdown [OPTIONS] TIME [MESSAGE].
Explanation: This command immediately halts the system (-h now) and sends a message to all
logged-in users.
Common Options:
The shutdown command notifies users, prevents new logins, sends TERM signals to running
processes to allow them to terminate gracefully, and eventually unmounts file systems before
halting or rebooting. This ensures data integrity.
4. Discuss the importance of user account management for system security and efficient operation. What
are the potential risks of poor user account practices?
Ans: User account management is fundamental for both system security and efficient operation. Proper
management ensures that users only have the necessary privileges, adhering to the principle of least
privilege, thereby minimizing the attack surface. It involves setting strong password policies, regularly
reviewing user permissions, and promptly disabling accounts of departed employees. Poor user account
practices, such as weak passwords, excessive permissions, or unmanaged dormant accounts, pose
significant risks. These can lead to unauthorized access, data breaches, privilege escalation, and system
compromise, making the system vulnerable to malicious activities.
5. Explain the various components of a user account entry in /etc/passwd and their significance for user
identification and system interaction.
Ans: A typical user account entry in /etc/passwd consists of seven colon-separated fields, each holding
vital information:
6. Describe how to create a new user account in UNIX, including setting up their home directory, primary
group, and initial password. Provide command examples.
Ans: Creating a new user account involves several steps using the useradd and passwd commands.
Explanation: The useradd command creates a new user named newuser. The -m option ensures
that a home directory (/home/newuser by default) is created for the user. This command also
adds an entry for newuser in /etc/passwd and /etc/shadow and creates a primary group for
newuser with the same name.
Explanation: After creating the user, the passwd command is used to set the initial password for
newuser. The system will prompt the administrator to enter the new password twice for
confirmation. This password hash is then stored securely in /etc/shadow.
Example Output:
▪ $ sudo useradd -m newuser
▪ $ sudo passwd newuser
▪ New password:
▪ Retype new password:
▪ passwd: password updated successfully
This process ensures a new user can log in with a defined home directory and secure credentials.
7. Explain the concept of user groups in UNIX and their role in managing file permissions and access
control. Provide an example of adding a user to an existing group.
Ans: User groups in UNIX are collections of users that share common access permissions to files and
directories. They streamline access control by allowing administrators to grant permissions to a group
rather than to individual users. This simplifies management, especially in environments with many users.
Explanation: The usermod command is used to modify an existing user account. The -a option
means "append" (to the existing groups), and -G specifies the supplementary group(s) to which
the user should be added. In this case, newuser is added to the developers group.
o Verification: You can verify the user's group memberships using the groups command: groups
newuser (output might be newuser : newuser developers). By using groups, an administrator
can set read/write/execute permissions on a directory or file for the developers group, and all
members of that group will inherit those permissions without needing individual assignments.
8. How do disk quotas work in UNIX, and why are they important for system resource management?
Ans: Describe how to set a disk quota for a user. Disk quotas are a vital feature in UNIX for managing
disk space consumption. They allow system administrators to limit the amount of disk space or the
number of files (inodes) a user or group can utilize on a specific file system. This prevents any single user
from monopolizing disk resources, which could lead to a "disk full" scenario, impacting system stability
and performance for all users.
o Importance: Disk quotas ensure fair resource allocation, prevent abuse, and help maintain system
health by proactively managing storage.
▪ Enable quota support: Ensure the file system is mounted with quota options (e.g., usrquota,
grpquota in /etc/fstab).
▪ Remount the filesystem: Remount the filesystem to apply the quota options.
▪ Create quota files: Run quotacheck -cum /path/to/filesystem to create initial quota files.
▪ Set user quota: Use the edquota command.
sudo edquota -u username
Explanation: This command opens a text editor (usually vi) with the quota settings for
username. You'll see lines for blocks (disk space) and inodes (number of files). You can set
soft and hard limits for both.
This example sets a soft limit of 500MB and a hard limit of 1GB for blocks, and soft/hard limits of
1000/2000 for inodes on /dev/sda1.
o useradd (Create User): Used to create new user accounts on the system. It adds an entry to
/etc/passwd and /etc/shadow, creates a home directory (with -m), and sets up other initial user
attributes.
sudo useradd -m -s /bin/bash -g developers john_doe
Explanation: Creates a new user john_doe, creates their home directory (-m), sets their
default shell to /bin/bash, and assigns their primary group to developers.
o usermod (Modify User): Used to modify existing user account properties. This can include
changing the username, home directory, shell, primary group, or adding/removing from
supplementary groups.
sudo usermod -d /home/johndoe/new_home -s /bin/sh john_doe
o userdel (Delete User): Used to delete user accounts from the system. It removes the user's
entry from /etc/passwd and /etc/shadow. With the -r option, it also removes the user's home
directory and mail spool.
sudo userdel -r john_doe
Explanation: Deletes the user account john_doe and recursively removes their home
directory and mail spool. In summary, useradd initializes, usermod updates, and userdel
removes user accounts, forming a complete lifecycle management suite.
10. Discuss the process of system startup in a modern UNIX/Linux system using systemd. How does it
differ from older init-based systems, and what are its advantages?
Ans: In modern UNIX/Linux systems, systemd has largely replaced the traditional init process as the
primary system and service manager.
▪ Parallelization: systemd starts services in parallel, while init typically starts them
sequentially based on runlevel scripts.
▪ Unit Files vs. Shell Scripts: systemd uses declarative unit files for service configuration,
which are more structured and less error-prone than the imperative shell scripts used by
init in /etc/rc.d or /etc/init.d.
▪ On-demand Activation: systemd supports socket and D-Bus activation, allowing services
to start only when needed, unlike init which starts all services at boot.
o Advantages:
▪ Modern Features: Supports cgroups for resource control and system state snapshots.
END