Unix
Unix
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:
bash
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
2. Displaying Variables:
o You can use echo to show the value of shell variables.
o Example:
bash
echo $HOME
Output: /home/username
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
5. Suppressing Newline:
o To avoid adding a newline at the end, use the -n option.
o Example:
bash
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
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
bash
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
bash
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:
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
bash
uname -a
uname -s
bash
uname -r
bash
uname -v
bash
uname -m
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
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
bash
cd $HOME
bash
export HOME=/new/home/directory
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).
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 (.)
bash
./script.sh
cp file.txt .
Dotdot (..)
bash
cd ..
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:
bash
cd directory_path
o Example:
bash
cd /home/username/documents
bash
cd
bash
cd $HOME
bash
cd ..
bash
cd .
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
1. Basic Usage:
o Syntax:
bash
mkdir directory_name
o Example:
bash
mkdir my_folder
bash
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
bash
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
bash
rmdir -p parent/child
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:
bash
ls
Common Options:
bash
ls -l
Output:
bash
ls -a
ls -R
bash
ls -lh
bash
ls -lt
bash
ls -lS
Combining Options:
bash
ls -lha
This displays all files (including hidden), detailed information, and sizes in a readable format.
/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:
Syntax:
bash
cat file.txt
bash
Concatenating Files:
bash
cat file1.txt file2.txt > combined.txt
bash
cat -n file.txt
Redirecting Output:
bash
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
Copying to a Directory:
bash
cp source.txt /path/to/directory/
This places the file in the specified directory, keeping the original name.
bash
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:
bash
cp -i source.txt destination.txt
Verbose Output:
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
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:
bash
rm -f filename.txt
Combining Options:
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
Moving a File:
bash
mv filename.txt /path/to/destination/
Example:
bash
mv file1.txt /home/user/documents/
You can move a file to a new directory and rename it in the process:
bash
mv filename.txt /path/to/destination/newname.txt
bash
mv file1.txt file2.txt /path/to/destination/
Interactive Move:
bash
mv -i file1.txt /path/to/destination/
Verbose Output:
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:
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).
bash
ls -l | more
This lets you navigate through the ls output one screen at a time.
Navigation Keys:
Options:
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
Specifying a Printer:
bash
lp -d printer_name filename.txt
bash
lp -n 2 filename.txt
bash
bash
lpstat
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:
bash
file filename
Example:
bash
file document.pdf
Key Features:
bash
Options:
bash
file -i filename
Output:
-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:
bash
wc filename.txt
Example output:
10 50 250 filename.txt
This shows:
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
bash
Output:
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
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
bash
bash
bash
bash
Exit Codes:
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
Example:
apple
banana
cherry
file2.txt:
banana
cherry
date
Run:
bash
comm file1.txt file2.txt
Output:
apple
date
banana
cherry
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
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
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
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.
Example:
apple
banana
cherry
apple
blueberry
cherry
Running:
bash
diff file1.txt file2.txt
Output:
2c2
< banana
---
> blueberry
Common Options:
bash
bash
bash
The tar command in UNIX is used to create, extract, and manage archive files. Here's how you
can create an archive file:
bash
tar -cf archive_name.tar file1 file2
Example:
bash
tar -cf archive.tar file1.txt file2.txt
Including Directories:
bash
tar -cf archive.tar directory_name/
1. Gzip Compression:
o Use the -z option to compress the archive:
bash
2. Bzip2 Compression:
o Use the -j option for better compression:
bash
Verbose Mode:
bash
tar -cvf archive.tar file1.txt file2.txt
Helpful Options:
Excluding Files:
bash
bash
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.
bash
Decompressing Files:
bash
gzip -d filename.txt.gz
Or:
bash
gunzip filename.txt.gz
bash
gzip -k filename.txt
bash
gzip -9 filename.txt
Checking Compression Ratio:
bash
gzip -l filename.txt.gz
To uncompress a file using gunzip in a terminal, you can follow this straightforward method:
bash
gunzip filename.gz
To archive files into a .zip file in Unix, you can use the zip command. Here's how you can do
it:
bash
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:
bash
unzip archive_name.zip
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
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
Use the -r (recursive) option to delete the directory and all its contents:
bash
rm -r directory_name
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:
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:
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.
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
bash
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:
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:
bash
Here, 755 sets read, write, and execute for the owner, and read and execute for
group/others.
bash
When changing file permissions in Unix, you can use relative permissions or absolute
permissions. Here's a brief explanation:
Relative Permissions
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
bash
chmod g-w file
bash
chmod o=r file
Absolute Permissions
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
bash
chmod 644 file
Syntax:
bash
chown [options] new_owner filename
Examples:
bash
bash
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
This will change ownership for the directory and all files/subdirectories within it.
View Ownership:
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:
bash
bash
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
Verifying Changes:
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
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
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:
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:
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.
Example:
bash
umask
bash
umask 027
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:
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
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:
touch filename
bash
touch newfile
bash
bash
touch -a filename
bash
touch -m filename
bash
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:
bash
o Replace "filename" with the name (or pattern, e.g., *.txt) of the file you are
looking for.
2. Find Files by Type:
bash
bash
o Finds files larger than 1 megabyte. (+ for larger, - for smaller, or = for exact size).
4. Find Files Modified Recently:
bash
bash
find /path/to/search -perm 644
bash
bash
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:
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:
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.
bash
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
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.
1. Backslash (\):
o A backslash is used to escape a single special character.
o Example:
bash
bash
bash
name="Alice"
echo "Hello, $name"
Outputs: Hello, Alice
bash
Quoting in Unix is essential for controlling how the shell interprets characters in commands or
scripts. There are three main types of quoting:
bash
Preserve the literal value of most characters, except $, backticks (\``), and backslash
(\`).
Allow variable expansion and command substitution.
Example:
bash
name="Alice"
echo "Hello, $name"
3. Backslash (\)
Escapes a single special character outside of quotes.
Example:
bash
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:
bash
ls > files.txt
bash
bash
bash
5. Error Redirection:
o Redirects error messages (stderr) to a file using 2>.
o Example:
bash
bash
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:
bash
cat
bash
bash
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.
bash
ls > files.txt
bash
bash
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.
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:
bash
bash
bash
/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
/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
2. Pipe (|):
Pipes connect the output (stdout) of one command to the input (stdin) of another.
Example:
bash
3. tee:
teereads from stdin and writes to both stdout and a file simultaneously.
Example:
bash
bash
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:
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.
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.
bash
ps
Displays a simple list of your processes.
bash
ps -aux
bash
ps -axjf
bash
bash
ps -eo pid,user,%cpu,%mem,command
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.
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
bash
ps -axjf
top
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:
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.
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.
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.
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.
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.
bash
sleep 60 &
This starts the sleep command as a background job, allowing you to use the terminal
immediately.
bash
jobs
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
Redirect Output:
o To customize the output file, redirect stdout and stderr.
bash
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
bash
nice -n 10 my_program
This starts my_program with a niceness of 10, lowering its CPU priority.
You can use the renice command to change the priority of an already running process.
Syntax:
bash
o Example:
bash
renice 15 -p 1234
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.
bash
bash
kill [PID]
bash
kill -9 [PID]
bash
kill -1 [PID]
Using kill:
o Sends signals to processes by specifying their Process ID (PID).
o Example to terminate a process:
bash
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.
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.
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
bash
bg %1
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
bash
fg %1
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.
1. Basic Command:
o Simply type jobs to display the list of jobs.
o Example:
bash
jobs
Output (example):
bash
fg %job_number
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.
1. Using Ctrl+Z:
o While a job is running in the foreground, press Ctrl+Z to suspend it.
o Example:
bash
sleep 100
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
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:
bash
jobs
Example output:
bash
ps
bash
kill %1
bash
kill 1234
bash
kill -9 %1
Or:
bash
kill -9 1234
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:
bash
at [time]
Examples:
o Schedule a job for 3:00 PM:
bash
at 3:00 PM
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
bash
atq
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
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.
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
bash
export DATABASE_URL=jdbc:mysql://localhost:3306/mydb
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.
bash
printenv
Or:
bash
env
bash
echo $HOME
bash
export VAR_NAME=value
bash
Environment Variables:
1. HOME:
o Stores the current user's home directory.
o Example:
bash
echo $HOME
bash
echo $PATH
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
4. USER:
o Represents the username of the current user.
o Similar to LOGNAME, but used in different contexts.
o Example:
bash
echo $USER
bash
echo $TERM
bash
echo $PWD
7. PS1:
o Defines the primary prompt string, which appears when you interact with the
shell.
o Example:
bash
echo $PS1
bash
echo $PS2
View a Variable:
bash
echo $VARIABLE_NAME
bash
export VARIABLE_NAME=new_value
bash
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.
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
bash
pr -2 filename
bash
pr -n filename
bash
bash
pr -o 5 filename
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
bash
head -n 5 filename.txt
bash
head -c 50 filename.txt
The tail command displays the last few lines of a file (default is 10).
Example:
bash
tail filename.txt
bash
tail -n 7 filename.txt
bash
bash
tail -f filename.txt
To extract a specific section of the file, combine head and tail with a pipe (|).
Example:
o Display lines 11 to 20:
bash
bash
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
bash
bash
bash
Extracts the usernames (1st field) from the system's password file.
bash
ls | cut -d '.' -f 2
bash
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 ...
bash
paste file1.txt file2.txt
If file1.txt contains:
A
B
C
1
2
3
A 1
B 2
C 3
bash
Output:
A,1
B,2
C,3
bash
Output:
A A
B B
C C
paste
Enter:
A
B
C
Ctrl+D
Output:
A
B
C
bash
paste -s file1.txt
Output:
A B C
bash
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]
bash
uniq input.txt
If input.txt contains:
apple
apple
banana
orange
orange
apple
banana
orange
bash
uniq -d input.txt
Output:
apple
orange
bash
uniq -u input.txt
Output:
banana
bash
uniq -c input.txt
Output:
2 apple
1 banana
2 orange
bash
uniq -i input.txt
bash
bash
uniq -f 1 input.txt
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]
1. Translate Characters:
o Replace characters in SET1 with corresponding characters in SET2.
o Example:
bash
HELLO
bash
Deletes vowels:
hll wrld
bash
Collapses spaces:
bash
ABC
bash
123456
bash
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]
1. Basic Search:
o Search for a pattern in a file.
o Example:
bash
bash
bash
bash
bash
bash
bash
bash
bash
bash
1. Literal Characters:
o Match exact characters as they appear.
o Example:
bash
2. Wildcard (.):
o Matches any single character.
o Example:
bash
3. Anchor Characters:
o ^: Matches the beginning of a line.
bash
bash
5. Ranges (-):
o Specifies a range of characters within brackets.
o Example:
bash
6. Repetition (\):
o Match repeated occurrences of a pattern.
o Example:
bash
Matches "aa".
bash
8. Grouping (\(...\):
o Groups patterns together to perform combined matches.
o Example:
bash
Practical Applications:
Search for Patterns in Files:
bash
bash
bash
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.
1. Alternation (|):
o Matches one of several patterns.
o Example:
bash
Output:
apple
banana
bash
Output:
aaa
bash
Output:
ac
c
bash
Output:
aaa
4. Grouping (()):
o Groups patterns for combined matching.
o Example:
bash
Output:
apple
banana
1. egrep:
o egrepis a variant of grep that uses ERE by default.
o Example:
bash
2. grep -E:
o Enables ERE in standard grep.
o Example:
bash
Comparison: