0% found this document useful (0 votes)
0 views150 pages

Unix and Shell Programming

UNIX is a pioneering operating system developed in the 1970s, serving as a foundation for many modern systems. It features a multiuser, multitasking environment with a command-line interface, robust security, and a hierarchical file system. Key components include the kernel, shell commands, and system calls for process and file management, with standards like POSIX and the Single UNIX Specification ensuring compatibility and certification across UNIX-like systems.

Uploaded by

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

Unix and Shell Programming

UNIX is a pioneering operating system developed in the 1970s, serving as a foundation for many modern systems. It features a multiuser, multitasking environment with a command-line interface, robust security, and a hierarchical file system. Key components include the kernel, shell commands, and system calls for process and file management, with standards like POSIX and the Single UNIX Specification ensuring compatibility and certification across UNIX-like systems.

Uploaded by

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

<—Unix and Shell Programming—>

Introduction to UNIX System—>


UNIX is an innovative or groundbreaking operating system which was
developed in the 1970s by Ken Thompson, Dennis Ritchie, and many others at
AT&T Laboratories. It is like a backbone for many modern operating systems
like Ubuntu, Solaris, Kali Linux, Arch Linux, and also POSIX. Originally, It was
designed for developers only, UNIX played a most important role in the
development and creation of the software and computing environments. Its
distribution to government and academic institutions led to its widespread
adoption across various types of hardware components. The core part of the
UNIX system lies in its base Kernel, which is integral to its architecture,
structure, and key functionality making it the heart of the operating system.
The basic design philosophy of UNIX is to provide simple, powerful tools that
can be combined to perform complex tasks. It features a command-line
interface that allows users to interact with the system through a series of
commands, rather than through a graphical user interface (GUI).
7
Key Features of UNIX Include—>
6
. Multiuser support: UNIX allows multiple users to simultaneously access
the same system and share resources.
5
. Multitasking: UNIX is capable of running multiple processes at the same
time.
4
. Shell scripting: UNIX provides a powerful scripting language that allows
users to automate tasks.
3
. Security: UNIX has a robust security model that includes file permissions,
user accounts, and network security features.
. Portability: UNIX can run on a wide variety of hardware platforms, from
2
small embedded systems to large mainframe computers.
. Communication: UNIX supports communication methods using the write
1
command, mail command, etc.
. Process Tracking: UNIX maintains a record of the jobs that the user
creates. This function improves system performance by monitoring CPU
usage. It also allows you to keep track of how much disk space each user
uses, and the use that information to regulate disk space.
● Layer-1: Hardware: It consists of all hardware related information.
● Layer-2: Kernel: This is the core of the Operating System. It is a software
that acts as the interface between the hardware and the software. Most of
the tasks like memory management, file management, network
management, process management, etc., are done by the kernel.
● Layer-3: Shell commands: This is the interface between the user and the
kernel. Shell is the utility that processes your requests. When you type in a
command at the terminal, the shell interprets the command and calls the
program that you want. There are various commands like cp, mv, cat, grep,
id, wc, nroff, a.out and more.
● Layer-4: Application Layer: It is the outermost layer that executes the
given external applications.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

System Calls in UNIX—>
A system call is an interface between user programs and the kernel. It allows
applications to request services like file management, process control,
memory allocation, and device interaction.

System calls are categorized into the following types:

1. Process Control System Calls


Used to create, terminate, and manage processes.
System Call Description
fork() Creates a new child process.
exec() Replaces a process with a new
program.
System Call Description
fork() Creates a new child process.
exec() Replaces a process with a new
program.
wait() Makes a process wait until a child
process finishes.
exit() Terminates a process.
kill() Sends a signal to terminate a
process.
getpid() Retrieves the process ID of the
calling process.
getppid() Retrieves the parent process ID.

2. File Management System Calls


Used for handling files (open, read, write, close).
System Call Description
open() Opens a file.
read() Reads data from a file.
write() Writes data to a file.
close() Closes an open file.
lseek() Moves the file read/write pointer.
stat() Retrieves information about a file.
unlink() Deletes a file.

3. Device Management System Calls


Used to interact with hardware devices like printers, disks, and network
interfaces.
System Call Description
ioctl() Sends a control command to a
device.
read() Reads data from a device.
write() Writes data to a device.

4. Information System Calls


Used to get system-related information.
System Call Description
getuid() Returns the user ID.
getgid() Returns the group ID.
getcwd() Gets the current working directory.
uname() Retrieves system information.

5. Inter-Process Communication (IPC) System Calls


Used for communication between processes.
System Call Description
pipe() Creates a pipe for process
communication.
shmget() Creates shared memory.
shmat() Attaches shared memory to a
process.
msgget() Creates a message queue.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

1. POSIX (Portable Operating System Interface)
POSIX is a standard developed by the IEEE (Institute of Electrical and
Electronics Engineers) to ensure compatibility between UNIX-based operating
systems. It defines a set of APIs, command-line interfaces, and utilities to
allow software applications to run on different UNIX-like systems without
modification.
Key Features of POSIX
● Ensures portability of applications across different UNIX-like systems
(Linux, macOS, BSD, etc.).
● Defines system calls, shell commands, utilities, and threading models.
● Used in real-time operating systems (RTOS), embedded systems, and
servers.
● Supported by UNIX-based OS such as Linux, macOS, FreeBSD, and
Solaris.
POSIX Standards
POSIX consists of multiple parts:
● POSIX.1 – Core system API (process control, file operations, signals).
● POSIX.2 – Shell and utilities (command-line tools like grep, sed, awk).
● POSIX.4 – Real-time extensions (threading, scheduling).

2. Single UNIX Specification (SUS)


The Single UNIX Specification (SUS) is a set of standards maintained by The
Open Group to define a certified UNIX operating system. It extends POSIX to
provide a consistent UNIX environment.
Key Features of SUS
● Defines a standard API for UNIX systems.
● Ensures UNIX-certified systems are consistent, reliable, and portable.
● Includes POSIX as a subset of its specification.
● Operating systems that meet SUS requirements get the UNIX® trademark.
Certified UNIX Systems
● AIX (IBM)
● HP-UX (Hewlett-Packard)
● Solaris (Oracle)
● macOS (Apple)
Differences Between POSIX and SUS
Feature POSIX Single UNIX
Specification
(SUS)
Maintained By IEEE The Open Group
Focus Compatibility for Official UNIX
UNIX-like systems certification
Scope Defines APIs and Defines a complete
shell commands UNIX environment
Compliance Used by Linux, Only certified UNIX
macOS, BSD systems
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Internal and External Commands in UNIX
In UNIX/Linux, commands are classified into Internal Commands and External
Commands based on how they are executed by the shell.

1. Internal Commands
● Built into the shell (e.g., bash, sh, zsh).
● Do not require an external program to run.
● Execute faster because they do not need to load from disk.
● Used for simple file, directory, and process management.

Examples of Internal Commands


Command Description
cd Change directory.
pwd Print working directory.
echo Print text to the terminal.
exit Exit the shell session.
alias Create command shortcuts.
set Set shell environment variables.
export Export an environment variable.
history Show command history.
help Display shell help information.
Example (Internal Command Execution)
$ cd /home/user # Changes directory
$ pwd # Prints the current directory
$ echo "Hello, World!" # Prints Hello, World!

2. External Commands
● Stored as separate executable files in system directories like /bin, /usr/

bin, and /sbin.
● The shell finds them using the PATH environment variable.
● Can be system utilities or user-installed programs.
● Run slower than internal commands because they require disk access.
Examples of External Commands
Command Description
ls List directory contents.
cp Copy files and directories.
mv Move or rename files.
rm Delete files or directories.
grep Search text in files.
find Locate files in directories.
awk Text processing tool.
wget Download files from the internet.
tar Archive files.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Summary of Cal(calendar) Command Options—>
Command Description
cal Shows the current month's
calendar.
cal 2025 Displays the calendar for the year
2025.
cal 07 2024 Shows the calendar for July 2024.
cal -m Starts the week on Monday instead
of Sunday.
cal -j Displays Julian days.
cal -h Highlights the current date.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Summary of date Command Options—>
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Summary of echo Command Option—>
The echo command is used to display messages, variable values, and
formatted text in the terminal.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Calculator (bc)—>The bc (Basic Calculator) command is a command-line
calculator in UNIX/Linux used for performing arithmetic operations, including
floating-point calculations.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Summary of passwd Commands—>
In UNIX/Linux, the passwd command is used to change the password of a user
account.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Knowing who are logged in (who)—>
The who command is used to display information about users currently logged
into the system.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

System Information using uname—>
The uname command is used to display system information such as the OS
name, kernel version, and hardware details.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

tty Command in UNIX/Linux—>
The tty command is used to display the file name of the terminal connected to
the standard input (stdin).

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Unix File System
Unix File System is a logical method of organizing and storing large amounts
of information in a way that makes it easy to manage. A file is the smallest unit
in which the information is stored. Unix file system has several important
features. All data in Unix is organized into files. All files are organized into
directories. These directories are organized into a tree-like structure called the
file system. Files in Unix System are organized into multi-level hierarchy
structure known as a directory tree. At the very top of the file system is a
directory called “root” which is represented by a “/”. All other files are
“descendants” of root.
The Unix file system is a hierarchical file system used by Unix-based operating
systems to store and organize files and directories. It is a tree-like structure
that starts with a single directory called the root directory, which is denoted by
a forward slash (/) character.
The Unix file system uses a directory hierarchy that allows for easy navigation
and organization of files. Directories can contain both files and other
directories, and each file or directory has a unique name.
Unix file system also uses a set of permissions to control access to files and
directories. Each file and directory has an owner and a group associated with it,
and permissions can be set to allow or restrict access to these entities.
One of the most important features of the Unix file system is its support for
symbolic links, which are pointers to other files or directories. This allows for
flexible organization of files and directories without having to physically move
them around.

File on Unix Operating System:


● In Unix everything is treated as a file, even devices are also treated as a
special file.
● All devices are represented by files called special files that are located in/
dev directory.
● These are access in the same way as regular file.
● Device files has two category: 1)Block Special File , 2) Character Special
File
● In Block Special File data gets transfer in terms of block. So it has
characteristics similar to disk.
● In Character Special File data gets transfer by stream of bits in sequential
order like keyboard.
● Every file on a Unix System has a Unique Inode.
● Processes access files by well defined set of system call.
● Files can be specifies by a character string called as path name.
● Each Pathname is unique and it is converted to an Inode.
Directories or Files and their Description—>
NAME DESCRIPTION
/ The slash / character alone
denotes the root of the filesystem
tree.
/bin Stands for “binaries” and contains
certain fundamental utilities, such
as ls or cp, which are generally
needed by all users.
/boot Contains all the files that are
required for successful booting
process.
/dev Stands for “devices”. Contains file
representations of peripheral
devices and pseudo-devices.
/etc Contains system-wide
configuration files and system
databases. Originally also
contained “dangerous maintenance
utilities” such as init,but these
have typically been moved to /sbin
or elsewhere.
/home Contains the home directories for
the users.
/lib Contains system libraries, and
some critical files such as kernel
modules or device drivers.
/media Default mount point for removable
devices, such as USB sticks, media
players, etc.
/mnt Stands for “mount”. Contains
filesystem mount points. These are
/lib Contains system libraries, and
some critical files such as kernel
modules or device drivers.
/media Default mount point for removable
devices, such as USB sticks, media
players, etc.
/mnt Stands for “mount”. Contains
filesystem mount points. These are
used, for example, if the system
uses multiple hard disks or hard
disk partitions. It is also often used
for remote (network) filesystems,
CD-ROM/DVD drives, and so on.
/proc procfs virtual filesystem showing
information about processes as
files.
/root The home direc tor y for the
superuser “root” – that is, the
s y s t e m a d m i n i s t r a t o r. T h i s
account’s home directory is usually
on the initial filesystem, and hence
not in /home (which may be a
mount point for another filesystem)
in case specific maintenance
needs to be performed, during
which other filesystems are not
available. Such a case could occur,
for example, if a hard disk drive
suffers physical failures and
cannot be properly mounted.
/tmp A place for temporary files. Many
systems clear this directory upon
startup; it might have tmpfs
mounted atop it, in which case its
contents do not survive a reboot,
or it might be explicitly cleared by
a startup script at boot time.
/usr Originally the directory holding
user home directories,its use has
changed. It now holds executables,
libraries, and shared resources
that are not system critical, like the
X Window System, KDE, Perl, etc.
However, on some Unix systems,
some user accounts may still have
a home directory that is a direct
subdirectory of /usr, such as the
default as in Minix. (on modern
systems, these user accounts are
contents do not survive a reboot,
or it might be explicitly cleared by
a startup script at boot time.
/usr Originally the directory holding
user home directories,its use has
changed. It now holds executables,
libraries, and shared resources
that are not system critical, like the
X Window System, KDE, Perl, etc.
However, on some Unix systems,
some user accounts may still have
a home directory that is a direct
subdirectory of /usr, such as the
default as in Minix. (on modern
systems, these user accounts are
often related to server or system
use, and not directly used by a
person).
/usr/bin This directory stores all binary
programs distributed with the
operating system not residing in /
bin, /sbin or (rarely) /etc.
/usr/include Stores the development headers
used throughout the system.
Header files are mostly used by
the #include directive in C/C++
programming language.
/usr/lib Stores the required libraries and
data files for programs stored
within /usr or elsewhere.
/var A short for “variable.” A place for
files that may change often –
especially in size, for example e-
mail sent to users on the system,
or process-ID lock files.
/var/log Contains system log files.
/var/mail The place where all the incoming
mails are stored. Users (other than
root) can access their own mail
only. Often, this directory is a
symbolic link to /var/spool/mail.
/var/spool Spool directory. Contains print
jobs, mail spools and other queued
tasks.
/var/tmp A place for temporary files which
should be preserved between
system reboots.
/var/spool Spool directory. Contains print
jobs, mail spools and other queued
tasks.
/var/tmp A place for temporary files which
should be preserved between
system reboots.
Types of Unix Files—>

Ordinary Files
An ordinary file is a file on the system that contains data, text, or program
instructions.
● Used to store your information, such as some text you have written or an
image you have drawn. This is the type of file that you usually work with.
● Always located within/under a directory file.
● Do not contain other files.
● In long-format output of ls -l, this type of file is specified by the “-” symbol.

Directories
Directories store both special and ordinary files. For users familiar with
Windows or Mac OS, UNIX directories are equivalent to folders. A directory file
contains an entry for every file and subdirectory that it houses. If you have 10
files in a directory, there will be 10 entries in the directory. Each entry has two
components. (1) The Filename (2) A unique identification number for the file or
directory (called the inode number)
● Branching points in the hierarchical tree.
● Used to organize groups of files.
● May contain ordinary files, special files or other directories.
● Never contain “real” information which you would work with (such as text).
Basically, just used for organizing files.
● All files are descendants of the root directory, ( named / ) located at the top
of the tree.
In long-format output of ls –l , this type of file is specified by the “d” symbol.

Special Files
Used to represent a real physical device such as a printer, tape drive or
terminal, used for Input/Output (I/O) operations. Device or special files are
used for device Input/Output(I/O) on UNIX and Linux systems. They appear in a
file system just like an ordinary file or a directory. On UNIX systems there are
two flavors of special files for each device, character special files and block
special files :
● When a character special file is used for device Input/Output(I/O), data is
transferred one character at a time. This type of access is called raw device
access.
● When a block special file is used for device Input/Output(I/O), data is
transferred in large fixed-size blocks. This type of access is called block
device access.
For terminal devices, it’s one character at a time. For disk devices though, raw
access means reading or writing in whole chunks of data – blocks, which are
native to your disk.
● In long-format output of ls -l, character special files are marked by the “c”
symbol.
● In long-format output of ls -l, block special files are marked by the “b”
symbol.

Pipes
UNIX allows you to link commands together using a pipe. The pipe acts a
temporary file which only exists to hold data from one command until it is read
by another.A Unix pipe provides a one-way flow of data.The output or result of
the first command sequence is used as the input to the second command
sequence. To make a pipe, put a vertical bar (|) on the command line between
two commands.For example: who | wc -l In long-format output of ls –l , named
pipes are marked by the “p” symbol.

Sockets
A Unix socket (or Inter-process communication socket) is a special file which
allows for advanced inter-process communication. A Unix Socket is used in a
client-server application framework. In essence, it is a stream of data, very
similar to network stream (and network sockets), but all the transactions are
local to the filesystem. In long-format output of ls -l, Unix sockets are marked
by “s” symbol.

Symbolic Link
Symbolic link is used for referencing some other file of the file system.Symbolic
link is also known as Soft link. It contains a text form of the path to the file it
references. To an end user, symbolic link will appear to have its own name, but
when you try reading or writing data to this file, it will instead reference these
operations to the file it points to. If we delete the soft link itself , the data file
would still be there.If we delete the source file or move it to a different location,
symbolic file will not function properly. In long-format output of ls –l , Symbolic
link are marked by the “l” symbol (that’s a lower case L).

Advantages of the Unix file System


● Hierarchical organization: The hierarchical structure of the Unix file
system makes it easy to organize and navigate files and directories.
● Robustness: The Unix file system is known for its stability and reliability. It
can handle large amounts of data without becoming unstable or crashing.
● Security: The Unix file system uses a set of permissions that allows
administrators to control who has access to files and directories.
● Compatibility: The Unix file system is widely used and supported, which
means that files can be easily transferred between different Unix-based
systems.

Disadvantages of the Unix file System


● Complexity: The Unix file system can be complex to understand and
manage, especially for users who are not familiar with the command line
interface.
● Steep Learning Curve: Users who are not familiar with Unix-based
systems may find it difficult to learn how to use the Unix file system.
● Lack of User-Friendly Interface: The Unix file system is primarily
managed through the command line interface, which may not be as user-
friendly as a graphical user interface.
● Limited Support for Certain File Systems: While the Unix file system is
compatible with many file systems, there are some file systems that are not
fully supported.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

File Naming Conventions in UNIX—>
UNIX follows flexible and case-sensitive rules for naming files. Below are the
key conventions:

1. General Rules for Naming Files in UNIX—>


Allowed Characters:
● Letters (A-Z, a-z)
● Numbers (0-9)
● Special characters (_, ., -)
Avoid Special Characters Like:
● / → Reserved as a directory separator
● *, ?, [], {} → Used in shell wildcards
● $, &, |, !, ;, > → Have special meanings in the shell

2. Naming Best Practices


● Use Descriptive Names → Example: project_report.txt instead of p.txt
● Avoid Spaces → Use underscores _ or hyphens - (e.g., file_name.txt or
file-name.txt)
● Keep Names Short → Avoid overly long names
● Use Lowercase Letters → Consistency and ease of access
● Use Extensions → .txt, .sh, .log (not mandatory but helpful)
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Parent-Child Relationship in UNIX—>
In Unix, a "parent-child relationship" refers to the connection between a
process that creates another process, called the "parent," and the newly
created process, called the "child," which is established using the "fork()"
system call; essentially, whenever a process creates a new process, the original
process becomes the parent and the new one becomes the child.

Key points about the parent-child relationship in Unix:


● Parent Process → A process that creates another process.
● Child Process → A process spawned by a parent process using the fork()

system call.
● Process ID (PID) → Unique identifier for each process.


● Parent Process ID (PPID) → The ID of the parent process.

Example usage:
● Shell commands:
When you run a command in a shell, the shell process creates a new child
process to execute that command.

● Background processes:
By using the "&" operator, you can create a child process that runs in the
background, allowing the parent process to continue execution.
Process Creation in UNIX—>
When a process is created, it follows these steps:
. The parent process calls fork(), which duplicates the process.
. The child process runs independently of the parent.
. The child can execute a different program using exec().
. The parent waits for the child process to complete using wait().
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

HOME Variable in UNIX—>
The HOME variable in UNIX/Linux represents the home directory of the
currently logged-in user. It is an environment variable that stores the absolute
path of the user’s home directory.

Where is HOME Defined?


The HOME variable is set when a user logs in and is defined in the /etc/passwd
file.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Inode Number in UNIX/Linux—>
In UNIX/Linux, every file and directory is assigned a unique inode number. An
inode (Index Node) stores metadata about the file but not its name or data.
1. What is an Inode?
An inode is a data structure that contains information about a file, including:
File type (regular file, directory, symbolic link, etc.)
File permissions (read, write, execute)
Owner and group ID
File size
Time Stamps (creation, modification, access)
Number of hard links
Pointers to file data blocks

An inode does NOT store:


File name
File contents
The file name is stored separately in the directory structure, which maps
filenames to inode numbers.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Absolute Pathname in UNIX—>
An absolute pathname (or absolute path) is the full path to a file or directory
starting from the root directory (/).
Key Characteristics:
Starts from / (root directory).
Specifies the complete location of a file or directory.
Works independently of the current working directory.
Why Use Absolute Pathnames?
Ensures correct file access regardless of location.
Useful in scripts & automation to avoid dependency on the working
directory.
Avoids confusion when running commands from different locations.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

1. What is a Relative Pathname?
A relative pathname (or relative path) is the location of a file or directory
relative to the current working directory (PWD).
Key Characteristics:
Does NOT start with / (root).
Depends on the current directory.
Shorter and flexible compared to absolute paths.
Why Use Relative Paths?
Shorter commands (less typing).
More flexible (works for scripts running from different locations).
Easier for navigating within directories.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Symbol Meaning Example Effect
. Current ./script.sh Runs script.sh in
directory the current
directory
.. Parent directory cd .. Moves one level
up
../ One level up ls ../ Lists files in the
parent directory
../../ Two levels up cd ../../ Moves two
directories up
directory
.. Parent directory cd .. Moves one level
up
../ One level up ls ../ Lists files in the
parent directory
../../ Two levels up cd ../../ Moves two
directories up
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Displaying Pathname of the Current Directory (pwd) in UNIX—>
1. What is pwd?
The pwd (print working directory) command in UNIX/Linux displays the
absolute pathname of the current directory you are working in.

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Changing the Current Directory (cd) in UNIX-->
The cd (change directory) command is used to navigate between directories
in UNIX/Linux.

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Creating Directories (mkdir) in UNIX—>
The mkdir (make directory) command is used to create new directories
(folders) in UNIX.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Removing Directories (rmdir) in UNIX—>
The rmdir (remove directory) command is used to delete empty directories
in UNIX/Linux.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Listing Contents of a Directory (ls) in UNIX/Linux—>
The ls (list) command is used to display the contents of a directory in UNIX.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Important File Systems in UNIX/Linux—>
UNIX follows a hierarchical file system where directories serve specific
purposes.
Directory Purpose
/bin Essential system binaries
(e.g., ls, cp, mkdir) for all
users.
/usr/bin Additional system binaries for
regular users (e.g., python,
git).
/sbin System binaries for
administrators (e.g., fdisk,
reboot).
/usr/sbin Extra admin tools (e.g.,
/usr/bin Additional system binaries for
regular users (e.g., python,
git).
/sbin System binaries for
administrators (e.g., fdisk,
reboot).
/usr/sbin Extra admin tools (e.g.,
apachectl, ufw).
/etc System configuration files
(e.g., /etc/passwd, /etc/fstab).
/dev Device files (hardware
access) like disks, USB (/dev/
sda).
/lib System libraries needed by
binaries in /bin and /sbin.
/usr/lib Libraries for programs in /usr/
bin and /usr/sbin.
/usr/include C/C++ header files for
software development.
/usr/share/man Manual (man) pages for
commands (man ls).
/tmp Temporary files (deleted on
reboot).
/var Variable data (logs, emails,
databases, print jobs).
/home User home directories (e.g., /
home/sayan/).
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Displaying and Creating Files using cat in UNIX/Linux—>
The cat (concatenate) command is used to create, view, and concatenate
files.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Copying a File Using cp in UNIX/Linux—>
The cp (copy) command is used to copy files and directories.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Deleting a File Using rm in UNIX—>
The rm (remove) command is used to delete files and directories.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Renaming / Moving a File Using mv in UNIX—>
The mv (move) command is used to rename or move files and directories.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Paging Output Using more in UNIX/Linux—>
The more command is used to view large files page by page instead of
displaying everything at once.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Printing a File Using lp in UNIX/Linux—>
The lp (line printer) command is used to send files to the printer for printing.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Knowing File Type Using file in UNIX—>
The file command is used to determine the type of a file in UNIX.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Word and Character Counting Using wc in UNIX—>
The wc (word count) command is used to count lines, words, and characters
in a file.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Comparing Files Using cmp in UNIX—>
The cmp (compare) command is used to compare two files byte by byte and
find differences.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Finding Common Lines Between Two Files Using comm in UNIX—
>
The comm (common) command is used to compare two sorted files line by
line and find:
. Unique lines in file1
. Unique lines in file2
. Common lines in both files
Requirement: Both files must be sorted before using comm.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Displaying File Differences Using diff in UNIX—>
The diff (difference) command compares two text files line by line and shows
the differences.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Creating an Archive File Using tar in UNIX—>
The tar (tape archive) command is used to create, extract, and manage archive
files in UNIX/Linux.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Compressing Files Using gzip in UNIX—>
The gzip (GNU Zip) command is used to compress files in UNIX/Linux, reducing
their size.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Uncompressing Files Using gunzip in UNIX—>
The gunzip (GNU Unzip) command is used to decompress files that were
compressed using gzip.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Archiving and Compressing Files Using zip in UNIX—>
The zip command is used to create compressed .zip archives in UNIX/Linux.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Extracting Compressed Files Using unzip in UNIX—>
The unzip command is used to extract files from a .zip archive in UNIX/Linux.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Effect of cp, rm, and mv Commands on Directories in UNIX—>
1. cp (Copy Command) and Directories
● By default, cp does not copy directories unless the -r (recursive) option is
used.
● Example (Copy a directory and its contents):
cp -r source_directory destination_directory/
Effect: Creates a duplicate of source_directory inside

destination_directory.

2. rm (Remove Command) and Directories


● By default, rm does not remove directories unless the -r option is used.
● Example (Remove an empty directory):
rmdir directory_name
Effect: Deletes directory_name only if it is empty.
● Example (Remove a directory with files):
rm -r directory_name
Effect: Deletes directory_name and all its contents.
● Force deletion (-rf option):
rm -rf directory_name
Effect: Removes the directory without any confirmation.
3. mv (Move/Rename Command) and Directories
● Move a directory to a new location:
mv source_directory destination_directory/
Effect: Moves source_directory into destination_directory.
● Rename a directory:
mv old_directory new_directory
Effect: Renames old_directory to new_directory.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

File and Directory Attributes in UNIX—>
The ls -l command is used to list file and directory attributes in UNIX/Linux.

2. Breakdown of Attributes
Field Example Description
File Type - or d - (file), d (directory), l
(symbolic link)
Permissions rw-r--r-- Read (r), Write (w),
Execute (x) for User,
Group, and Others
Links 1 Number of hard links
Field Example Description
File Type - or d - (file), d (directory), l
(symbolic link)
Permissions rw-r--r-- Read (r), Write (w),
Execute (x) for User,
Group, and Others
Links 1 Number of hard links
to the file
Owner user Username of the file
owner
Group group Group to which the file
belongs
Size 1024 File size in bytes
Date & Time Feb 10 12:00 Last modified date and
time
File Name file.txt Name of the file or
directory
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

File Ownership in UNIX—>
Every file and directory in UNIX/Linux has an owner and belongs to a group.
Ownership defines who can access and modify a file.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

File Permissions in UNIX—>
File permissions in UNIX/Linux control who can read, write, or execute a file or
directory.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Changing File Permissions in UNIX—>
There are two ways to change file permissions:
. Relative Permission (Symbolic Mode)
. Absolute Permission (Octal Mode)
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Hard Link vs Soft Link in UNIX—>
A link in UNIX/Linux is a way to create multiple references to the same file.
Links help manage files efficiently without duplicating data.
There are two types of links:
. Hard Link – Points directly to the same inode as the original file.
. Soft Link (Symbolic Link) – Points to the file name (path), not the inode.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Significance of File Attributes for a Directory in UNIX—>
File attributes in a directory define access control and functionality, ensuring
security and proper file management. Their significance includes:
. Access Control – Permissions (r, w, x) determine who can read, modify, or
enter a directory.
. File Management – Determines whether users can list files (ls), create new
files, or delete existing ones.
. Security Enforcement – Restricts unauthorized users from modifying or
accessing sensitive data.
. User & Group Ownership – Defines which user or group has control over
directory contents.
. Navigation Control – Execute (x) permission allows users to cd into a
directory, even if they cannot list its contents.
. Preventing Accidental Deletion – If a directory lacks w permission, files
inside cannot be deleted or modified.
. File System Integrity – Ensures only authorized processes can alter
system-critical directories (e.g., /etc, /var).
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Default Permissions of Files and Directories & Using umask in
UNIX—>
1. Default Permissions of Files and Directories
When a new file or directory is created, it is assigned default permissions
based on the system's default settings.
● Default File Permissions → rw-r--r-- (Readable & writable by the owner,
readable by others)
● Default Directory Permissions → rwxr-xr-x (Owner has full access, others
can read & enter)
However, these are not absolute defaults—they are determined by a setting
called umask.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Changing File Timestamps with touch Command in UNIX—>
The touch command is commonly used to update file timestamps or create new
empty files. You can modify the access time (atime) and modification time
(mtime) of a file using touch.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

File Locating with the find Command in UNIX—>
The find command is one of the most powerful tools for searching files and
directories on your system. It allows you to search based on various criteria like
file name, type, size, permissions, and timestamps.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Interpretive Cycle of the Shell in UNIX—>
The interpretive cycle refers to the sequence of steps that the shell follows to
interpret and execute commands that a user inputs into the terminal. The shell
acts as an interface between the user and the operating system, translating
user commands into actions that the operating system can understand and
execute.
The interpretive cycle is often described as a sequence of steps that the shell
follows to process a command. Here's an overview of the interpretive cycle of
the shell:
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Types of Shells in UNIX—>
In UNIX and Linux systems, the shell is a program that provides an interface for
users to interact with the operating system. It interprets and executes user
commands. Different types of shells offer varied features, scripting capabilities,
and user interfaces.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Pattern Matching in UNIX/Linux
Pattern matching is a mechanism used in UNIX/Linux systems to match files or
strings based on a specific pattern, often using wildcards or regular
expressions. It allows users to search for specific files, strings, or content
within files efficiently.
There are two primary types of pattern matching in UNIX/Linux systems:
1. Wildcard Pattern Matching (Globbing)
Wildcards are characters that allow you to match file names or parts of file
names in the terminal. The most common wildcards are:
● * (Asterisk):
○ Matches any string of characters, including an empty string.
○ Example: *.txt will match all files that end with .txt.
● ? (Question Mark):
○ Matches exactly one character.
○ Example: file?.txt will match file1.txt, fileA.txt, but not file10.txt.
● [] (Square Brackets):
○ Matches any one character within the specified range or set of
characters.
○ Example: file[1-5].txt will match file1.txt, file2.txt, ..., file5.txt.
● [!...] or [^...] (Negation in Square Brackets):
○ Matches any character except those within the brackets.
○ Example: file[!1-5].txt will match files like fileA.txt, fileX.txt, but not
file1.txt, file2.txt, etc.

2. Regular Expressions (Regex) Pattern Matching


Regular expressions provide a more powerful and flexible way to perform
pattern matching, especially when dealing with strings and text within files.
Regular expressions are supported by various command-line utilities, such as
grep, sed, awk, etc.
Some of the most commonly used regular expression features are:
● . (Dot):
○ Matches any single character except a newline.
○ Example: a.b will match aab, acb, a1b, etc.
● * (Asterisk):
○ Matches zero or more of the preceding element.
○ Example: ab*c will match ac, abc, abbc, abbbc, etc.
● ^ (Caret):
○ Matches the start of a string.
○ Example: ^abc will match abc at the beginning of the string.
● $ (Dollar Sign):
○ Matches the end of a string.
○ Example: abc$ will match abc at the end of the string.
● [] (Square Brackets):
○ Matches any one of the characters inside the brackets.
○ Example: [aeiou] matches any vowel.
● [^] (Negated Square Brackets):
○ Matches any character that is not inside the brackets.
○ Example: [^0-9] matches any character that is not a digit.
● {n,m} (Curly Braces):
○ Matches the preceding character or group at least n times, but not
more than m times.
○ Example: a{2,4} will match aa, aaa, or aaaa.
● | (Pipe):
○ Acts as a logical OR operator between patterns.
○ Example: abc|xyz will match abc or xyz.
● () (Parentheses):
○ Used to group patterns together.
○ Example: (abc|xyz)123 will match abc123 or xyz123.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Escaping in UNIX—>
Escaping is the process of treating special characters as literal characters
instead of having them perform their usual function. In UNIX/Linux systems,
certain characters have special meanings (e.g., spaces, quotes, backslashes,
and others), and sometimes you want to include them as part of a string rather
than use their special function. In such cases, you can "escape" these
characters to prevent them from being interpreted by the shell or other
programs.

Escaping Mechanism
In UNIX/Linux, the backslash (\) is the most commonly used escape character. It
is used to escape characters that would otherwise have special meanings.
Common Use Cases for Escaping:
. Escaping Special Characters:
○ If you want to include characters like spaces, parentheses, or dollar
signs in a string, you use the backslash (\).
. Examples:
○ Space: My\ File.txt (This represents a file with a space in its name: My
File.txt)
○ Dollar Sign: \$5 (This prevents the shell from interpreting $5 as a
variable.)
○ Parentheses: \(\) (This prevents the shell from interpreting the
parentheses as grouping symbols.)
. Escaping Quotes:
○ If you want to include single quotes (') or double quotes (") inside a
string surrounded by the same type of quote, you need to escape the
quote.
. Examples:
○ Single Quote Inside Single Quote: 'It\'s a good day'
○ Double Quote Inside Double Quote: "He said, \"Hello!\""
. Escaping Backslashes:
○ To include a backslash (\) in your string, you need to escape it with
another backslash (\\).
. Example:
○ C:\\Windows\\System32

Escaping in Regular Expressions


In regular expressions (regex), certain characters like *, +, ?, ., [ and others are
special, so if you want to match them literally, you need to escape them.
Examples in Regex:
● To match a literal period (.), you would escape it as \..
● To match an asterisk (*), you would escape it as \*.
Example:
● \d matches any digit, but to match a literal \, you would use \\.

Escaping in Shell Commands


When using special characters in shell commands, escaping helps you avoid
shell interpretation or prevent arguments from being split or treated as
wildcards.
Examples:
● Using echo:
○ echo "This is a \$5 bill" will print This is a $5 bill, because the backslash
escapes the dollar sign.
● Using find command:
○ If you're looking for a file with spaces in its name, you can escape the
spaces using backslashes:
find /path/to/directory -name "My\ File.txt"

● Using ls to list a file with special characters:


○ ls My\ File.txt will list My File.txt, where the backslash escapes the
space character.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Quoting in UNIX Shell—>
Quoting in UNIX/Linux is used to handle special characters and prevent the
shell from interpreting them as commands or operators. There are three types
of quoting methods:
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

Redirection, Standard Input, Standard Output, and Standard Error in UNIX/
Linux
In UNIX/Linux, processes interact with the system through three standard data
streams:
. Standard Input (stdin - file descriptor 0) → Takes input from the
keyboard or a file.
. Standard Output (stdout - file descriptor 1) → Displays output to the
terminal.
. Standard Error (stderr - file descriptor 2) → Displays error messages to
the terminal.
Redirection allows users to modify the default behavior of these streams by
sending output to files, reading input from files, and redirecting errors.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

/dev/null – The Black Hole of UNIX—>
What is /dev/null?
● /dev/null is a special file in UNIX/Linux that discards all data written to it.
● It acts like a "black hole"—anything sent to it is permanently lost.
● If you try to read from /dev/null, it always returns empty.

Uses of /dev/null
1. Discarding Unwanted Output—>
If you don’t want to display the output of a command, redirect it to /dev/null.
ls > /dev/null
● The output of ls is discarded.

2. Suppressing Error Messages—>


To discard only error messages (stderr) while keeping normal output:
ls non_existing_file 2> /dev/null
● The error message is ignored, but any valid output is displayed.

3. Suppressing Both Output and Errors—>


To discard both stdout and stderr, use:
command &> /dev/null
or
command > /dev/null 2>&1
● No output or error messages appear on the screen.

4. Using /dev/null in Shell Scripts—>


Sometimes, you might want to execute a command but prevent any output.
if ping -c 1 example.com > /dev/null 2>&1; then
echo "Internet is working"
else
echo "No internet connection"
fi
● The script checks if ping works but hides the output.

5. Running Commands Silently in the Background—>


If you want to run a command without seeing any output, redirect it to /dev/
null.
nohup myscript.sh > /dev/null 2>&1 &
● The script runs in the background, and output is discarded.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

/dev/tty – User’s Terminal in UNIX/Linux
What is /dev/tty?
● /dev/tty represents the current terminal (teletypewriter) used by the
logged-in user.
● It allows interaction directly with the terminal, even when output is
redirected elsewhere.
● If multiple terminal sessions exist, /dev/tty refers to the active session.

Uses of /dev/tty
1. Sending Messages to the User's Terminal—>
Even if output is redirected, you can force display on the terminal using /dev/
tty.
echo "Hello, User!" > /dev/tty
● This ensures the message appears only in the user's terminal.
2. Identifying Your Current Terminal—>
To check which terminal you're using:
tty
● Example output:
/dev/pts/0

● This means you are using a pseudo-terminal (SSH session or terminal


emulator).

3. Asking for Input in Scripts (Even If Redirected)—>


If a script needs user input but its standard input is redirected, /dev/tty ensures
interaction.
read -p "Enter password: " password < /dev/tty
● This forces the prompt to appear in the terminal, even if input is redirected.

4. Securely Prompting for Sensitive Information—>


If you want to read passwords securely (instead of using standard input):
stty -echo # Disable input echoing
read -p "Enter secret: " secret < /dev/tty
stty echo # Re-enable input echoing
● This prevents the entered text from being displayed on the screen.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

What is Command Substitution?
Command substitution allows executing a command and using its output in
another command or assigning it to a variable.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

What are Shell Variables?
Shell variables are used to store values (such as text, numbers, or command
output) that can be used later in scripts or commands.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺

What is a Process in UNIX?
A process is an instance of a running program in UNIX/Linux. Every command
or script executed in UNIX creates a process.

Types of Processes in UNIX—>


Type Description
Foreground Process Runs actively and requires user
interaction.
Background Process Runs in the background
without user interaction (&
used).
Daemon Process System process that runs
continuously (e.g., cron, sshd).
Zombie Process A process that has completed
execution but still remains in
the process table.
Orphan Process A child process whose parent
has terminated.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Display Process Attributes Using ps Command in UNIX—>
The ps (Process Status) command in UNIX/Linux is used to view
running processes and their attributes such as Process ID (PID),
Parent Process ID (PPID), CPU usage, memory usage, and execution
time.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Displaying System Processes in UNIX—>
System processes in UNIX/Linux can be viewed using different variations of the
ps command. These processes run in the background and are often managed
by the system.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Process Creation Cycle in UNIX
A process in UNIX/Linux is an instance of a running program. The process
creation cycle involves several key steps, starting from initiation to
termination.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Shell Creation Steps in UNIX—>
The process of creating a shell in UNIX/Linux involves multiple steps, starting
from system initialization to providing a user with an interactive shell. The main
steps are:
init → getty → login → shell
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Process States Overview
Processes can be in different states, depending on what stage of execution
they are in. These are the common process states:
. Running (R)
○ The process is either running or ready to run (waiting for CPU

time).
○ The process is actively executing instructions or is waiting to

execute.
. Sleeping (S)
○ The process is waiting for some event, like user input or I/O

operation. It is in a non-interruptible state until the event occurs.


○ Two types of sleeping processes:
◆ Interruptible Sleep (S): The process is waiting for an event,

but it can be woken up by signals.


◆ Uninterruptible Sleep (D): The process is waiting for a system

event (like disk access) and cannot be interrupted by signals.


. Stopped (T)
○ The process is stopped, usually by a signal (like SIGSTOP or

SIGTSTP).
○ It can be resumed later.

. Zombie (Z)
○ Zombie state occurs when a child process has completed

execution but still has an entry in the process table.


○ This happens because the parent process has not read the exit

status of the child process using wait() or waitpid().


○ Zombies are dead processes that haven't been cleaned up yet.

They don't consume CPU or memory but still occupy a Process ID


(PID) in the process table.
○ If many zombies accumulate, the system can run out of PIDs.

. Defunct (D)
○ Sometimes used interchangeably with Zombie, the process is not

executing but has not been completely removed from the process
table.

Zombie Process (State Z)


What is a Zombie Process?
A Zombie process is a process that has finished execution but still has an
entry in the process table. It is essentially a dead process that hasn’t been
fully cleaned up because the parent process hasn't retrieved the exit status of
the child process.
Why Does a Zombie Process Exist?
● After a process terminates, it sends an exit status code to its parent
process.
● If the parent does not call wait() or waitpid() to retrieve this exit status,
the child process remains in the zombie state.
● This is typically a short-lived state, and the system will clean it up
once the parent process acknowledges it.
Example of Zombie Process:
● Consider a program where a child process executes and then exits, but
the parent process doesn’t wait for it, leaving the child in a zombie
state.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Background Jobs in UNIX—>
In UNIX/Linux, background jobs allow you to run processes in the background
while continuing to work on other tasks in the foreground. You can manage
background jobs using different operators and commands like the & operator
and the nohup command.

& Operator (Running Jobs in the Background)—>


The & operator allows you to run a command or program in the background.
When you add & at the end of a command, the shell immediately executes the
command in the background and returns the control to the user.

Syntax:
command &
Example:
sleep 100 &
This runs the sleep command for 100 seconds in the background. The shell will
immediately return the prompt, allowing you to run other commands.

nohup Command (Running Jobs Without Hanging Up)—>


The nohup command (short for "no hang up") allows a process to run even if
the user logs out or the terminal is closed. It prevents the process from being
terminated when the session ends.

Syntax:
nohup command &

How nohup Works:


● When a command is run with nohup, it ignores the SIGHUP (hangup signal)
that would normally terminate it when the user logs out.
● By default, nohup sends the output of the command to a file called
nohup.out in the current directory.

Example:
nohup sleep 1000 &
This command will run the sleep command for 1000 seconds in the
background, and the output will be saved to nohup.out.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Sending Jobs to Background and Foreground in UNIX/Linux
In UNIX/Linux, you can manage running processes by placing them in the
background or bringing them to the foreground. This allows you to continue
using the terminal while a process runs in the background or bring it back into
the foreground to interact with it.
Background Jobs (bg Command)
● bg is used to resume a suspended job in the background, allowing it to
continue running while freeing up the terminal for other tasks.
● If a job is paused (stopped), you can send it to the background using the bg
command.
Foreground Jobs (fg Command)
● fg is used to bring a background job back into the foreground, allowing
you to interact with it directly.
Job Control Commands Overview:
● &: Starts a command in the background.
● bg: Resumes a stopped job in the background.
● fg: Brings a background job to the foreground.

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Environment Variables in UNIX—>
Environment variables are variables that are defined outside the application or
script and are available system-wide for programs to use. These variables store
important information about the system environment and configuration, such as
the user’s home directory, system paths, and terminal settings.
Usage of Environment Variables:—>
. Configuring System Behavior:
○ Environment variables like PATH, TERM, and HOME influence how the
shell and applications behave, such as where to search for commands
or how to display output.
. Customizing the Command Prompt:
○ By modifying PS1 and PS2, you can change the look of your terminal
prompt, adding useful information like the current directory, username,
or system name.
. System Info for Applications:
○ Programs can access environment variables like USER, LOGNAME, and
HOME to get the current user's information or file paths.
. Storing System Configuration:
○ Many applications rely on environment variables for configuration, such
as TERM for terminal settings or PATH for locating commands.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Searching Patterns using grep:
The grep command is used to search for a specific pattern in a file.
Syntax:
grep 'pattern' file_name
Example:
grep 'hello' file.txt # Searches for the word "hello" in file.txt
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Basic Regular Expression (BRE), Extended Regular Expression
(ERE), and egrep:—>
● Basic Regular Expression (BRE): Used with grep to perform pattern
matching. The syntax for patterns is simpler (e.g., * is used as \*).
● Extended Regular Expression (ERE): Offers more advanced pattern
matching (e.g., * works without escaping, | for OR).
● egrep: An extended version of grep that supports ERE.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
Using grep -E (Extended Regex with grep)—>
The grep -E option allows the use of extended regular expressions (ERE)
directly with grep.
Example:
grep -E 'a|b' file.txt # Searches for lines containing 'a' or 'b' in file.txt
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸺
⸻⸻⸻
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—

⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—
Essential Duties of a UNIX System Administrator—>
A UNIX system administrator is responsible for managing and maintaining
UNIX-based systems. Their primary duties include:
. System Maintenance
○ Installing, updating, and configuring UNIX/Linux operating systems.
○Applying security patches and system updates.
. User Account Management
○ Creating, modifying, and deleting user accounts.
○ Managing user groups, passwords, and permissions.
. File System Management
○ Monitoring disk usage and managing disk quotas.
○ Maintaining file systems and partitions.
. Process Management
○ Monitoring system processes using commands like ps, top, and kill.
○ Managing system performance and load balancing.
. System Security
○ Implementing user authentication and access control.
○ Configuring firewalls and monitoring security logs.
. Backup and Recovery
○ Setting up automated backups using tools like tar, rsync, or cron jobs.
○ Restoring system data in case of failures.
. Network Configuration
○ Managing network interfaces and troubleshooting connectivity issues.
○ Configuring services like SSH, FTP, and DNS.
. System Startup and Shutdown
○ Controlling system boot processes using init, systemd, or rc scripts.
○ Gracefully shutting down or rebooting the system using shutdown,
reboot, or halt.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—
Starting and Shutting Down UNIX—>
. Starting UNIX System
○ The system starts with the boot loader (like GRUB).
○ The kernel initializes hardware and system processes.
○ The init/systemd process starts user services and login prompts.
. Shutting Down UNIX System
○ shutdown -h now → Immediately shuts down the system.
○ shutdown -r now → Reboots the system.
○ halt → Stops the system.
○ reboot → Restarts the system.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—
User Account Management—>
UNIX user accounts control access to system resources. Admins manage users
using commands like useradd, passwd, and usermod.
. Username
○ Each user has a unique login name.
○ Stored in /etc/passwd.
. Password
○ Passwords are encrypted and stored in /etc/shadow.
○ Changed using the passwd command.
. Home Directory
○ Default directory assigned to a user (e.g., /home/username).
○ Created when the user account is added.
. Group ID (GID)
○ Users belong to groups for permission control.
○ Groups are managed in /etc/group.
. Disk Quota
○ Limits the amount of disk space a user can use.
○ Managed using quota commands.
. Terminal
○ Defines how a user interacts with the system (e.g., /dev/tty1).
○ Can be checked using tty command.
⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻⸻—

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy