Unix Notes
Unix Notes
The Unix architecture is designed in a layered approach, structured like concentric circles where
each layer has a distinct responsibility. It isolates the hardware from the user application to
ensure stability and security.
● Layer 1: Hardware: The innermost core consisting of physical components like the CPU,
RAM, and hard disks.
● Layer 2: Kernel: The heart of the OS. It interacts directly with the hardware. It manages
memory, processes, file systems, and device drivers.
● Layer 3: The Shell: The command interpreter. It acts as an interface between the user
and the Kernel. When you type a command, the shell reads it, interprets it, and requests
the Kernel to execute it. Examples: Bash, Bourne Shell (sh), C Shell (csh).
● Layer 4: Application Programs / Utilities: The outermost layer where users execute
commands, scripts, compilers, and applications (e.g., vi, grep, gcc).
● The Kernel: It is the core program that loads into memory upon booting and remains
active until shutdown. Its core duties include:
○ Process Scheduling: Deciding which process gets CPU time.
○ Memory Management: Allocating and freeing RAM allocations.
○ Device Management: Communicating with hardware via device drivers.
● System Call Interface: A user program cannot access the hardware directly due to
safety protocols. When a program needs an operation done (like reading a file from a
disk), it makes a System Call. The System Call interface acts as a secure gateway that
transitions the CPU from User Mode to Kernel Mode to perform safe hardware execution.
Common examples include fork(), open(), read(), and write().
The Unix file system follows a hierarchical, inverted tree structure starting from the Root
directory, represented by a single forward slash (/).
/ (Root)
______|___________________________________________
| | | | | | | |
/bin /boot /dev /etc /home /lib /root /usr
● /bin: Essential binary executables needed in single-user mode (e.g., ls, cp, mv).
● /boot: Contains files required to boot the system (e.g., Linux Kernel, GRUB bootloader).
● /dev: Essential device files (e.g., /dev/sda for hard disks, /dev/null).
● /etc: Holds system configuration files (e.g., /etc/passwd).
● /home: The default directory containing personal folders for regular users.
● /lib: Core shared library files needed by binaries in /bin and /sbin.
● /root: The dedicated home directory for the Superuser (administrator).
● /usr: Sub-hierarchy containing user binaries, documentation, and source code.
Short Questions
1. What is Unix?
2. What is Linux?
Since Linux is just a kernel, companies bundle it with graphical interfaces, packages, and
installation tools to create a usable operating system called a distribution (distro). Examples:
Ubuntu, Fedora, Red Hat Enterprise Linux (RHEL), Debian.
4. Define POSIX.
POSIX is a set of IEEE standards that defines the application programming interface (API) for
software compatible with variants of the Unix operating system.
System calls are programmatic requests made by a running application to the operating system
kernel to perform privileged operations like hardware input/output or process creation.
● ls: Lists directory contents. (Options: -l for long detailed listing, -a to show hidden files).
ls -la
Every file has an ownership structure: User (u), Group (g), and Others (o). Permissions
consist of Read (r=4), Write (w=2), and Execute (x=1).
You change security settings via chmod using two methods:
● Symbolic Mode:
chmod u+x [Link] # Adds execute permission to the owner
chmod g-w [Link] # Removes write permission from the group
An inode (Index Node) is a low-level data structure on a Unix file system that stores metadata
about a file. Crucially, the inode does not store the file name or actual file content.
An inode contains:
● File size
● Device ID
● User ID (UID) and Group ID (GID) of the owner
● File permissions (rwx)
● Timestamps (atime, mtime, ctime)
● Pointers to data blocks on the physical drive where content is stored.
Significance: Unix tracks files internally by their unique inode number, not their text name. This
allows files to be renamed instantly without moving actual underlying data blocks.
6. Explain the touch command and file timestamps (atime, mtime, ctime).
The touch command creates an empty file if it doesn't exist, or updates its timestamps if it does.
● atime (Access Time): Last time a file's content was read/opened (e.g., via cat or grep).
● mtime (Modification Time): Last time a file's content was edited or changed.
● ctime (Change Time): Last time a file's metadata changed (e.g., renaming it or altering
permissions via chmod).
Using touch options:
● -d: Update to an explicit date string.
touch -d "2025-10-12 14:30" [Link]
7. Explain the use of pipes (|), redirection (>, >>) and tee command.
● Append Redirection (>>): Appends standard output to the end of a file without
overwriting.
echo "New line" >> [Link]
● Pipe (|): Passes the standard output of one command to the input of another.
ls | grep ".txt" # Streams the file list directly into grep
● tee Command: Splits output stream into two directions. It displays output on the terminal
screen and saves it into a file simultaneously.
ls | tee output_log.txt
Short Questions
1. What is an inode?
An inode is a metadata record on a disk containing critical structural details about a file
(permissions, owner, size, block locations), identified by an integer value.
A hard link is a direct duplicate directory reference pointing to an existing file's underlying inode.
A soft link is an independent shortcut file storing a text string path to another file.
The cut command slices vertical columns or byte strings out of file streams.
● Column Selection by Delimiter (-d and -f): Extracts precise column fields using a
character separator.
# Extract 1st and 3rd field from a colon-separated file
cut -d ":" -f 1,3 [Link]
While cut extracts columns, paste joins separate file contents horizontally, column-by-column,
separated by default with a tab character.
# Combine contents of [Link] and [Link] side-by-side
paste [Link] [Link]
# Custom delimiter option (-d)
paste -d "," [Link] [Link]
The split command breaks large monolithic files into manageable smaller pieces.
● Split by Line Count (-l):
split -l 100 [Link] small_chunk_
# Creates small_chunk_aa, small_chunk_ab, etc., each with 100
lines.
● join: Merges lines from two sorted files based on a shared common key column.
# Merges rows matching on key column 1
join [Link] [Link]
The tr command converts, maps, or purges target character characters coming via standard
input. It cannot read files directly; it requires standard input piping.
● Case Conversion:
echo "hello" | tr 'a-z' 'A-Z' # Output: HELLO
● Deletion (-d):
echo "Phone: 123-45" | tr -d '-' # Output: Phone: 12345
Short Questions
1. What is wc command?
wc (Word Count) counts the lines (-l), words (-w), and characters (-c) present in a target file.
It directs the sort utility to parse fields as numeric mathematical quantities rather than
alphabetical string sequences.
diff identifies differences textually across lines, whereas cmp flags the first physical byte offset
mismatch location between two target files.
It merges database-like fields from two separate relational data files horizontally into a
combined output using a matching identity column.
uniq filters out repeating identical matching text values, provided they sit directly next to each
other sequentially.
2. Explain file compression and archiving using zip, unzip, and gzip.
● gzip: Compresses a standalone file down using the Lempel-Ziv coding algorithm. It
replaces the original file with a .gz extension.
gzip [Link] # Creates [Link]
● gunzip: Reverts a compressed .gz asset back to its original raw file form.
● zip: Compresses multiple files and directories into a single .zip package.
zip [Link] [Link] [Link]
● unzip: Extracts packed components out of an existing .zip archive file structure.
The tar (Tape Archive) utility bundles collections of directory structures into a single solid file
object (.tar), which can then be compressed.
● Create an Archive (-cvf): c (create), v (verbose output logging), f (file target definition).
tar -cvf [Link] /home/user/docs
Short Questions
1. What is gzip?
gzip is a file compression tool that reduces single file footprints. It outputs files with the .gz
extension.
2. What is tar?
tar is an archiving tool used to collect groups of separate physical files and folders into one
collective bundle.
3. What is bc command?
who identifies user account logins, terminal lines used, and connection times on the active Unix
machine.
Module 5: VI Editor
Long Questions
1. Explain different modes of VI editor.
The vi text editor relies heavily on switching operational states to navigate and edit files without
mouse input. It operates in three main modes:
1. Command Mode: The default operational entry state. Key presses map to movement
and formatting commands (e.g., dd to delete a line) rather than printing letters to the
screen. Pressing Esc returns you here from other modes.
2. Insert Mode: The state used for entering text directly into the file buffer. You enter it from
Command Mode by pressing keys like i, I, a, or o.
3. Last-Line (Ex) Mode: Activated by typing a colon (:) from Command Mode. It allows you
to save files, quit the editor, run shell commands, or perform global search-and-replace
actions.
● Inserting text:
○ i: Insert text before current cursor position.
○ a: Append text after current cursor position.
○ o: Open an empty new line below current cursor row.
● Deleting text:
○ x: Delete a single character under the cursor.
○ dw: Delete from the cursor position to the end of the current word.
○ dd: Delete the current line.
● Copy & Paste:
○ yy: "Yank" (copy) the current line.
○ p: Paste copied buffer content below current cursor line.
Short Questions
1. How do you enter insert mode?
Pressing lowercase i from Command Mode enters Insert Mode before the current cursor
location.
:q closes the editor only if all changes are saved. :q! forces an exit immediately, discarding all
unsaved modifications.
4. What is :wq?
It is a dual command that writes (saves) changes to disk and immediately quits the active vi
session.
In Command Mode, pressing u reverses the last modification. Pressing Ctrl + r redoes an action
that was undone.
grep (Global Regular Expression Print) searches line-by-line through input files for lines
containing matches to a specified pattern.
● Syntax: grep [options] pattern filename
● Common Options:
○ -i: Ignores alphabetical character casing.
○ -v: Inverts the match; displays lines that do not match the pattern.
○ -c: Displays only the count of matching rows.
○ -n: Prefixes each output line with its original line number.
● Examples:
grep -i "admin" [Link] # Case-insensitive search
grep -v "success" [Link] # Prints only non-success lines
Advanced usage involves leveraging Regular Expressions (Regex) anchors and wildcards:
● ^ (Caret): Anchors a pattern to match only at the absolute beginning of a line.
grep "^Error" [Link] # Lines starting with "Error"
● BEGIN block: Evaluated exactly once before any text rows are read from the input file. It
is useful for setting up global variables or printing report headers.
● END block: Evaluated exactly once after all input file records have been completely
processed. It is useful for printing final summaries or total tallies.
awk 'BEGIN {print "--- START ---"} {print $1} END {print "--- FINISHED
---"}' [Link]
5. Discuss AWK with comparison and arithmetic operators.
● FS (Field Separator): Defines the character that splits columns in the input file. Default is
whitespace.
● OFS (Output Field Separator): Defines the character used to separate columns when
printing. Default is a space.
# Process a comma-separated file (CSV) and output it as tab-separated
awk 'BEGIN {FS=","; OFS="\t"} {print $1, $2}' [Link]
● Arithmetic Functions: int(x) (truncates to integer), sqrt(x) (square root), rand() (random
number).
● String Functions: * length(str): Returns character length.
○ substr(str, start, len): Extracts a substring.
○ tolower(str) and toupper(str): Case mapping.
awk '{print toupper($1), length($1)}' [Link]
9. Explain search and substitute functions in AWK.
awk provides sub() and gsub() functions for regular expression modifications.
● sub(regex, replacement, target): Replaces only the first matching instance on the line.
● gsub(regex, replacement, target): Globally replaces all matching instances on the line.
# Globally replace "USA" with "United States" in column 3
awk '{ gsub(/USA/, "United States", $3); print }' [Link]
Short Questions
1. What is grep?
grep is a command-line tool used to scan a data stream or file text for any lines that match a
specified regular expression pattern.
2. What is AWK?
awk is a domain-specific, data-driven programming language designed for complex parsing, text
restructuring, and report generation from columnar text data.
FS is the character marker that dictates how input lines are split into variables, whereas OFS
defines how fields are separated in the output.
print appends a newline automatically and handles simple data dumps, while printf allows you to
define exact column widths and data type formatting.
During its lifecycle, a Unix process transitions through several operational states:
1. Created/New: The process is being initialized by the OS.
2. Ready/Runnable: The process is fully loaded in memory and waiting in a queue for the
CPU scheduler to assign it execution time.
3. Running: The CPU is actively executing the process instructions.
4. Blocked/Sleeping (Waiting): The process cannot continue until an external event or I/O
operation completes (e.g., waiting for user keyboard input).
5. Zombie: The process has finished execution but still occupies an entry in the system
process table so its parent can read its exit status code.
● fork(): Creates a new child process by making an exact duplicate copy of the calling
parent process. After invocation, both processes run concurrently.
○ Return Value: Returns 0 inside the child process, and returns the Child's PID inside
the parent process.
● getpid(): Returns the unique Process ID (PID) of the active calling process.
● getppid(): Returns the Process ID of the Parent (PPID) that spawned the calling process.
● wait(): Suspends the parent process execution until one of its child processes terminates.
A Zombie process is a process that has completed its task and terminated, but its entry
remains in the process table. This happens because its parent process has not yet read its exit
status using the wait() system call.
Example scenario: If a child finishes its task while its parent is stuck executing an infinite
while(1) loop without calling wait(), the child becomes a zombie. It consumes no memory or
CPU, but it ties up a process table slot. If the process table fills up with zombies, no new
programs can be started.
● init Process: The grandfather of all processes. It is the first user-space process started
by the kernel during booting, always carrying a PID of 1. It runs continuously until
shutdown and is responsible for bringing up system services and adopting orphaned child
processes.
● Login Process:
1. init reads configuration files and spawns a getty process on communication lines.
2. getty sets up terminal lines and waits for a user to type a username.
3. Once entered, getty execs the login program, which prompts for a password and
verifies it against /etc/passwd.
4. If authenticated, login invokes the user's default shell (e.g., bash), presenting the
command prompt.
● top: Provides a real-time, interactive, dynamic view of active processor activity and
memory usage. It continuously lists the top resource-consuming processes.
● vmstat (Virtual Memory Statistics): Displays summaries of system memory, swap
utilization, CPU activity, and I/O traps. It is useful for identifying resource performance
bottlenecks.
Unix uses a priority scheduling scale. The scheduling priority of a process can be altered using
a metric called a nice value.
● Nice values range from -20 (highest priority) to 19 (lowest priority).
● A high nice value makes the process "nice" to other programs by yielding CPU time.
● Usage:
nice -n 10 backup_script.sh # Runs the script with a lower
priority
Short Questions
1. What is a process?
A process is a program in execution, complete with its own memory space, registers, stack
pointer, and tracking state.
A terminated process whose exit status has not been retrieved by its parent, leaving its identifier
stuck in the system process table.
3. What is fork()?
A core system call that creates an identical concurrent duplicate child process from the parent
process.
It displays a real-time, auto-refreshing monitor of system resource metrics and a list of running
processes sorted by resource consumption.
The Shell is a command-line interpreter that reads user commands and passes them to the
operating system kernel for execution.
Common Types of Shells:
● Bourne Shell (sh): The original standard Unix shell developed by Stephen Bourne.
● Bash (Bourne Again Shell): The standard default shell for most modern Linux systems.
It builds on sh with enhancements like tab-completion and command history.
● C Shell (csh): Uses a syntax that resembles the C programming language.
● Korn Shell (ksh): Combines features of both the Bourne and C shells.
● System Variables: Predefined variables created and maintained by the shell environment
to manage system settings. They are usually written in uppercase letters.
○ HOME: The path to the current user's home directory.
○ PATH: A colon-separated list of directories the shell searches to locate executable
commands.
○ USER: The name of the logged-in user.
● User-Defined Variables: Temporary variables created by the user within a shell script.
They are case-sensitive and assigned using the = operator (without spaces).
my_name="Alice"
echo $my_name
These characters control how the shell processes special characters (quoting mechanisms):
● Double Quotes ("): Weak quoting. It preserves the literal value of most characters, but
allows variable expansion ($) and command substitution to execute.
echo "Home is $HOME" # Output: Home is /home/user
● Single Quotes ('): Strong quoting. It preserves the literal value of every single character
inside the quotes. No substitutions take place.
echo 'Home is $HOME' # Output: Home is $HOME
● Backslash (\): Escapes the character immediately following it, stripping away any special
meaning it has to the shell.
echo "The price is \$10" # Output: The price is $10
4. Explain command substitution with examples.
Command substitution allows the output of a command to be captured and assigned directly to
a variable or used within another statement. This can be done using backticks (`...`) or the
modern $() syntax.
# Using modern syntax (Recommended)
current_users=$(who | wc -l)
echo "There are $current_users users logged in."
# Using legacy backticks
today=`date`
echo "Today is $today"
The if statement evaluates a condition. If the condition is true, the corresponding block of code
runs. The block is closed with fi.
Syntax Example:
#!/bin/bash
echo "Enter a score out of 100:"
read score
if [ $score -ge 50 ]
then
echo "Result: Passed"
else
echo "Result: Failed"
fi
● while loop: Continues executing a block of code as long as the specified condition
remains true.
count=1
while [ $count -le 3 ]
do
echo "Value: $count"
count=$((count + 1))
done
● until loop: Continues executing code as long as the specified condition evaluates to
false (it stops once the condition becomes true).
num=1
until [ $num -gt 3 ]
do
echo "Number: $num"
num=$((num + 1))
done
#!/bin/bash
# Program to calculate factorial
echo "Enter a number:"
read num
fact=1
temp=$num
while [ $num -gt 1 ]
do
fact=$((fact * num))
num=$((num - 1))
done
echo "The factorial of $temp is $fact"
#!/bin/bash
# Program to check if a number is prime
echo "Enter an integer:"
read n
if [ $n -lt 2 ]
then
echo "$n is not a prime number."
exit 0
fi
i=2
is_prime=1
while [ $((i * i)) -le $n ]
do
if [ $((n % i)) -eq 0 ]
then
is_prime=0
break
fi
i=$((i + 1))
done
if [ $is_prime -eq 1 ]
then
echo "$n is a prime number."
else
echo "$n is not a prime number."
fi
#!/bin/bash
# Program to find the largest of three inputs
echo "Enter three numbers separated by spaces:"
read a b c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "$a is the largest number."
elif [ $b -gt $a ] && [ $b -gt $c ]
then
echo "$b is the largest number."
else
echo "$c is the largest number."
fi
This script processes arguments passed to it when it is run (e.g., ./[Link] 10 20). $1 captures
the first argument, $2 captures the second, and $# contains the total number of arguments
passed.
#!/bin/bash
# Program showing command-line arguments execution
if [ $# -lt 2 ]
then
echo "Error: Please provide at least two arguments."
echo "Usage: $0 num1 num2"
exit 1
fi
sum=$(($1 + $2))
echo "Script Name: $0"
echo "Total Arguments Received: $#"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "Sum of the two arguments: $sum"