0% found this document useful (0 votes)
12 views109 pages

Unix

The document provides an overview of the UNIX architecture, detailing its core components including the kernel, shell, file system, processes, system utilities, and user applications. It also explains various UNIX commands such as cal, date, echo, bc, passwd, who, uname, tty, and discusses file types, naming conventions, and the parent-child process relationship. Additionally, it covers concepts like the HOME environment variable and inode numbers, emphasizing their roles in file management and system operations.

Uploaded by

waxeron854
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)
12 views109 pages

Unix

The document provides an overview of the UNIX architecture, detailing its core components including the kernel, shell, file system, processes, system utilities, and user applications. It also explains various UNIX commands such as cal, date, echo, bc, passwd, who, uname, tty, and discusses file types, naming conventions, and the parent-child process relationship. Additionally, it covers concepts like the HOME environment variable and inode numbers, emphasizing their roles in file management and system operations.

Uploaded by

waxeron854
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/ 109

The architecture of UNIX is designed with simplicity and modularity in mind, which makes it

highly efficient and adaptable. It is primarily composed of the following layers:

1. Kernel:
o This is the core of the operating system, responsible for interacting directly with
the hardware.
o It manages resources such as memory, processes, file systems, and devices.
o The kernel handles system calls from applications, providing services like process
scheduling, input/output operations, and network management.
2. Shell:
o The shell acts as an interface between the user and the kernel.
o It interprets commands entered by the user and translates them into actions
performed by the kernel.
o Examples of shells include bash, zsh, and csh.
3. File System:
o UNIX organizes data hierarchically within a unified file system.
o Everything in UNIX is treated as a file, whether it’s regular data, directories, or
devices.
4. Processes:
o Processes are instances of running programs, managed by the kernel.
o UNIX allows multitasking, where multiple processes can run concurrently, each
with its own unique identifier (PID).
5. System Utilities and Tools:
o UNIX provides a wide array of utilities for file management, text processing,
networking, and more.
o These tools interact with the kernel and shell, enabling users to accomplish
various tasks.
6. User Applications:
o Applications run on top of the shell, utilizing system utilities and making calls to
the kernel when needed.

The cal command in UNIX is used to display a calendar. Here's how it works:

1. Basic Usage:
o Just type cal in the terminal to see the current month’s calendar.
2. Display a Specific Year:
o Use cal followed by a year to view the calendar for that entire year. Example:
cal 2025
3. Display a Specific Month and Year:
o Provide both the month and year to see the calendar for that particular month.
Example: cal 3 2025 (This shows March 2025).
4. Options:
o -3: Displays the previous, current, and next month. Example: cal -3
o -m: Starts the week on Monday. Example: cal -m
o -y: Displays the entire current year. Example: cal -y

The date command in UNIX is used to display or set the system date and time. Here's how you
can use it:

1. Display the Current Date and Time:


o Simply type date in the terminal.
o Example output:

bash

Fri Mar 28 13:50:00 IST 2025

2. Format the Output:


o You can format the output using specific options.
o Example: date "+%Y-%m-%d" will display the date in YYYY-MM-DD format.
3. Set the System Date and Time (requires admin privileges):
o Example: sudo date mmddhhmiyy
 mm: Month
 dd: Day
 hh: Hour
 mi: Minute
 yy: Year
o Example: sudo date 0328135025 sets the date and time to March 28, 13:50,
2025.
4. Display UTC Time:
o Use the -u option to display the Coordinated Universal Time (UTC).
o Example: date -u

The echo command in UNIX is used to display messages or output text to the terminal. It's
simple yet highly versatile. Here's how you can use it:

1. Basic Usage:
o To display a message, just type echo followed by the text in quotes.
o Example:

bash

echo "Hello, world!"

Output: Hello, world!

2. Displaying Variables:
o You can use echo to show the value of shell variables.
o Example:

bash

echo $HOME

Output: /home/username

3. Adding Special Characters:


o Use options to interpret escape sequences like newline (\n) and tab (\t).
o Example:

bash

echo -e "Hello\nWorld!"

Output:

Hello
World!

4. Redirecting Output:
o Combine echo with redirection to send the output to a file.
o Example:

bash

echo "This is a test" > file.txt

5. Suppressing Newline:
o To avoid adding a newline at the end, use the -n option.
o Example:

bash

echo -n "Hello, world!"


The bc command in UNIX is a powerful calculator tool that operates as an arbitrary-precision
calculator language. Here's how it works:

1. Basic Usage:
o Launch bc by simply typing bc in the terminal.
o You can then perform basic arithmetic operations like addition, subtraction,
multiplication, and division. Example:

bash

$ bc
5 + 3
8

2. Exiting bc:
o To exit, type quit or press Ctrl+D.
3. Using bc for Floating-Point Calculations:
o By default, bc doesn’t handle floating-point numbers unless you specify the scale
(number of decimal places). Example:

bash

$ echo "scale=2; 5/3" | bc


1.66

4. Advanced Math:
o You can use mathematical operations like powers (^), square roots, and more by
enabling the math library with the -l option. Example:

bash

$ echo "sqrt(16)" | bc -l
4

5. Script Integration:
o The bc tool is often used in shell scripts for performing calculations. Example:

bash

result=$(echo "scale=2; 20.5 * 3.14" | bc)


echo $result
The passwd command in UNIX is used to change user passwords. Here's how it works:

1. Changing Your Password:


o Type passwd in the terminal and press Enter.
o You’ll be prompted to:
 Enter your current password.
 Enter your new password.
 Confirm the new password by entering it again.
2. Changing Another User’s Password (requires administrative privileges):
o Use the passwd command followed by the username. Example:

bash

sudo passwd username

o You’ll be prompted to enter the new password for the specified user.
3. Password Guidelines:
o Passwords should be strong (mix of letters, numbers, and symbols).
o Some UNIX systems enforce complexity rules, such as minimum length or
avoiding dictionary words.
4. Locking or Unlocking Passwords:
o Lock a user’s account:

bash

sudo passwd -l username

o Unlock a user’s account:

bash

sudo passwd -u username

The who command in UNIX is used to display information about users who are currently logged
into the system. Here's how it works:

1. Basic Usage:
o Simply type who in the terminal to list all currently logged-in users.
o Example output:

bash
username tty1 2025-03-28 13:45

This shows:

 username: The name of the user.


 tty1: The terminal they're logged in from.
 2025-03-28 13:45: The date and time of login.
2. Display Detailed Information:
o Use who -H to include column headers for better readability.
o Example output:
o NAME LINE TIME IDLE PID COMMENT
o username tty1 2025-03-28 13:45 ...
3. Check User's IP Address:
o Use who combined with other commands to find login details, such as IP
addresses, on remote logins.
4. Monitoring System:
o Combine the who command with other tools like grep to filter specific user
information.

The uname command in UNIX is used to display system information. Here's how it works:

1. Basic Usage:
o Run uname to display the name of the operating system.
o Example output:

bash

Linux

2. Display Detailed Information:


o Use options to get specific details:
 -a: Shows all system information. Example:

bash

uname -a

Output might include kernel name, version, machine hardware, etc.

 -s: Displays the kernel name. Example:


bash

uname -s

 -r: Displays the kernel release. Example:

bash

uname -r

 -v: Displays the kernel version. Example:

bash

uname -v

 -m: Displays the machine hardware name. Example:

bash

uname -m

 -n: Displays the network node hostname. Example:

bash

uname -n

The tty command in UNIX is used to display the file name of the terminal that is connected to
the standard input. Here's how it works:

1. Basic Usage:
o Type tty in the terminal.
o Example:

bash

$ tty
/dev/tty1

This output (/dev/tty1) represents the terminal device file.

2. Purpose:
o It is useful for identifying which terminal you are currently working on, especially
in multi-terminal environments.
3. Practical Applications:
o Debugging scripts or programs that interact with terminal devices.
o Checking terminal connectivity in various system operations.

In UNIX, the file system is a vital component that organizes and manages data storage. It
ensures that data is stored efficiently and is easily accessible. Here's an overview:

1. Hierarchy:
o The UNIX file system is organized as a hierarchical tree structure, with the root
directory / at the top.
o All files and directories branch out from this root.
2. Types of Files:
o Regular files: Contain data, like text or binaries.
o Directories: Containers that hold other files and directories.
o Special files: Represent hardware devices like /dev/sda.
o Links: Shortcuts or references to other files (hard and symbolic links).
o Sockets and Named Pipes: Enable inter-process communication.
3. Key Directories:
o /bin: Essential system binaries.
o /etc: Configuration files.
o /home: User home directories.
o /var: Logs and variable data.
o /dev: Device files.
4. File Permissions:
o UNIX uses a permission system with read (r), write (w), and execute (x) access
for the owner, group, and others.
o Permissions can be managed with commands like chmod and chown.
5. File System Features:
o Supports large file sizes and efficient data handling.
o Ensures data security through permissions and ownership.
o Allows mounting and unmounting of external file systems (e.g., USB drives,
remote shares).
6. Common Commands:
o ls: List files in a directory.
o cd: Change directory.
o mkdir: Create a new directory.
o rm: Remove files or directories.
o df: Show disk space usage.
o du: Display the space used by files and directories.

In UNIX, files are categorized based on their function and characteristics. Here are the primary
types of files:

1. Regular Files:
o Contains data, such as text, binary, or executable code.
o These are the most common files used to store user or system information.
o Example: Text files (.txt), image files (.png), executable files.
2. Directories:
o Special files that contain a list of other files and directories.
o They act as containers for organizing files into a hierarchical structure.
3. Special Files:
o UNIX treats hardware devices (like disks and printers) as files.
o These files can be further divided into:
 Character Special Files: Handle data character by character, like
keyboards.
 Block Special Files: Handle data in blocks, like hard drives.
4. Links:
o References or shortcuts to other files.
o Hard Links: Directly point to the file’s data on the disk.
o Symbolic Links: Point to the file’s path, allowing flexibility.
5. Named Pipes (FIFO):
o Allow inter-process communication by creating a pipeline for data exchange
between processes.
6. Sockets:
o Enable communication between processes, even across different systems, like
networking applications.
In UNIX, file naming conventions are quite flexible, but there are some general guidelines and
best practices to follow:

1. Permissible Characters:
o File names can include letters (A-Z, a-z), numbers (0-9), dots (.), underscores (_),
and hyphens (-).
o Avoid using special characters like *, ?, /, \, |, <, >, : as they have special
meanings in the shell.
2. Case Sensitivity:
o File names in UNIX are case-sensitive.
o For example, File.txt and file.txt are treated as two different files.
3. Length Limitations:
o File names can be up to 255 characters long (depending on the file system, e.g.,
ext4).
4. Avoid Spaces:
o While spaces are allowed, they can complicate command usage.
o Instead of my file, use my_file or my-file.
5. Meaningful Names:
o Use descriptive and meaningful names to make file identification easier.
o For example, use report_2025.pdf instead of file1.pdf.
6. Hidden Files:
o Files starting with a dot (.) are treated as hidden files.
o For example, .config is a hidden file.
7. File Extensions:
o File extensions are optional but help identify the file type (e.g., .txt for text files,
.sh for shell scripts).

The parent-child relationship in UNIX typically refers to how processes interact and are
structured in the system. Here's an explanation:

1. Parent Process:
o A process that creates one or more other processes (children).
o Created initially by the system during boot-up (e.g., init or systemd is the first
parent process).
2. Child Process:
o A process spawned by the parent process.
o Created using system calls like fork() or exec().
3. Key Characteristics:
o Each process in UNIX has a unique process ID (PID).
oThe child process inherits certain attributes from the parent, such as environment
variables and open file descriptors.
4. System Calls for Relationships:
o fork(): Creates a child process that is a copy of the parent.
o exec(): Replaces the child process with a new executable.
o wait(): Parent waits for the child to finish execution.
o exit(): Terminates a process.
5. Communication:
o Parent and child processes communicate via pipes, signals, or shared memory.
6. Hierarchy:
o UNIX processes form a hierarchy, and the relationships can be viewed using
commands like:
 ps -e --forest (displays process tree).
 pstree (visualizes the hierarchy of processes).

The HOME variable in UNIX is an environment variable that specifies the absolute path to the
current user's home directory. Here's a quick overview:

1. Purpose:
o It provides a convenient way to reference the user's home directory.
o The home directory is where user-specific files and configurations are stored.
2. Checking the Value of HOME:
o You can display the value of the HOME variable using the echo command.
Example:

bash

echo $HOME

Output might look like /home/username.

3. Usage in Commands and Scripts:


o The HOME variable is used to simplify navigation and file operations. Example:

bash

cd $HOME

This command takes you directly to your home directory.


4. Setting or Changing HOME:
o While it's typically set automatically during login, you can manually change it.
Example:

bash

export HOME=/new/home/directory

o Be cautious when changing HOME, as many applications rely on it.


5. Applications:
o Programs and scripts use HOME to access user-specific settings or create temporary
files.

In UNIX, an inode number is a unique identifier assigned to each file or directory in the file
system. It plays a crucial role in how the system manages and organizes data. Here's a
breakdown:

1. What is an Inode?:
o An inode is a data structure that stores metadata about a file or directory.
o It contains information such as:
 File type (regular file, directory, etc.)
 File size
 Permissions
 Ownership (user and group IDs)
 Timestamps (creation, modification, and access times)
 Pointers to the file's data blocks on disk
2. Inode Number:
o Each file or directory is associated with an inode number.
o This number is an index into the inode table, which stores all the inodes for the
file system.
3. Viewing the Inode Number:
o Use the ls command with the -i option to display inode numbers. Example:

bash

ls -i

Output:

12345 file1.txt
12346 file2.txt
oHere, 12345 and 12346 are inode numbers.
4. Importance:
o Inode numbers allow the operating system to locate files without relying on file
names.
o A file name is merely an entry in a directory that points to an inode.
5. Hard Links and Inodes:
o Multiple file names can point to the same inode, which is how hard links are
implemented.
6. Limitations:
o Each file system has a fixed number of inodes. If all inodes are used up, no new
files can be created, even if there is free disk space.

An absolute pathname in UNIX specifies the exact location of a file or directory in the file
system hierarchy. It starts from the root directory (/) and provides the full path to the desired file
or directory, ensuring there’s no ambiguity. Here's what you need to know:

1. Structure:
o Begins with / (the root directory).
o Includes all parent directories in the hierarchy, separated by slashes (/), leading to
the target file or directory.
2. Examples:
o /home/username/documents/file.txt: This points to file.txt inside the
documents folder of the username directory, starting from the root.
o /etc/passwd: Refers to the passwd file in the etc directory.
3. Key Features:
o It is universal and works regardless of the current directory.
o Always begins with / to signify the root.
4. Difference from Relative Pathname:
o A relative pathname specifies a file location relative to the current working
directory.
o Example of a relative path: ./documents/file.txt (where . refers to the current
directory).
A relative pathname in UNIX specifies the location of a file or directory in relation to the
current working directory. It does not begin with the root directory (/) but rather uses relative
references such as . (current directory) and .. (parent directory).

Characteristics of a Relative Pathname:

1. Does Not Start with /:


o Example: documents/file.txt refers to file.txt located in the documents
folder within the current working directory.
2. Uses Special Symbols:
o . represents the current directory.
o .. refers to the parent directory.
o Example: ../documents/file.txt navigates up one level (parent directory) and
then accesses file.txt.
3. Practical Examples:
o ./script.sh: Runs script.sh located in the current directory.
o ../../notes.txt: Moves up two levels and accesses notes.txt.
4. Difference from Absolute Pathname:
o Absolute pathnames start from the root (/) and specify the full path.
o Relative pathnames depend on the user's current location in the directory
hierarchy.

In UNIX, the dot (.) and dotdot (..) symbols have special significance for navigating the file
system and represent relative path references. Here's their meaning and importance:

Dot (.)

 Represents the current directory.


 Useful for referencing or operating on files or directories within the current working
directory.
 Common uses:
o Running a script in the current directory:

bash

./script.sh

Here, ./ explicitly indicates that script.sh is in the current directory.

o Copying a file to the current directory:


bash

cp file.txt .

Dotdot (..)

 Represents the parent directory of the current directory.


 It allows you to move up one level in the directory hierarchy or reference files located in
the parent directory.
 Common uses:
o Moving up one level:

bash

cd ..

This takes you to the parent directory.

o Accessing a file in the parent directory:

bash

cp ../file.txt .

This copies file.txt from the parent directory to the current directory.

Significance:

 Efficiency: These symbols make navigation and file manipulation faster and easier,
especially in scripts and commands.
 Simplicity: They eliminate the need for specifying absolute paths repeatedly.
 Portability: Dot and dotdot references make scripts more adaptable across different
environments.

The pwd command in UNIX is used to display the full pathname of the current working
directory. Here's a quick guide:

1. Basic Usage:
o Simply type pwd in the terminal and press Enter.
o Example:
bash

$ pwd
/home/username/projects

Output shows the absolute path of the directory you are currently in.

2. Purpose:
o It helps you confirm your location in the file system hierarchy, especially useful
when navigating complex directory structures.
3. Options:
o -P: Displays the physical directory by resolving symbolic links. Example:

bash

pwd -P

o -L:Displays the logical directory, reflecting how you navigated there (default
behavior).

The cd command in UNIX is used to change the current working directory. Here's how it works:

1. Changing to a Specific Directory:


o Syntax:

bash

cd directory_path

o Example:

bash

cd /home/username/documents

This changes the current working directory to /home/username/documents.

2. Returning to Home Directory:


o Simply type cd without any arguments:

bash
cd

o Or use the $HOME environment variable:

bash

cd $HOME

3. Going to the Parent Directory:


o Use .. to move up one level in the directory hierarchy:

bash

cd ..

4. Staying in the Current Directory:


o Use . to refer to the current directory (though not common for cd):

bash

cd .

5. Switching to the Previous Directory:


o Use a hyphen (-) to return to the last visited directory:

bash

cd -

6. Handling Paths:
o Absolute Path: Specify the full path starting from the root (/). Example:

bash

cd /var/log

o Relative Path: Specify the path relative to the current directory. Example:

bash

cd ../downloads

7. Checking Current Directory:


o After changing directories, use the pwd command to confirm the current working
directory.
The mkdir command in UNIX is used to create new directories. Here's a guide on how to use it:

1. Basic Usage:
o Syntax:

bash

mkdir directory_name

o Example:

bash

mkdir my_folder

This creates a directory named my_folder in the current working directory.

2. Create Multiple Directories:


o You can create several directories at once by listing their names. Example:

bash

mkdir dir1 dir2 dir3

This creates dir1, dir2, and dir3.

3. Create Parent Directories:


o Use the -p option to create parent directories along with the target directory.
Example:

bash

mkdir -p parent/child/grandchild

This ensures the entire path is created, even if parent and child don’t already
exist.

4. Permissions:
o The new directory will have default permissions assigned based on the user's
umask setting.
o You can adjust permissions afterward with the chmod command if needed.
5. Error Handling:
o If the directory already exists, mkdir will show an error unless you're using -p,
which suppresses the error.
The rmdir command in UNIX is used to remove empty directories. Here's how it works:

1. Basic Usage:
o Syntax:

bash

rmdir directory_name

o Example:

bash

rmdir my_folder

If my_folder is empty, it will be removed successfully.

2. Remove Multiple Directories:


o You can remove several empty directories at once. Example:

bash

rmdir dir1 dir2 dir3

3. Error Handling:
o If the directory is not empty, rmdir will display an error and refuse to remove it.
o Example:

bash

rmdir my_folder
rmdir: failed to remove ‘my_folder’: Directory not empty

4. Using -p to Remove Parent Directories:


o The -p option removes a directory and its parent directories, if they are all empty.
Example:

bash

rmdir -p parent/child

5. Removing Non-Empty Directories:


o Use the rm command with the -r option if you need to remove directories that
contain files:

bash
rm -r directory_name

The ls command in UNIX is used to list the contents of a directory. It’s a versatile and widely
used command for navigating and managing files. Here's how it works:

Basic Usage:

 Type ls to list files and directories in the current directory. Example:

bash

ls

Output might look like:

file1.txt file2.txt dir1 dir2

Common Options:

1. Detailed Information (-l):


o Displays files and directories with details like permissions, owner, size, and
modification date.
o Example:

bash

ls -l

Output:

-rw-r--r-- 1 user group 1024 Mar 28 14:07 file1.txt

2. Including Hidden Files (-a):


o Lists all files, including hidden files (those starting with .).
o Example:

bash

ls -a

3. Recursive Listing (-R):


o Lists contents of directories and their subdirectories.
o Example:
bash

ls -R

4. Human-Readable Sizes (-lh):


o Combines detailed information (-l) with file sizes in human-readable format
(e.g., KB, MB).
o Example:

bash

ls -lh

5. Sorting by Time (-t):


o Lists files sorted by modification time (newest first).
o Example:

bash

ls -lt

6. Sorting by Size (-S):


o Lists files sorted by size (largest first).
o Example:

bash

ls -lS

Combining Options:

You can combine multiple options for customized output:

bash
ls -lha

This displays all files (including hidden), detailed information, and sizes in a readable format.

Here’s a concise overview of the important file systems in UNIX:

 /bin: Contains essential binary executables, like ls, cp, and mv, used by all users.
 /usr/bin: Stores non-essential binaries intended for general user applications.
 /sbin: Holds system administration binaries, such as ifconfig and fdisk.
 /usr/sbin: Contains non-essential system binaries for administrative tasks.
 /etc: Houses configuration files for the system and various applications.
 /dev: Contains device files representing hardware such as disks, printers, and terminals.
 /lib: Includes shared libraries required by system executables in /bin and /sbin.
 /usr/lib: Stores libraries used by programs in /usr/bin and /usr/sbin.
 /usr/include: Provides header files used during programming and software development.
 /usr/share/man: Contains manual pages for UNIX commands and programs.
 /tmp: A temporary file directory for system and user processes; often cleared on reboot.
 /var: Stores variable data like logs, mail queues, and temporary files.
 /home: Contains personal directories for individual users.

The cat command in UNIX is used to display, create, and concatenate files. Here's a brief
guide:

Displaying File Contents:

 Syntax:

bash

cat file.txt

 This outputs the entire content of file.txt to the terminal.

Creating a New File:

 Use cat to create a file and input its contents interactively:

bash

cat > newfile.txt

o Type your content, and press Ctrl+D to save and exit.

Concatenating Files:

 Combine the contents of multiple files into one:

bash
cat file1.txt file2.txt > combined.txt

Displaying File Contents Line by Line:

 Use the -n option to add line numbers to the output:

bash

cat -n file.txt

Redirecting Output:

 Redirect the output of a file to another file or command:

bash

cat file.txt > copy.txt

The cp command in UNIX is used to copy files and directories. Here's a concise guide:

Basic Usage:

 To copy a file:

bash

cp source.txt destination.txt

This duplicates source.txt as destination.txt.

Copying to a Directory:

 To copy a file into a specific directory:

bash

cp source.txt /path/to/directory/

This places the file in the specified directory, keeping the original name.

Copying Multiple Files:


 To copy multiple files to a directory:

bash

cp file1.txt file2.txt /path/to/directory/

Copying Directories:

 Use the -r (recursive) option to copy entire directories, including their contents:

bash

cp -r source_directory/ destination_directory/

Preserving Attributes:

 To preserve file attributes like permissions and timestamps, use the -p option:

bash

cp -p source.txt destination.txt

Interactive Copy:

 Use the -i option to prompt before overwriting existing files:

bash

cp -i source.txt destination.txt

Verbose Output:

 Use the -v option to display the files being copied:

bash

cp -v source.txt destination.txt

The rm command in UNIX is used to delete files and directories. Here's a quick guide:
Basic Usage:

 To delete a file:

bash

rm filename.txt

Deleting Multiple Files:

 List multiple files to delete them in one go:

bash

rm file1.txt file2.txt

Interactive Deletion:

 Use the -i option to prompt for confirmation before deleting each file:

bash

rm -i filename.txt

Deleting a Directory:

 Use the -r (recursive) option to delete a directory and all its contents:

bash

rm -r directory_name/

Force Deletion:

 The -f (force) option deletes files or directories without confirmation:

bash

rm -f filename.txt

Combining Options:

 To delete a non-empty directory without prompts:

bash
rm -rf directory_name/

The mv command in UNIX is used to rename or move files and directories. Here's a quick guide:

Renaming a File:

 Syntax:

bash

mv oldname.txt newname.txt

 This renames oldname.txt to newname.txt in the same directory.

Moving a File:

 To move a file to a different directory:

bash

mv filename.txt /path/to/destination/

 Example:

bash

mv file1.txt /home/user/documents/

Renaming and Moving Simultaneously:

 You can move a file to a new directory and rename it in the process:

bash

mv filename.txt /path/to/destination/newname.txt

Moving Multiple Files:

 To move multiple files to a directory:

bash
mv file1.txt file2.txt /path/to/destination/

Interactive Move:

 Use the -i option to prompt for confirmation before overwriting:

bash

mv -i file1.txt /path/to/destination/

Verbose Output:

 Use the -v option to see detailed progress of the move:

bash

mv -v file1.txt /home/user/

The more command in UNIX is used to view the content of a file or command output one screen
at a time, making it easier to read large amounts of text. Here's how it works:

Basic Usage:

 To page through a file:

bash

more filename.txt

 It displays the file contents one screen at a time. Press the spacebar to move forward and
the b key to move backward (if supported).

Paging Command Output:

 Combine more with other commands to paginate their output:

bash

ls -l | more
 This lets you navigate through the ls output one screen at a time.

Navigation Keys:

 Spacebar: Moves to the next page.


 Enter: Scrolls line by line.
 b: Moves back one page (if supported).
 q: Quits the more viewer.

Options:

 -d: Provides help text for navigation keys.

bash

more -d filename.txt

he lp command in UNIX is used to print files using the system's print service. Here's how it
works:

Basic Usage:

 To print a file:

bash

lp filename.txt

This sends filename.txt to the default printer.

Specifying a Printer:

 Use the -d option to specify a particular printer:

bash

lp -d printer_name filename.txt

Printing Multiple Copies:


 Use the -n option to specify the number of copies:

bash

lp -n 2 filename.txt

This prints 2 copies of the file.

Setting a Job Name:

 Use the -t option to assign a title or name to the print job:

bash

lp -t "My Print Job" filename.txt

Viewing Print Jobs:

 Use the lpstat command to check the status of print jobs:

bash

lpstat

Canceling a Print Job:

 Use the cancel command followed by the job ID:

bash

cancel job_id

The file command in UNIX is used to determine the type of a file. Here's how it works:

Basic Usage:

 To identify the type of a file:

bash
file filename

Example:

bash

file document.pdf

Output might be:

document.pdf: PDF document, version 1.4

Key Features:

1. Handles Different File Types:


o It can identify text files, binary files, directories, images, scripts, and more.
2. Working with Multiple Files:
o You can check the type of multiple files at once:

bash

file file1.txt file2.png

3. File Content Inspection:


o file examines the contents and structure of the file instead of relying on its
extension.

Options:

 -i: Displays the MIME type of the file:

bash

file -i filename

Output:

filename: text/plain; charset=us-ascii

 -b: Omits the filename in the output and only shows the file type:

bash

file -b filename
The wc command in UNIX is used for counting lines, words, and characters in a file or standard
input. Here's how it works:

Basic Usage:

 To count lines, words, and characters in a file:

bash

wc filename.txt

Example output:

10 50 250 filename.txt

This shows:

o 10: Number of lines.


o 50: Number of words.
o 250: Number of characters.

Counting Specific Elements:

1. Lines:
o Use the -l option to count only the lines:

bash

wc -l filename.txt

2. Words:
o Use the -w option to count only the words:

bash

wc -w filename.txt

3. Characters:
o Use the -m option to count the characters:

bash

wc -m filename.txt

4. Bytes:
o Use the -c option to count the bytes in a file:
bash

wc -c filename.txt

Working with Standard Input:

 Use wc in combination with other commands:

bash

echo "Hello World" | wc -w

Output:

This counts the words in the string Hello World.

Multiple Files:

 Use wc with multiple files:

bash

wc file1.txt file2.txt

The cmp command in UNIX is used to compare two files byte by byte and identify differences.
It is especially useful for binary and text file comparisons. Here's how it works:

Basic Usage:

 Syntax:

bash

cmp file1 file2

 If the files are identical, no output is displayed, and the command exits silently.
 If differences are found, cmp shows the byte and line where the first difference occurs:

bash

file1 file2 differ: byte 10, line 1


Options:

1. Show Differences Verbosely (-l):


o Displays all differing bytes and their values in octal:

bash

cmp -l file1 file2

2. Limit Number of Bytes Compared (-n):


o Compare only a specified number of bytes:

bash

cmp -n 100 file1 file2

3. Skip Bytes in Files:


o Use -i to skip a specific number of bytes in the comparison:

bash

cmp -i 10 file1 file2

4. Suppress Output (-s):


o Check only if the files are different (no output shown):

bash

cmp -s file1 file2

o This can be useful in scripts for conditional checks.

Exit Codes:

 0: Files are identical.


 1: Files are different.
 2: An error occurred

The comm command in UNIX is used to compare two sorted files line by line and display their
common and unique lines in a structured format. Here's how it works:
Basic Syntax:

bash
comm file1 file2

 It assumes both files are sorted in ascending order.


 Output is divided into three columns:
1. Lines unique to file1.
2. Lines unique to file2.
3. Lines common to both files.

Example:

Suppose you have two sorted files: file1.txt:

apple
banana
cherry

file2.txt:

banana
cherry
date

Run:

bash
comm file1.txt file2.txt

Output:

apple
date
banana
cherry

 apple is unique to file1.txt.


 date is unique to file2.txt.
 banana and cherry are common.

Options:

1. Suppress Columns:
o -1: Suppress lines unique to file1.
o -2: Suppress lines unique to file2.
o -3: Suppress common lines. Example:
bash

comm -12 file1.txt file2.txt

This displays only the common lines.

2. Case Sensitivity:
o By default, comm is case-sensitive. Ensure both files are sorted in the same case
for accurate results.

Important Notes:

 Files must be sorted before using comm. If they aren't, sort them first:

bash

sort file1.txt -o file1.txt


sort file2.txt -o file2.txt

The diff command in UNIX is used to display differences between two files line by line. It is
particularly useful for comparing text files. Here's how it works:

Basic Usage:

 Syntax:

bash

diff file1 file2

 If the files differ, diff outputs the lines that need to be changed to make the files
identical.
 If the files are identical, there is no output.

How the Output Works:

 The output shows lines that are different, using symbols:


o <: Line from the first file.
o >: Line from the second file.

Example:

Suppose file1.txt contains:

apple
banana
cherry

And file2.txt contains:

apple
blueberry
cherry

Running:

bash
diff file1.txt file2.txt

Output:

2c2
< banana
---
> blueberry

This indicates that on line 2, banana in file1.txt is replaced by blueberry in file2.txt.

Common Options:

1. Unified Format (-u):


o Produces a unified view of differences.

bash

diff -u file1 file2

2. Side-by-Side Comparison (-y):


o Displays differences side by side for easier visualization.

bash

diff -y file1 file2

3. Ignore Case (-i):


o Compares files while ignoring case differences.
bash

diff -i file1 file2

4. Suppress Output for Identical Files (-q):


o Only displays if the files differ or not.

bash

diff -q file1 file2

The tar command in UNIX is used to create, extract, and manage archive files. Here's how you
can create an archive file:

Basic Syntax for Creating an Archive:

bash
tar -cf archive_name.tar file1 file2

 -c: Creates a new archive.


 -f: Specifies the name of the archive file.

Example:

To archive file1.txt and file2.txt into archive.tar:

bash
tar -cf archive.tar file1.txt file2.txt

Including Directories:

To archive an entire directory:

bash
tar -cf archive.tar directory_name/

This includes all files and subdirectories within directory_name.


Adding Compression:

1. Gzip Compression:
o Use the -z option to compress the archive:

bash

tar -czf archive_name.tar.gz file1 file2

2. Bzip2 Compression:
o Use the -j option for better compression:

bash

tar -cjf archive_name.tar.bz2 file1 file2

Verbose Mode:

To see the files being added to the archive:

bash
tar -cvf archive.tar file1.txt file2.txt

Helpful Options:

 Excluding Files:

bash

tar --exclude="file_to_exclude.txt" -cf archive.tar file1 file2

 List Contents of an Archive:

bash

tar -tf archive.tar

The gzip command in UNIX is used to compress files, reducing their size and saving storage
space. Here's a concise guide:

Basic Usage:
 To compress a file:

bash

gzip filename.txt

This creates a compressed file named filename.txt.gz and removes the original
filename.txt.

Compress Multiple Files:

 You can compress multiple files in one command:

bash

gzip file1.txt file2.txt

Decompressing Files:

 Use the -d option or the gunzip command to decompress a file:

bash

gzip -d filename.txt.gz

Or:

bash

gunzip filename.txt.gz

Preserving the Original File:

 Use the -k option to compress a file but keep the original:

bash

gzip -k filename.txt

Compressing with Maximum Efficiency:

 Use the -9 option for maximum compression:

bash

gzip -9 filename.txt
Checking Compression Ratio:

 To see the compression ratio of a file, use:

bash

gzip -l filename.txt.gz

To uncompress a file using gunzip in a terminal, you can follow this straightforward method:

1. Open your terminal.


2. Navigate to the directory where your .gz file is located using the cd command.
3. Run the command:

bash

gunzip filename.gz

Replace filename.gz with the name of your compressed file.

To archive files into a .zip file in Unix, you can use the zip command. Here's how you can do
it:

1. Open your terminal.


2. Navigate to the directory containing the files you want to archive.
3. Run the following command:

bash

zip archive_name.zip file1 file2 file3

Replace archive_name.zip with the name you want for your zip file, and list the files
you want to include (e.g., file1, file2, etc.).
If you want to archive all the files in the current directory, you can use:

bash
zip archive_name.zip *

To extract a compressed .zip file in Unix, you can use the unzip command. Here's how:

1. Open your terminal.


2. Navigate to the directory containing the .zip file using the cd command.
3. Run the following command:

bash

unzip archive_name.zip

Replace archive_name.zip with the actual name of your zip file.

This will extract all the contents of the zip file into the current directory. If you want to extract it
to a specific folder, you can use:

bash
unzip archive_name.zip -d /path/to/destination/

The cp command in Unix is used to copy files and directories. Here's a quick idea of its effects:

1. Copies Files: It duplicates the content of a file to a new location. For example:

bash

cp source_file destination_file

2. Copies Directories: With the -r option (recursive), it copies entire directories and their
contents:

bash
cp -r source_directory destination_directory

3. Overwrites Files: By default, cp overwrites the destination file if it already exists. To


avoid this, you can use the -i option (interactive), which prompts you before overwriting.
4. Preserves File Attributes: With the -p option, it preserves file attributes such as
timestamps and permissions:

bash

cp -p source_file destination_file

The rm and mv commands in Unix are used to remove and move directories, respectively. Here's
a brief overview of their functionality:

rm Command

The rm command is used to delete files and directories. To remove a directory:

 Use the -r (recursive) option to delete the directory and all its contents:

bash

rm -r directory_name

 Add the -i option to confirm each deletion interactively:

bash

rm -ri directory_name

Warning: The rm command permanently deletes files and directories, so use it with caution!

mv Command

The mv command is used to move or rename files and directories. To move a directory:

 Run the following command:

bash

mv source_directory target_directory
Here, source_directory is the directory you want to move, and target_directory is
the destination.

To rename a directory:

 Simply specify the new name as the target:

bash

mv old_directory_name new_directory_name

To list the attributes of files and directories in Unix, you can use the ls -l command. Here's an
example:

bash
ls -l

This displays detailed information about files and directories in the current directory. The output
includes:

1. File Type and Permissions: The first column shows the file type (- for files, d for
directories) and permissions (e.g., rwxr-xr-x).
2. Number of Links: The second column indicates the number of hard links to the file or
directory.
3. Owner: The third column specifies the user who owns the file.
4. Group: The fourth column shows the group associated with the file.
5. File Size: The fifth column displays the size of the file in bytes.
6. Timestamp: The sixth column shows the last modification date and time.
7. File/Directory Name: The final column lists the name of the file or directory.

To include hidden files in the listing, use:

bash
ls -la
File ownership in Unix refers to the user and group associated with a file or directory. Here's a
breakdown of its key aspects:

1. Owner/User:
o This is the user who owns the file or directory. By default, it's the user who
created it.
o The owner has specific permissions to read, write, or execute the file.
2. Group:
o Each file or directory is associated with a group.
o Members of this group can have permissions separate from the owner.
3. Changing Ownership:
o You can change the owner of a file using the chown command:

bash

chown new_owner filename

o To change the group, use:

bash

chgrp new_group filename

File permissions in Unix determine who can access and perform actions on files or directories.
Here's a brief overview:

Types of Permissions:

1. Read (r): Allows viewing the content of a file or listing a directory's contents.
2. Write (w): Grants the ability to modify the content of a file or add/remove files in a
directory.
3. Execute (x): Enables running a file as a program or accessing a directory.

Categories of Users:

1. Owner (User): The user who owns the file.


2. Group: A set of users who share access based on group permissions.
3. Others: Everyone else not in the owner or group categories.

Viewing Permissions:
Use the ls -l command to see file permissions. For example:

-rwxr-xr--

Each group of three characters represents permissions for owner, group, and others. The initial
- indicates it's a file (or d for a directory).

Changing Permissions:

 Use the chmod command to modify permissions. For example:

bash

chmod 755 filename

Here, 755 sets read, write, and execute for the owner, and read and execute for
group/others.

 Alternatively, use symbolic notation:

bash

chmod u+x filename

When changing file permissions in Unix, you can use relative permissions or absolute
permissions. Here's a brief explanation:

Relative Permissions

Relative permissions modify existing permissions by adding, removing, or changing specific


ones. They use symbolic notation:

 u: User (owner)
 g: Group
 o: Others
 a: All (user, group, others)
 +: Add permission
 -: Remove permission
 =: Set permission explicitly
Example:

bash
chmod u+x file

This adds execute permission to the user.

bash
chmod g-w file

This removes write permission for the group.

bash
chmod o=r file

This sets read-only permission for others.

Absolute Permissions

Absolute permissions set all permissions explicitly using octal numbers:

 4:Read (r)
 2:Write (w)
 1:Execute (x)
 Combine these values to define permissions for user, group, and others.

Example:

bash
chmod 755 file

 7 (user): Read, write, and execute (4+2+1)


 5 (group): Read and execute (4+1)
 5 (others): Read and execute (4+1)

bash
chmod 644 file

 6 (user): Read and write (4+2)


 4 (group): Read-only
 4 (others): Read-only
To change file ownership in Unix, you can use the chown command. Here's a quick guide:

Syntax:

bash
chown [options] new_owner filename

Examples:

1. Change File Owner:

bash

chown username file

This sets the owner of the file to the specified user.

2. Change Both Owner and Group:

bash

chown username:groupname file

This changes the owner and the group associated with the file.

3. Change Ownership of a Directory and Its Contents: Use the -R option for recursive
changes:

bash

chown -R username:groupname directory

This will change ownership for the directory and all files/subdirectories within it.

View Ownership:

To check file ownership, you can use:

bash
ls -l
To change the group ownership of a file or directory in Unix, you can use the chgrp command.
Here's how it works:

Syntax:

bash
chgrp group_name filename

Examples:

1. Change Group Ownership of a File:

bash

chgrp new_group file

This changes the group associated with the file to new_group.

2. Change Group Ownership of a Directory:

bash

chgrp new_group directory

This changes the group associated with the directory.

3. Change Group Ownership Recursively: If you want to change the group ownership for
a directory and all its contents, use the -R (recursive) option:

bash

chgrp -R new_group directory

Verifying Changes:

You can check the updated group ownership with:

bash
ls -l
A file system in Unix is a method for organizing and storing data on a disk. It manages files and
directories and provides access to them, ensuring efficient data retrieval and storage. Here’s a
brief overview:

File System

1. Structure: A file system consists of:


o Directories (folders) for organizing files hierarchically.
o Files containing actual data.
2. Components: It includes:
o Superblock: Metadata about the file system.
o Inodes: Details of individual files and directories.
o Data Blocks: Where file content is stored.
3. Types: Common Unix file systems include ext2, ext3, ext4, XFS, and ZFS.

Inodes

An inode (index node) is a data structure used to store metadata about a file or directory. Each
file has a unique inode within its file system. Inodes contain:

1. File Metadata:
o Permissions
o Ownership (user and group)
o File size
o Last access/modification times
2. Pointers to Data Blocks:
o Inodes store pointers to the actual data blocks that contain the file content.

In Unix, hard links and soft links (or symbolic links) are methods of creating references to files.
Here's a concise overview:

Hard Link:

1. Definition:
o A hard link is an additional name for a file, pointing directly to its inode (its
metadata). Essentially, it creates a second "path" to the same file.
2. Key Features:
o Files are indistinguishable: deleting one doesn't affect the other.
o The link exists as long as the inode exists (even if the original file name is
deleted).
o Cannot span across different file systems (e.g., separate partitions).
3. Creating a Hard Link:

bash

ln original_file hard_link

Soft Link (Symbolic Link):

1. Definition:
o A soft link is a shortcut pointing to the name of the original file, rather than
directly to its inode.
2. Key Features:
o Acts like a reference; if the original file is deleted, the link becomes broken.
o Can cross file systems and partitions.
o Easily identifiable as a link (e.g., ls -l shows the link path with ->).
3. Creating a Soft Link:

bash

ln -s original_file soft_link

Comparison:

Feature Hard Link Soft Link


Points to Inode File name
Dependency Independent of original file Dependent on original file
Cross file systems No Yes
Broken on deletion No Yes

File attributes for directories in Unix hold critical importance, as they define how the directory
can be accessed and interacted with. Here's a brief overview of their significance:

Key Attributes for Directories:

1. Permissions (r, w, x):


o Read (r): Grants the ability to list the contents of the directory using commands
like ls.
o Write (w): Allows adding, renaming, or deleting files within the directory.
o Execute (x): Enables access to the directory (e.g., navigating into it with cd and
reading file metadata).
2. Ownership:
o A directory's owner and group determine who can manipulate its contents and
metadata.
3. Timestamps:
o Access Time: Indicates the last time a file within the directory was accessed.
o Modification Time: Records when files or the directory structure were last
changed.
o Change Time: Reflects updates to metadata, such as permissions.
4. Links:
o Directories typically have 2 or more links, where one points to itself and others
to subdirectories.

In Unix, the default permissions for files and directories are determined by the system, and they
can be modified by using a value called umask. Here's a concise explanation:

Default Permissions:

1. Files:
o By default, files are created with 666 (read and write for user, group, and others).
o Files do not have execute (x) permissions by default, as execution is not typically
needed for regular files.
2. Directories:
o By default, directories are created with 777 (read, write, and execute for user,
group, and others).
o The execute (x) permission on a directory allows users to access and traverse the
directory.

umask:

 umask (user file-creation mask) is a value that modifies the default permissions by
"masking" certain permissions.
 It subtracts permissions from the system defaults.

How umask Works:

To calculate the effective permissions:

1. Subtract the umask value from the system default permissions.


2. The resulting permissions are applied to new files and directories.

Example:

 Default file permissions: 666


 Default directory permissions: 777
 If the umask is 022:
o File permissions: 666 - 022 = 644 (read and write for user, read-only for group
and others)
o Directory permissions: 777 - 022 = 755 (read, write, and execute for user; read
and execute for group and others)

Viewing and Setting umask:

1. To view the current umask, use:

bash

umask

2. To set a new umask, use:

bash

umask 027

This sets default permissions to:

o Files: 640
o Directories: 750

To list the modification and access times of files in Unix, you can use the ls command with
appropriate options:

Commands:

1. Show Last Modification Time:

bash
ls -l

o This lists files with their last modification time in the output.
o Example:
o -rw-r--r-- 1 user group 1234 Mar 28 14:30 filename
2. Show Last Access Time:

bash

ls -lu

o This lists files with their last access time instead of modification time.
3. Show Last Status Change Time:

bash

ls -lc

o This lists files with the last metadata change (permissions, ownership, etc.) time.
4. Detailed Time Information: If you want more detailed timestamps:

bash

ls -lt --time-style=full-iso

o This displays full timestamps for modification or access.

The touch command in Unix can be used to update the timestamps of a file, including its access
and modification times. Here's how it works:

Syntax:

bash
touch [options] filename

Key Uses:

1. Update to Current Time:


o If you use touch on an existing file, its access and modification times will be set
to the current system time.
bash

touch filename

2. Create a New File:


o If the specified file does not exist, touch creates an empty file with the current
timestamps.

bash

touch newfile

3. Set Specific Timestamps:


o Use the -t option to set a custom timestamp in the format
[[CC]YY]MMDDhhmm[.ss]:

bash

touch -t 202503281230.45 filename

This sets the time to March 28, 2025, at 12:30:45.

4. Change Only Access or Modification Time:


o Use -a to change the access time only:

bash

touch -a filename

o Use -m to change the modification time only:

bash

touch -m filename

5. Reference Another File's Timestamps:


o Use -r to copy the timestamps of another file:

bash

touch -r reference_file target_file


The find command in Unix is a powerful tool for locating files and directories. Here's an
overview of how it works and common use cases:

Syntax:

bash
find [path] [expression]

 path: The directory to search (e.g., /home/user/ or . for the current directory).
 expression: Criteria to match files, such as name, size, type, etc.

Examples:

1. Find Files by Name:

bash

find /path/to/search -name "filename"

o Replace "filename" with the name (or pattern, e.g., *.txt) of the file you are
looking for.
2. Find Files by Type:

bash

find /path/to/search -type f

o -type fsearches for files.


o -type dsearches for directories.
3. Find Files by Size:

bash

find /path/to/search -size +1M

o Finds files larger than 1 megabyte. (+ for larger, - for smaller, or = for exact size).
4. Find Files Modified Recently:

bash

find /path/to/search -mtime -7

o Finds files modified in the last 7 days.


5. Find Files by Permissions:

bash
find /path/to/search -perm 644

o Matches files with specific permissions.


6. Perform Actions on Found Files:
o Delete files:

bash

find /path/to/search -name "*.tmp" -delete

o Execute a command on found files:

bash

find /path/to/search -name "*.log" -exec rm {} \;

The interpretive cycle of the shell refers to the sequence of steps a Unix shell follows to process
and execute commands. Here’s a simplified overview of this cycle:

Steps in the Interpretive Cycle

1. User Input:
o The shell waits for the user to type a command or script at the prompt.
2. Parsing:
o The shell reads the command and breaks it into individual elements (e.g.,
command, options, arguments).
o Special characters (e.g., |, >, $) are interpreted and processed.
3. Expansion:
o The shell performs expansions, such as:
 Variable Expansion: Substituting variables with their values (e.g.,
$HOME).
 Command Substitution: Executing commands within backticks or
$(...) and using their output.
 Wildcard Expansion: Expanding patterns like *.txt to match filenames.
4. Command Lookup:
o The shell searches for the command:
 Built-in Commands: Executed directly by the shell.
 External Commands: Located in directories specified by the $PATH
variable.
5. Execution:
o The shell initiates the execution of the command.
o If it’s an external command, the shell forks a new process to execute it.
6. Output Handling:
o The shell processes the command's output and redirects it (if specified by
operators like >, <, or |).
7. Completion:
o Once the command is executed, the shell returns control to the user and awaits the
next input.

In Unix and Linux systems, there are various types of shells that act as command interpreters
and provide interfaces for users to interact with the operating system. Here’s a brief overview:

Common Shell Types

1. Bourne Shell (sh):


o One of the earliest shells developed for Unix.
o Known for its simplicity and scripting capabilities.
o Located at /bin/sh.
2. Bourne Again Shell (bash):
o An enhanced version of the Bourne Shell.
o Most widely used shell today.
o Includes features like command history, tab completion, and better scripting.
o Default shell in many Linux distributions.
3. C Shell (csh):
o Inspired by the C programming language.
o Offers syntax similar to C for scripting.
o Includes features like command aliases and job control.
4. Korn Shell (ksh):
o Combines features of Bourne Shell and C Shell.
o Enhanced scripting capabilities.
o Suitable for system administration tasks.
5. Z Shell (zsh):
o Highly customizable and user-friendly.
o Includes advanced features like spelling correction, better tab completion, and
themes.
o Popular among developers.
6. Fish (Friendly Interactive Shell):
o A modern and interactive shell with user-friendly features.
o Provides suggestions and syntax highlighting.
Key Features of Different Shells:

Shell Type Key Features Typical Use Case


sh Basic scripting Legacy systems
bash Advanced scripting, user-friendly Default shell in Linux
csh C-like syntax, aliases Specialized scripting
ksh Powerful scripting System administration
zsh Customizable, interactive Development environments
fish Syntax highlighting, suggestions Beginners and modern use

Pattern matching in Unix is a powerful way to work with text and files by identifying strings that
match specific patterns. It's commonly used with commands like grep, awk, sed, and in shell
scripting.

Key Techniques for Pattern Matching:

1. Regular Expressions (Regex):


o Regex allows for flexible search patterns. For example:
 ^pattern matches lines that start with "pattern."
 pattern$ matches lines that end with "pattern."
 .* matches any number of any characters.
o Example:

bash

grep "^Hello" file.txt

2. Wildcards:
o Wildcards are often used in file matching.
 * matches zero or more characters (e.g., *.txt matches all .txt files).
 ? matches a single character (e.g., file?.txt matches file1.txt or
fileA.txt).
 [] matches specific characters (e.g., file[1-3].txt matches file1.txt,
file2.txt, and file3.txt).
3. Using Tools:
o grep: Searches for patterns in files.
o sed: Performs search, replace, and text manipulation.
o awk: Processes and analyzes text files based on patterns.
o Example of grep:

bash

grep "error" logs.txt

In Unix, "escaping" refers to using special characters or sequences to prevent the shell from
interpreting certain characters as having special meaning. This is often necessary when working
with file names, commands, or patterns that include characters like spaces, quotes, or symbols.

Common Ways to Escape Characters:

1. Backslash (\):
o A backslash is used to escape a single special character.
o Example:

bash

echo "This is a \$dollar sign"

Outputs: This is a $dollar sign

2. Single Quotes ('):


o Encloses a string so that special characters are taken literally (except ' itself).
o Example:

bash

echo 'This is $100'

Outputs: This is $100

3. Double Quotes ("):


o Encloses a string while still allowing some special characters (like $ for variables)
to be interpreted.
o Example:

bash

name="Alice"
echo "Hello, $name"
Outputs: Hello, Alice

4. Escape Sequences in Regex:


o Escape sequences are also used in pattern matching.
o Example:

bash

grep "file\.txt" filenames.txt

Quoting in Unix is essential for controlling how the shell interprets characters in commands or
scripts. There are three main types of quoting:

1. Single Quotes (')

 Preserve the literal value of all enclosed characters.


 Used when you don't want the shell to interpret any special characters (e.g., $, *, ?, etc.).
 Example:

bash

echo 'This is $100'

Output: This is $100

2. Double Quotes (")

 Preserve the literal value of most characters, except $, backticks (\``), and backslash
(\`).
 Allow variable expansion and command substitution.
 Example:

bash

name="Alice"
echo "Hello, $name"

Output: Hello, Alice

3. Backslash (\)
 Escapes a single special character outside of quotes.
 Example:

bash

echo Hello\ World

Output: Hello World

Combining Quotes

You can mix quotes and escape characters for complex scenarios:

bash
echo 'The cost is \$100, not "$200"!'

Redirection in Unix is a method of directing data between commands, files, and the terminal. It's
a crucial concept for managing input and output streams effectively.

Types of Redirection:

1. Output Redirection (>):


o Sends the output of a command to a file, overwriting its contents.
o Example:

bash

ls > files.txt

Saves the output of ls into files.txt.

2. Append Output (>>):


o Appends the output of a command to the end of a file without overwriting.
o Example:

bash

echo "New line" >> files.txt

Adds New line to files.txt.


3. Input Redirection (<):
o Provides input to a command from a file instead of the keyboard.
o Example:

bash

sort < unsorted.txt

Sorts the contents of unsorted.txt.

4. Combine Redirection (|, Pipe):


o Connects the output of one command to the input of another.
o Example:

bash

cat file.txt | grep "pattern"

Sends the content of file.txt to grep for pattern matching.

5. Error Redirection:
o Redirects error messages (stderr) to a file using 2>.
o Example:

bash

command 2> errors.txt

Sends error output to errors.txt.

6. Combine Standard Output & Error:


o Redirect both stdout and stderr to a single file.
o Example:

bash

command > all_output.txt 2>&1

Standard input, or stdin, is one of the standard I/O streams in Unix-like systems. It allows data
to be fed into a program or command, typically from the keyboard or a file. It plays a key role in
creating flexible and interactive commands.
How Standard Input Works:

1. Keyboard Input (Default):


o By default, stdin takes input from the keyboard until Enter is pressed.
o Example:

bash

cat

Typing text followed by Ctrl+D sends the input to cat.

2. Input Redirection (<):


o Redirect input from a file to a command.
o Example:

bash

sort < file.txt

Feeds the contents of file.txt into the sort command.

3. Using Pipes (|):


o Connects the output of one command to the stdin of another.
o Example:

bash

echo "Hello World" | tr '[:lower:]' '[:upper:]'

Standard output, or stdout, is one of the standard I/O streams in Unix-like systems. It is used by
programs and commands to send their output, typically to the terminal or console.

How Standard Output Works:

1. Display Output (Default):


o By default, commands and programs send their output to stdout, which is
displayed on the screen.
o Example:
bash

echo "Hello, World!"

Output: Hello, World!

2. Output Redirection (>):


o Redirect stdout to a file, overwriting its contents.
o Example:

bash

ls > files.txt

Saves the output of the ls command to files.txt.

3. Appending Output (>>):


o Append stdout to the end of a file without overwriting.
o Example:

bash

echo "New line" >> files.txt

Adds "New line" to files.txt.

4. Using Pipes (|):


o Sends stdout of one command as stdin for another command.
o Example:

bash

cat file.txt | grep "pattern"

Connects the output of cat file.txt to grep.

Error Output (Differentiating stdout and stderr):

While stdout is for regular output, error messages are sent to stderr. You can redirect or
combine them for advanced use:

bash
command > output.txt 2> errors.txt

Or combine:

bash
command > all_output.txt 2>&1

Standard error, or stderr, is one of the standard I/O streams in Unix-like systems. It is used by
programs and commands to output error messages or diagnostics, separate from normal output
(stdout). This separation allows for better handling and debugging of errors.

Key Features of stderr:

1. Default Behavior:
o Error messages are displayed on the terminal by default, regardless of where
stdout is directed.
o Example:

bash

ls nonexistent_directory

Output:

ls: cannot access 'nonexistent_directory': No such file or


directory

2. Redirecting stderr (2>):


o You can redirect stderr to a file for error logging.
o Example:

bash

command 2> error_log.txt

Saves all error messages to error_log.txt.

3. Combining stdout and stderr:


o Redirect both stdout and stderr to the same file for unified logging.
o Example:

bash

command > output_log.txt 2>&1

Combines standard output and error into output_log.txt.


4. Piping stderr:
o To process stderr output, use advanced tools like 2> >(command):

bash

ls nonexistent_directory 2> >(grep "No such file")

Let me break down each of these topics for you:

1. /dev/null and /dev/tty:

 /dev/null:
o It's a "null device," acting as a black hole for data.
o Any data written to /dev/null is discarded.
o Example:

bash

ls nonexistent_file 2> /dev/null

Suppresses error messages by redirecting stderr to /dev/null.

 /dev/tty:
o Refers to the terminal of the current user session.
o Useful for directly interacting with the user even in scripts.
o Example:

bash

echo "Enter your name: " > /dev/tty


read name < /dev/tty

2. Pipe (|):

 Pipes connect the output (stdout) of one command to the input (stdin) of another.
 Example:

bash

cat file.txt | grep "pattern"


Sends the contents of file.txt to grep for pattern matching.

 Pipes are essential for building complex command chains efficiently.

3. tee:

 teereads from stdin and writes to both stdout and a file simultaneously.
 Example:

bash

echo "Hello" | tee output.txt

Prints "Hello" to the terminal and saves it to output.txt.

 Useful for logging while still displaying output:

bash

command | tee log.txt

4. Command Substitution:

 Allows capturing the output of a command and using it as input in another command.
 Syntax:
o Backticks: `command`
o $(command)
 Example:

bash

current_date=$(date)
echo "Today is $current_date"

5. Shell Variables:

 Variables in shell scripts store and manage data.


 Syntax:
o Assign: variable_name=value
o Access: $variable_name
 Example:

bash

name="Alice"
echo "Hello, $name"
In UNIX, a process is an instance of a program in execution. It is fundamental to the functioning
of a UNIX system, as processes represent the tasks being performed by the system.

Key Concepts of a UNIX Process:

1. Process Structure:
o A process has several components, including:
 PID (Process ID): A unique identifier assigned to each process.
 Parent Process ID (PPID): Identifies the process that spawned the
current process.
 Memory Allocation: Processes have allocated memory regions (code,
data, stack, etc.).
 State: Indicates whether a process is running, sleeping, or stopped.
2. Process Lifecycle:
o Creation: A new process is created using fork() or similar system calls.
o Execution: The process begins executing the assigned program using calls like
exec().
o Termination: Processes terminate when they finish execution or are killed.
3. Hierarchy:
o UNIX processes follow a parent-child relationship. The init process is the first
process and the ancestor of all others.
4. Process Control:
o Commands: Tools like ps, top, kill, and jobs are used to view and manage
processes.
 Example:

bash

ps -aux

The ps command in Unix is used to display information about active processes. It’s a versatile
tool to view process attributes like Process IDs (PID), CPU usage, memory usage, and more.

Commonly Used ps Commands:

1. Basic Process List:

bash

ps
Displays a simple list of your processes.

2. Detailed Information (-aux):

bash

ps -aux

o Shows detailed information about all processes.


o Key attributes displayed:
 USER: The user owning the process.
 PID: Process ID.
 %CPU: CPU usage.
 %MEM: Memory usage.
 COMMAND: The command that started the process.
3. Tree Format (-axjf):

bash

ps -axjf

Displays processes in a hierarchical format.

4. Search for a Specific Process (grep):

bash

ps -aux | grep process_name

Finds a specific process by name.

5. Custom Format (-o):

bash

ps -eo pid,user,%cpu,%mem,command

o Displays specified attributes:


 PID: Process ID.
 USER: User running the process.
 %CPU: CPU usage percentage.
 %MEM: Memory usage percentage.
 COMMAND: Command that started the process.

Example Output for ps -aux:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 19360 1588 ? Ss Mar27 0:00 /sbin/init
user 1234 0.1 0.2 30000 5000 tty1 S 14:42 0:01 bash
user 5678 5.5 1.3 120000 40000 tty1 R 14:43 1:45 firefox

To display system processes in Unix, the ps command is commonly used with specific options to
include all processes, including system-level ones.

Display All System Processes:

1. ps -ef:
o This option shows all processes running on the system in a full-format listing.
o Example:

bash

ps -ef

 Columns include UID (user ID), PID (process ID), PPID (parent process
ID), and COMMAND.
2. ps -aux:
o This option provides a more detailed view of all processes, including system and
user-level processes.
o Example:

bash

ps -aux

 Displays details like CPU and memory usage.


3. Hierarchical View of Processes (ps -axjf):
o Use this to see processes in a tree-like format, including their parent-child
relationships.
o Example:

bash

ps -axjf

4. Using top or htop:


o For a real-time view of all processes, including system processes:
bash

top

 Displays processes dynamically and updates regularly.


o htop (if installed) is more user-friendly and colorful:

bash

htop

5. pidof Command:
o If you are looking for a specific system process:

bash

pidof systemd


Returns the PID of the systemd process or any specified process.
6. Display Kernel Processes (ps -eo):
o Use ps -eo for more control over the information displayed, such as showing
only system-specific attributes.
o Example:

bash

ps -eo pid,comm,state,start_time

The process creation cycle in UNIX describes how processes are created, managed, and
terminated. It's a fundamental aspect of how the operating system functions. Here's an overview
of the cycle:

1. Process Creation:

 A new process is created by an existing process using the fork() system call.
 The parent process creates a child process, which is an exact copy of itself.
 Both processes continue executing from the point where fork() was called, but the
return value of fork() helps differentiate between the parent and child.
o Parent process: fork() returns the child's Process ID (PID).
o Child process: fork() returns 0.
Example:

c
pid_t pid = fork();
if (pid == 0) {
// Code for the child process
} else {
// Code for the parent process
}

2. Program Execution:

 The child process can replace its own program image with a new program using exec()
family system calls (e.g., execlp(), execv()).
 This step allows the child process to execute a completely different program.
 Example:

execlp("ls", "ls", "-l", (char *)NULL);

3. Parent-Child Relationship:

 The parent process can monitor the child using the wait() or waitpid() system calls.
 These calls make the parent wait for the child to terminate, preventing orphaned
processes.

4. Termination:

 A process terminates when it finishes execution or is explicitly killed using system calls
like exit() or by receiving signals (e.g., SIGTERM).
 The process resources are released, and its termination status is returned to the parent.

The process of creating a shell in a UNIX system involves several steps, starting from system
initialization and leading up to the user's interactive shell. Here's an overview of the steps:

1. init (Initialization):

 The init process is the first process started by the kernel once the system boots.
 It initializes the system by running initialization scripts (/etc/init or other init system
configurations like systemd) to set up the environment, mount file systems, and start
essential services.
 init determines the system's runlevel and spawns getty processes on terminals.

2. getty (Terminal Initialization):

 getty is responsible for preparing the terminal device for user login.
 It configures terminal settings and displays the login prompt to the user.
 Example login prompt:
 login:
 Once the user enters their credentials, getty hands control to the login program.

3. login (User Authentication):

 The login process verifies the user's credentials (username and password) against the
system's user database (e.g., /etc/passwd or /etc/shadow).
 If authentication is successful:
o It sets up the user's environment, including their home directory and environment
variables.
o It starts the user's default shell (defined in the user database).
 If authentication fails, the user is prompted to try again.

4. Shell:

 Finally, the user's default shell (e.g., bash, sh, zsh) is launched.
 The shell provides an interactive command-line interface, allowing the user to execute
commands, run scripts, and interact with the system.

In Unix, the state of a process represents its current stage of execution. Processes can exist in
several states throughout their lifecycle, and the operating system manages these states to ensure
efficient execution.

Common Process States:

1. Running (R):
o The process is actively being executed by the CPU.
o If multiple processes are in the "Running" state, they share CPU time.
2. Waiting:
o Interruptible (S): The process is waiting for an event or I/O operation to
complete and can be interrupted by signals.
o Uninterruptible (D): The process is waiting for a hardware event and cannot be
interrupted by signals.
3. Stopped (T):
o The process has been suspended and is not being executed.
o This can happen due to signals like SIGSTOP or SIGTSTP.
o It can resume execution when signals like SIGCONT are sent.
4. Zombie (Z):
o The process has completed execution, but its entry remains in the process table
because the parent process has not yet read its termination status.
o Example: A process becomes a zombie if the parent doesn't call wait().
5. Idle/Orphan:
o An orphaned process is one whose parent has terminated, but the process
continues to run under the init process.

Visualizing Process States:

 You can view the states of active processes using the ps command:

bash

ps -aux

In Unix, background jobs are tasks that run independently of the terminal, allowing you to
continue working while they execute. The & operator and nohup command are commonly used to
manage background jobs.

1. Background Jobs with &:

 The & operator is used to run a command in the background.


 When you append & to a command, the shell starts the process in the background and
returns control to the terminal.
 Example:

bash

sleep 60 &
This starts the sleep command as a background job, allowing you to use the terminal
immediately.

 View Background Jobs:


o Use the jobs command to list all background jobs.

bash

jobs

 Bring Background Job to Foreground:


o Use fg followed by the job number.

bash

fg %1

2. nohup Command:

 nohup ("no hang-up") allows a command to continue running even after the terminal is
closed.
 It ignores the SIGHUP signal, ensuring the job keeps running.
 Example:

bash

nohup my_script.sh &

This runs my_script.sh in the background and outputs logs to nohup.out.

 Redirect Output:
o To customize the output file, redirect stdout and stderr.

bash

nohup my_script.sh > output.log 2>&1 &

In Unix, you can reduce the priority of a process using the nice command. It adjusts the
"niceness" value of a process, which influences its priority for CPU scheduling. A higher "nice"
value means the process is less prioritized, allowing other processes to use the CPU more.
Using nice to Start a Process with Lower Priority:

 Syntax:

bash

nice -n [niceness_value] command

o niceness_value ranges from -20 (highest priority) to 19 (lowest priority). Only


privileged users (e.g., root) can set negative values.
o Example:

bash

nice -n 10 my_program

This starts my_program with a niceness of 10, lowering its CPU priority.

Adjusting Priority of a Running Process (renice):

 You can use the renice command to change the priority of an already running process.
 Syntax:

bash

renice [niceness_value] -p [PID]

o Example:

bash

renice 15 -p 1234

Changes the niceness of the process with PID 1234 to 15.

Viewing Niceness of Processes:

 Use the ps command to check the niceness value of processes.

bash

ps -eo pid,ni,comm
In Unix, signals are used to communicate with processes. You can use signals to kill or manage
processes. The kill command is the most commonly used tool for sending signals to processes.

Key Signals Used for Killing Processes:

1. SIGTERM (Signal 15):


o Politely asks a process to terminate. The process can handle this signal and
perform cleanup before exiting.
o Example:

bash

kill -15 [PID]

o The default signal for kill is SIGTERM, so the following is equivalent:

bash

kill [PID]

2. SIGKILL (Signal 9):


o Forces a process to terminate immediately and cannot be ignored by the process.
o Example:

bash

kill -9 [PID]

3. SIGHUP (Signal 1):


o Hangs up a process, often causing it to reload configuration files or restart.
o Example:

bash

kill -1 [PID]

Sending Signals to Processes:

 Using kill:
o Sends signals to processes by specifying their Process ID (PID).
o Example to terminate a process:

bash

kill -SIGTERM 1234

 Using pkill:
o Kills processes by their name instead of PID.
o Example:

bash

pkill -f process_name

 Using killall:
o Kills all processes with a specific name.
o Example:

bash

killall process_name

 Ctrl+C (SIGINT):
o In an interactive shell, pressing Ctrl+C sends a SIGINT (Interrupt Signal) to the
foreground process.

View Available Signals:

 Use the kill command to list all available signals:

bash

kill -l

In Unix, the bg and fg commands allow you to manage jobs by sending them to the background
or bringing them to the foreground. This is particularly useful when working with tasks
interactively in the terminal.

1. Sending a Job to the Background (bg):

 If you have suspended a process (e.g., using Ctrl+Z), you can use the bg command to
resume it in the background.
 Steps:
1. Start a command.

bash

sleep 100
2. Suspend the process using Ctrl+Z.

bash

[1]+ Stopped sleep 100

3. Send the job to the background.

bash

bg %1

This resumes job 1 in the background.

2. Bringing a Job to the Foreground (fg):

 You can use the fg command to bring a background job to the foreground.
 Steps:
1. List jobs to find the job number.

bash

jobs

Example output:

bash

[1]+ Running sleep 100 &

2. Bring the job back to the foreground.

bash

fg %1

This brings job 1 to the foreground for direct interaction.

Key Notes:

 View Jobs: Use jobs to list all background and stopped jobs.

bash

jobs
In Unix, the jobs command is used to list all active jobs associated with the current shell
session. This includes background jobs and jobs that have been stopped.

How to Use jobs:

1. Basic Command:
o Simply type jobs to display the list of jobs.
o Example:

bash

jobs

Output (example):

[1]+ Running sleep 100 &


[2]- Stopped cat

 The output shows:


 Job numbers (e.g., [1], [2]).
 Job states (e.g., Running, Stopped).
 Associated commands (e.g., sleep 100, cat).
2. Job States:
o Running: The job is currently active in the background.
o Stopped: The job has been suspended (e.g., via Ctrl+Z).
3. Key Symbols in Job Listing:
o +: The default job that fg or bg will act on.
o -: The next job in line if the default one is acted upon.
4. Control Jobs:
o Bring Job to Foreground:

bash

fg %job_number

o Send Job to Background:

bash

bg %job_number
Suspending a job in Unix temporarily halts its execution, placing it in a "stopped" state. This is
useful when you need to pause a task to free up the terminal for other commands.

How to Suspend a Job:

1. Using Ctrl+Z:
o While a job is running in the foreground, press Ctrl+Z to suspend it.
o Example:

bash

sleep 100

 While sleep is running, press Ctrl+Z.


 Output:
 [1]+ Stopped sleep 100
2. Listing Suspended Jobs:
o Use the jobs command to see the list of suspended jobs.
o Example:

bash

jobs

 Output:
 [1]+ Stopped sleep 100
3. Resume a Suspended Job:
o To Background: Use the bg command to resume the job in the background.

bash

bg %1

o To Foreground: Use the fg command to resume the job in the foreground.

bash

fg %1

In Unix, you can kill a job using the kill command by specifying its process ID (PID) or job ID.
This terminates the process associated with the job.
Steps to Kill a Job:

1. Find the Job ID or PID:


o Use the jobs command to list all jobs and get the job ID (e.g., %1).

bash

jobs

Example output:

[1]+ Running sleep 100 &

o Alternatively, use the ps command to find the PID of the process.

bash

ps

2. Kill the Job:


o Use the kill command with the job ID:

bash

kill %1

 %1 refers to the job ID from the jobs output.


o To use the PID:

bash

kill 1234

Replace 1234 with the PID of the process.

3. Forcibly Kill a Job:


o If the process does not terminate with the default SIGTERM signal, use SIGKILL to
force it to stop:

bash

kill -9 %1

Or:

bash
kill -9 1234

Verify the Job is Killed:

 Use the jobs or ps command to check if the process is no longer running:

bash

jobs
ps

In Unix, the at and batch commands are used to schedule jobs to run at a specified time. These
commands are ideal for task automation when you need to run commands later without
continuous user interaction.

1. Using at:

 The at command schedules a task to execute at a specific time and date.


 Syntax:

bash

at [time]

 Examples:
o Schedule a job for 3:00 PM:

bash

at 3:00 PM

o Schedule a job for tomorrow at 10 AM:

bash

at 10:00 AM tomorrow

 Steps:
1. Enter the at command with the specified time.
2. Type the commands to execute.
3. Press Ctrl+D to save and exit. Example:

bash

at 4:30 PM
echo "Backup complete" > /var/log/backup.log
Ctrl+D

 View Scheduled Jobs (atq):


o List all scheduled jobs:

bash

atq

 Cancel a Job (atrm):


o Remove a scheduled job by its job number:

bash

atrm [job_number]

2. Using batch:

 The batch command schedules jobs to run when the system load drops below a certain
threshold. It’s useful for low-priority tasks.
 Syntax:

bash

batch

 Steps:
1. Enter the batch command.
2. Type the commands to execute.
3. Press Ctrl+D to save and exit. Example:

bash

batch
echo "Maintenance task completed" > /var/log/maintenance.log
Ctrl+D

Differences Between at and batch:


Command Purpose Execution
at Run jobs at a specified time Executes at the given time
batch Run jobs when system load is low Waits for ideal system load

Environment variables in Unix are dynamic values stored within the operating system, which
help processes and programs access configuration settings and runtime data. They are widely
used in scripting, programming, and managing the system environment.

Key Uses of Environment Variables:

1. System Configuration:
o Environment variables store important information about the system, such as the
user's home directory, shell type, and search paths.
o Examples:
 $HOME: The current user's home directory.
 $PATH: Directories to search for executable files.
2. Customizing the User Environment:
o Users can define their own environment variables to suit their workflows.
o Example:

bash

export EDITOR=nano

Sets nano as the default text editor.

3. Passing Data to Programs:


o Environment variables can pass runtime data to scripts and programs.
o Example:

bash

export DATABASE_URL=jdbc:mysql://localhost:3306/mydb

A program can access this variable to connect to the database.

4. Scripting:
o In shell scripts, environment variables are used to store temporary data,
configuration, or results.
o Example:
bash

#!/bin/bash
LOG_DIR="/var/logs/myapp"
echo "Logs are stored in $LOG_DIR"

5. Global Settings:
o System-wide settings are defined using environment variables in files like
/etc/environment or /etc/profile.
6. Debugging:
o Programs can use variables like $DEBUG to toggle debugging features.

Viewing and Modifying Environment Variables:

 List All Variables:

bash

printenv

Or:

bash

env

 View Specific Variable:

bash

echo $HOME

 Set/Modify Variable Temporarily:

bash

export VAR_NAME=value

 Set Variable Permanently:


o Add the variable to .bashrc or .bash_profile:

bash

echo 'export VAR_NAME=value' >> ~/.bashrc


Here are some common environment variables in Unix systems and their purposes:

Environment Variables:

1. HOME:
o Stores the current user's home directory.
o Example:

bash

echo $HOME

Output might be: /home/username

o Used by programs to locate user-specific files.


2. PATH:
o Contains a list of directories where the system searches for executables.
o Example:

bash

echo $PATH

Output might be: /usr/local/bin:/usr/bin:/bin

o When you run a command, the shell looks in the directories listed in PATH.
3. LOGNAME:
o Holds the current user's login name.
o Example:

bash

echo $LOGNAME

Output might be: username

4. USER:
o Represents the username of the current user.
o Similar to LOGNAME, but used in different contexts.
o Example:

bash

echo $USER

Output might be: username


5. TERM:
o Specifies the type of terminal being used.
o Example:

bash

echo $TERM

Output might be: xterm or linux

o Programs use this variable to configure terminal behavior.


6. PWD:
o Holds the current working directory.
o Example:

bash

echo $PWD

Output might be: /home/username/projects

7. PS1:
o Defines the primary prompt string, which appears when you interact with the
shell.
o Example:

bash

echo $PS1

Output might be: \u@\h:\w\$

 Customizable to show user, hostname, or working directory.


8. PS2:
o Represents the secondary prompt string, shown when a command spans multiple
lines.
o Example:

bash

echo $PS2

Output might be: >

Viewing or Modifying Environment Variables:

 View a Variable:
bash

echo $VARIABLE_NAME

 Set/Modify a Variable Temporarily:

bash

export VARIABLE_NAME=new_value

 Set Permanently: Add to .bashrc or .bash_profile:

bash

echo 'export VARIABLE_NAME=new_value' >> ~/.bashrc

The pr command in Unix is used to prepare and format text files for printing. It adds headers,
footers, pagination, and more to make files printer-friendly.

Key Features of the pr Command:

1. Basic Usage:
o Default formatting for a file:

bash

pr filename

oExample output:
 Adds headers with the date, time, and file name.
 Paginated with page numbers.
2. Control Page Width (-w):
o Specify the width of each line.
o Example:

bash

pr -w 80 filename

Sets the page width to 80 characters.


3. Control Number of Columns (-n):
o Print the file in multiple columns.
o Example:

bash

pr -2 filename

Outputs the file in two columns.

4. Add Line Numbers (-n):


o Print the file with line numbers.
o Example:

bash

pr -n filename

5. Change Header Text:


o Replace the default header with custom text.
o Example:

bash

pr -h "Custom Header" filename

6. Set Margins (-o):


o Add a left margin (indent).
o Example:

bash

pr -o 5 filename

Adds a margin of 5 spaces.

7. Preview File With Pagination:


o Send the file through more or less to preview the formatted file:

bash

pr filename | more

8. Send to Printer:
o Pipe the output to a printing command (e.g., lp or lpr).
o Example:
bash

pr filename | lpr

Example Workflow:

To format a file with a custom header, double-column layout, and send it to a printer:

bash
pr -h "Project Report" -2 filename | lpr

The head and tail commands in Unix are powerful tools for displaying specific portions of a
file. Together, they can be combined or used individually to create a custom display of file
contents.

1. Using head:

 The head command displays the first few lines of a file (default is 10).
 Example:

bash

head filename.txt

Displays the first 10 lines of filename.txt.

 To specify the number of lines:

bash

head -n 5 filename.txt

Displays the first 5 lines.

 To display a specific number of bytes:

bash

head -c 50 filename.txt

Displays the first 50 bytes.


2. Using tail:

 The tail command displays the last few lines of a file (default is 10).
 Example:

bash

tail filename.txt

Displays the last 10 lines of filename.txt.

 To specify the number of lines:

bash

tail -n 7 filename.txt

Displays the last 7 lines.

 To display the last few bytes:

bash

tail -c 100 filename.txt

Displays the last 100 bytes.

 Follow Mode (-f):


o Displays new lines as they are added to the file.

bash

tail -f filename.txt

3. Custom Display by Combining head and tail:

 To extract a specific section of the file, combine head and tail with a pipe (|).
 Example:
o Display lines 11 to 20:

bash

head -n 20 filename.txt | tail -n 10

 head -n 20 gets the first 20 lines.


 tail -n 10 extracts the last 10 lines from those 20, effectively showing
lines 11 to 20.
The cut command in Unix is used to extract specific sections (fields or columns) of a file or
input data. It's often employed for vertically dividing a file by selecting certain portions based on
delimiters or fixed positions.

Key Options of cut:

1. Select by Delimiters (-d and -f):


o The -d option specifies the delimiter, and the -f option selects the fields
(columns) to extract.
o Example:

bash

cut -d ',' -f 1,3 file.txt


Uses , as the delimiter.

Extracts the 1st and 3rd columns from file.txt.
2. Select by Character Positions (-c):
o The -c option extracts specific character positions.
o Example:

bash

cut -c 1-5 file.txt

 Extracts characters 1 through 5 from each line of file.txt.


3. Select by Bytes (-b):
o The -b option extracts specific bytes.
o Example:

bash

cut -b 1-10 file.txt

 Extracts bytes 1 through 10 from each line of file.txt.


4. Combine Options for Advanced Extraction:
o You can combine options to fine-tune the output.
o Example:

bash

cut -d ':' -f 2-4 file.txt

 Uses : as the delimiter.


 Extracts columns 2 through 4.
Practical Use Cases:

 Extract Usernames from /etc/passwd:

bash

cut -d ':' -f 1 /etc/passwd

Extracts the usernames (1st field) from the system's password file.

 Extract File Extensions:

bash

ls | cut -d '.' -f 2

Extracts the extensions of files in the current directory.

 Extract Specific Columns from a CSV File:

bash

cut -d ',' -f 1,3 data.csv

The paste command in Unix is used to merge lines of multiple files horizontally. It outputs lines
from each file side-by-side, separated by a tab or a custom delimiter.

Syntax:

bash
paste [options] file1 file2 ...

Common Uses of paste:

1. Combine Files Line by Line:


o Example:

bash
paste file1.txt file2.txt

If file1.txt contains:

A
B
C

And file2.txt contains:

1
2
3

The output will be:

A 1
B 2
C 3

2. Specify a Custom Delimiter (-d):


o Use -d to change the delimiter (default is a tab).
o Example:

bash

paste -d ',' file1.txt file2.txt

Output:

A,1
B,2
C,3

3. Combine a Single File Multiple Times:


o Example:

bash

paste file1.txt file1.txt

Output:

A A
B B
C C

4. Merge Without a File (Using Standard Input):


o If you don't specify a file, you can manually provide input:
bash

paste

Enter:

A
B
C
Ctrl+D

Output:

A
B
C

5. Suppress Newline (-s):


o Merge all lines of a file into a single row.
o Example:

bash

paste -s file1.txt

Output:

A B C

6. Combine Multiple Files in a Custom Way:


o For a specific arrangement, use options to suit your needs. Example:

bash

paste -s -d ',' file1.txt

Output:

A,B,C

The uniq command in Unix is used to filter out or identify repeated and non-repeated lines in a
file. It is most effective when the input file is sorted, as uniq compares adjacent lines.
Basic Syntax:

bash
uniq [options] [input_file] [output_file]

Key Features and Options of uniq:

1. Filter Out Adjacent Repeated Lines:


o Removes duplicate lines:

bash

uniq input.txt

If input.txt contains:

apple
apple
banana
orange
orange

The output will be:

apple
banana
orange

2. Show Only Repeated Lines (-d):


o Displays lines that appear more than once:

bash

uniq -d input.txt

Output:

apple
orange

3. Show Only Non-Repeated Lines (-u):


o Displays lines that appear only once:

bash

uniq -u input.txt

Output:
banana

4. Count Repetitions (-c):


o Prefixes each line with the count of occurrences:

bash

uniq -c input.txt

Output:

2 apple
1 banana
2 orange

5. Ignore Case (-i):


o Treats lines as identical even if they differ in case:

bash

uniq -i input.txt

Example: APPLE and apple are treated as the same line.

6. Specify the Output File:


o Save the result to an output file:

bash

uniq input.txt output.txt

7. Consider Only Specific Fields (-f) or Characters (-s):


o Ignore a certain number of fields or characters when comparing lines.
o Example:
 Ignore the first field:

bash

uniq -f 1 input.txt

 Skip the first 5 characters:

bash

uniq -s 5 input.txt

Example Workflow:
To sort a file and then find unique lines:

bash
sort input.txt | uniq

The tr command in Unix is a powerful tool for translating, deleting, and replacing characters in
text. It is particularly useful for data manipulation tasks, such as formatting or cleaning text files.

Syntax:

bash
tr [OPTION] SET1 [SET2]

Common Uses of tr:

1. Translate Characters:
o Replace characters in SET1 with corresponding characters in SET2.
o Example:

bash

echo "hello" | tr 'a-z' 'A-Z'

Converts lowercase to uppercase:

HELLO

2. Delete Characters (-d):


o Remove all occurrences of specified characters.
o Example:

bash

echo "hello world" | tr -d 'aeiou'

Deletes vowels:

hll wrld

3. Squeeze Repeated Characters (-s):


o Replace multiple consecutive occurrences of a character with a single instance.
o Example:

bash

echo "aaa bbb ccc" | tr -s ' '

Collapses spaces:

aaa bbb ccc

4. Delete and Translate Together:


o Combine translation and deletion for specific tasks.
o Example:

bash

echo "123abc456" | tr -d '0-9' | tr 'a-z' 'A-Z'

Removes digits and converts letters to uppercase:

ABC

5. Replace Characters Using Ranges:


o Specify character ranges for convenience.
o Example:

bash

echo "abcdef" | tr 'a-f' '1-6'

Replaces letters with numbers:

123456

6. Remove Non-Alphanumeric Characters:


o Example:

bash

echo "hello@world!" | tr -cd 'a-zA-Z0-9'

Removes special characters:

helloworld
The grep command in Unix is used to search for patterns in text files or streams. It is one of the
most powerful and widely-used tools for pattern matching and extracting information.

Syntax:

bash
grep [OPTIONS] PATTERN [FILE]

Key Features of grep:

1. Basic Search:
o Search for a pattern in a file.
o Example:

bash

grep "error" log.txt

Finds all lines containing the word "error" in log.txt.

2. Case-Insensitive Search (-i):


o Ignore case differences while matching.
o Example:

bash

grep -i "error" log.txt

3. Display Line Numbers (-n):


o Show the line numbers where the pattern occurs.
o Example:

bash

grep -n "error" log.txt

4. Search in Multiple Files:


o Example:

bash

grep "error" *.txt

Searches for "error" in all .txt files in the directory.

5. Invert Match (-v):


o Display lines that do not match the pattern.
o Example:

bash

grep -v "error" log.txt

6. Search for Whole Words (-w):


o Match only whole words.
o Example:

bash

grep -w "error" log.txt

7. Recursive Search (-r):


o Search in all files within a directory (and subdirectories).
o Example:

bash

grep -r "error" /path/to/directory

8. Count Occurrences (-c):


o Display the number of times the pattern appears.
o Example:

bash

grep -c "error" log.txt

9. Highlight Matches (--color):


o Highlight the matching text in the output.
o Example:

bash

grep --color "error" log.txt

10. Use Regular Expressions:


o Match complex patterns using regex.
o Example:

bash

grep "error[0-9]" log.txt


Basic Regular Expressions (BRE) in Unix allow you to define patterns for text searching and
manipulation. They are used by many commands like grep, sed, and awk to match specific text
strings or patterns.

Key Components of BRE:

1. Literal Characters:
o Match exact characters as they appear.
o Example:

bash

grep "apple" file.txt

Searches for lines containing the exact word "apple".

2. Wildcard (.):
o Matches any single character.
o Example:

bash

grep "a.c" file.txt

Matches "abc", "a1c", "a-c", etc.

3. Anchor Characters:
o ^: Matches the beginning of a line.

bash

grep "^apple" file.txt

Matches lines starting with "apple".

o $: Matches the end of a line.

bash

grep "apple$" file.txt

Matches lines ending with "apple".

4. Character Sets ([]):


o Matches any one character within the brackets.
o Example:
bash

grep "[aeiou]" file.txt

Matches any line containing a vowel.

5. Ranges (-):
o Specifies a range of characters within brackets.
o Example:

bash

grep "[a-z]" file.txt

Matches any lowercase letter.

6. Repetition (\):
o Match repeated occurrences of a pattern.
o Example:

bash

grep "a\{2\}" file.txt

Matches "aa".

7. Escaping Special Characters (\):


o To match special characters like . or *, use a backslash (\) to escape them.
o Example:

bash

grep "\." file.txt

Matches a literal dot (.) character.

8. Grouping (\(...\):
o Groups patterns together to perform combined matches.
o Example:

bash

grep "\(abc\|xyz\)" file.txt

Matches either "abc" or "xyz".

Practical Applications:
 Search for Patterns in Files:

bash

grep "pattern" file.txt

 Replace Patterns (Using sed):

bash

sed 's/pattern/replacement/' file.txt

 Extract Specific Data with awk:

bash

awk '/pattern/ { print $0 }' file.txt

Extended Regular Expressions (ERE) in Unix provide more powerful and flexible pattern-
matching capabilities than Basic Regular Expressions (BRE). They support additional operators
and syntax, allowing for advanced text searches and manipulations.

Overview of ERE:

 Unlike BRE, ERE includes operators such as +, ?, |, and {} without requiring escapes
(\).
 ERE is supported by tools like egrep (Enhanced grep) and grep -E.

Key Features of ERE:

1. Alternation (|):
o Matches one of several patterns.
o Example:

bash

echo "apple orange banana" | grep -E "apple|banana"

Output:

apple
banana

2. Repetition (+ and ?):


o +: Matches one or more occurrences.

bash

echo "aaa bbb ccc" | grep -E "a+"

Output:

aaa

o ?: Matches zero or one occurrence.

bash

echo "abc ac ab" | grep -E "a?c"

Output:

ac
c

3. Exact Repetition ({}):


o Matches a specific number of occurrences.
o Example:

bash

echo "aaa abbb acc" | grep -E "a{3}"

Output:

aaa

4. Grouping (()):
o Groups patterns for combined matching.
o Example:

bash

echo "apple orange banana" | grep -E "(apple|banana)"

Output:

apple
banana

Tools for ERE:

1. egrep:
o egrepis a variant of grep that uses ERE by default.
o Example:

bash

egrep "pattern" file.txt

2. grep -E:
o Enables ERE in standard grep.
o Example:

bash

grep -E "pattern" file.txt

Comparison:

ERE (egrep, grep -


Feature BRE (grep)
E)
Basic Syntax Requires escapes (\) No escapes needed
Advanced Ops Limited Extensive (+, ` ,{}`)

You might also like