Slide 1: Introduction to UNIX
What is UNIX?
Unix is a powerful, multiuser, multitasking operating system originally developed in the 1960s
and 1970s at AT&T's Bell Labs. It has become the foundation for many modern operating
systems and is especially popular in servers, workstations, and academic environments.
Key Features of Unix:
1. Multitasking: Can run multiple processes simultaneously.
2. Multiuser: Multiple users can access system resources at the same time.
3. Portability: Designed to be portable across different hardware platforms.
4. Security and Permissions: File and user-level security are built-in.
5. Modular Design: Built with a philosophy of small, simple programs that can be
combined (via piping).
6. Shell: A command-line interpreter (like sh, bash, ksh, etc.) to interact with the system.
7. File System Hierarchy: Organized as a single tree starting from the root (/).
Command-line interface
When people refer to "CLI Unix", they usually mean interacting with a Unix-based
system through the command-line interface, also known as the shell or terminal.
Shells & Kernel
In Unix (and most operating systems), the shell and the kernel are two essential but very
different components. Here's a clear breakdown:
Kernel — The Brain of the Operating System
What It Is:
The kernel is the core part of the Unix operating system. It manages the system’s hardware and
all low-level operations.
Key Responsibilities:
Process Management: Starts, stops, and manages running programs.
Memory Management: Allocates and tracks system memory.
Device Management: Interfaces with hardware like disk drives, keyboards, etc.
File System Management: Reads/writes to files and directories.
System Calls Handling: Provides a way for software (like the shell) to ask for kernel
services.
The kernel operates in the background and is not directly interacted with by users.
Shell — The User Interface to the OS
What It Is:
The shell is a command-line interpreter. It takes commands from the user and passes them to
the kernel to execute.
Common Shells:
sh (Bourne Shell)
bash (Bourne Again Shell)
zsh (Z Shell)
csh (C Shell)
fish (Friendly Interactive Shell)
What It Does:
Reads your command: ls -l
Parses it and sends a system call to the kernel
Displays the output back to you
Slide 2: Getting Started
Eplain Basic syntax: command [options] [arguments]
Slide 3: File and Directory Commands – Explain Each command
ls – list directory contents
cd – change directory
pwd – print working directory
mkdir – make directory
rmdir – remove directory
Slide 4: File Operations
touch – create a file
cp – copy files
mv – move/rename files
rm – remove files
cat, less, more – view file contents
head, tail, cut commands
Slide 5: File Permissions
chmod – change permissions
chown – change ownership
Symbolic (rwx) and numeric (755) notation
Slide 6: Searching and Finding Files
find – search files
grep – search inside files
locate – fast file location
Slide 7: vi editor
Insert mode/ command mode
Various ways to write in insert mode/command mode
Slide 8: Process Management
ps – list processes
top – real-time process viewer
kill – terminate processes
sar – get historical server stats
Slide 9: Networking Commands
ping – test network connection
ifconfig / ip a – view network interfaces
netstat – network statistics
Slide 10: Miscellaneous Useful Commands
man – manual pages
history – command history
alias – shortcut commands
Slide 11: Loops/Scriptng etc
Shell script
Various loops
sleep mode , running process in background/foreground
process priority
ls command
The ls command in Unix (and Unix-like systems like Linux and macOS) is used to list the
contents of a directory.
Basic Usage:
ls
This lists files and directories in the current working directory.
Common Options:
Option Description
-l Long listing format (includes permissions, ownership, size, date, etc.)
Option Description
-a Show all files, including hidden files (those starting with .)
-h Human-readable file sizes (used with -l)
-R Recursively list subdirectories
-t Sort by modification time, newest first
-S Sort by file size, largest first
-r Reverse the order of sort
--color=auto Show color-coded output (based on file type)
Examples:
ls -l
Long listing of files.
ls -la
Long listing including hidden files.
ls -lh
Long listing with human-readable file sizes.
ls /etc
List contents of the /etc directory.
ls -R
List current directory and all its subdirectories recursively.
Useful commands
Here are some important and commonly used Unix commands, grouped by category for easier
understanding:
File and Directory Management
Command Description
ls List directory contents
Command Description
cd Change directory
pwd Print working directory
mkdir Create a new directory
rmdir Remove empty directory
touch Create an empty file or update timestamp
cp Copy files/directories
mv Move or rename files/directories
rm Remove files or directories (-r for recursive)
File Viewing and Editing
Command Description
cat Display file contents
more / less View file contents page by page
head / tail View the beginning/end of a file
nano / vi / vim Text editors
wc Count lines, words, and characters
grep Search inside files
Searching and Finding
Command Description
find Search for files in a directory hierarchy
locate Find files quickly (uses a database)
grep Pattern matching in files
Command Description
which Show path of a command
whereis Locate source, binary, and man page files
Permissions and Ownership
Command Description
chmod Change file permissions
chown Change file owner and group
umask Set default permissions for new files
System Information
Command Description
uname -a Show system information
top Show running processes (live)
ps Show current processes
df -h Show disk space usage
du -sh Show directory/file size
free -h Show memory usage
uptime How long the system has been running
whoami Show current user
Networking and File Transfers
Command Description
ping Check network connection
Command Description
wget Download files from the internet
curl Transfer data from/to a server
scp Secure copy over SSH
ssh Secure shell (remote login)
netstat Network status
ifconfig / ip a Network interfaces
Process and Job Management
Command Description
kill Kill a process by PID
killall Kill a process by name
jobs List background jobs
bg / fg Resume jobs in background/foreground
nice / renice Set process priority
find command
The find command in Unix is a powerful utility used to search for files and directories in a
directory hierarchy based on various criteria.
Basic Syntax:
find [path] [options] [expression]
path: Where to begin the search (e.g., ., /home/user, etc.)
options/expression: Conditions to match files (e.g., by name, size, type)
Common Use Cases:
1. Find by name:
find . -name "filename.txt"
Find a file named filename.txt in the current directory and subdirectories.
2. Find by type:
find . -type f # Files only
find . -type d # Directories only
3. Find by size:
find . -size +10M # Files larger than 10 MB
find . -size -100k # Files smaller than 100 KB
4. Find by modification time:
find . -mtime -7 # Modified in the last 7 days
find . -mtime +30 # Modified more than 30 days ago
� 5. Find and delete:
Be careful with this one:
find . -name "*.tmp" -delete
Deletes all .tmp files in the current directory and below.
� 6. Find and execute a command:
find . -name "*.log" -exec rm {} \;
Find all .log files and delete them using rm.
Other Useful Options:
Option Description
-iname Case-insensitive match
-empty Find empty files/directories
-user username Files owned by a specific user
Option Description
-perm 755 Files with specific permissions
grep command
The grep command in Unix is used to search for text patterns within files. It's a powerful tool
for filtering and matching lines based on regular expressions.
Basic Syntax:
grep [options] 'pattern' [file...]
'pattern': The text or regex to search for.
[file]: The file(s) in which to search. If omitted, grep reads from standard input (useful
with pipes).
Common Examples:
1. Search for a string in a file:
grep "error" logfile.txt
Finds lines containing "error" in logfile.txt.
2. Case-insensitive search:
grep -i "error" logfile.txt
3. Search recursively in all files in a directory:
grep -r "TODO" .
4. Show line numbers:
grep -n "main" code.c
5. Show only matching part (not the whole line):
grep -o "http[s]*://[a-zA-Z0-9./?=_-]*" file.txt
6. Count matching lines:
grep -c "404" access.log
7. Invert match (show lines not containing pattern):
grep -v "success" output.txt
Useful Options:
Option Description
-i Ignore case
-v Invert match
-r or -R Recursive search
-n Show line numbers
-c Count of matching lines
-l Show only file names with matches
-o Show only matched text
-e Use multiple patterns
--color=auto Highlight matches in color
With Pipes:
cat file.txt | grep "pattern"
OR
ps aux | grep nginx
sed command
The sed command in Unix is a stream editor used to perform text transformations on input
from a file or standard input. It’s commonly used for find-and-replace, deleting lines, inserting
text, and more.
Basic Syntax:
sed [options] 'command' [file...]
Common Use Cases:
1. Find and replace (substitute):
sed 's/old/new/' file.txt
Replaces first occurrence of "old" with "new" per line.
sed 's/old/new/g' file.txt
Replaces all occurrences in each line.
sed -i 's/old/new/g' file.txt
In-place replace (modifies the file directly).
2. Delete lines:
sed '2d' file.txt
Deletes line 2.
sed '/pattern/d' file.txt
Deletes lines matching the pattern.
3. Print specific lines:
sed -n '5p' file.txt
Prints only line 5.
bash
CopyEdit
sed -n '/error/p' file.txt
Prints only lines containing "error".
4. Insert or append lines:
sed '2i\New line above 2' file.txt
Inserts text before line 2.
sed '2a\New line below 2' file.txt
Appends text after line 2.
5. Change line content:
sed '3c\This is the new line 3' file.txt
Replaces line 3 entirely.
Useful Options:
Option Description
-i Edit files in place (no backup)
-n Suppress automatic printing (use with p)
-e Add multiple commands
With Pipes:
echo "apple banana" | sed 's/banana/orange/'
Outputs: apple orange
Pipes in Unix
In Unix, a pipe (|) is used to pass the output of one command as input to another command.
It's a way to chain multiple commands together, creating powerful and efficient workflows.
Basic Syntax:
command1 | command2
The stdout (standard output) of command1 becomes the stdin (standard input) of
command2.
Common Examples:
1. View only matching lines from a file:
cat file.txt | grep "error"
(But more efficiently written as: grep "error" file.txt)
2. Count number of lines in a file:
cat file.txt | wc -l
Or simply:
wc -l < file.txt
3. Sort and remove duplicate lines:
cat file.txt | sort | uniq
4. Find large files:
du -ah . | sort -rh | head -n 10
Shows the 10 largest files/directories in current folder.
5. Monitor logs in real time:
tail -f /var/log/syslog | grep "CRON"
6. List running processes and filter:
ps aux | grep apache
✅ Why Use Pipes?
Efficient: Avoids using temporary files.
Modular: Each command does one job well.
Flexible: Combine basic commands to perform complex tasks.
Redirection in Unix
Redirection is a way to control where the input and output of commands go — to/from files,
devices, or other commands.
✅ Input Redirection (<)
Sends the contents of a file as input to a command.
command < input.txt
Example:
sort < unsorted.txt
Sorts the contents of unsorted.txt.
✅ Output Redirection (>)
Redirects standard output to a file (overwrites file).
command > output.txt
Example:
ls > filelist.txt
Saves the output of ls to filelist.txt.
✅ Append Output (>>)
Appends standard output to the end of a file.
command >> output.txt
Example:
echo "New log entry" >> logfile.txt
✅ Redirect Standard Error (2>)
Redirects standard error to a file.
command 2> error.log
Example:
grep "word" missingfile.txt 2> errors.txt
Redirect Both Stdout and Stderr
command > output.txt 2>&1
vi Mode in Unix
vi (or its improved version, vim) is a powerful text editor found on nearly all Unix and Linux
systems. It operates in multiple modes, which can confuse new users but offers great power
once learned.
The Three Key Modes in vi:
1. Normal Mode (default on open)
Used for navigation, copying/pasting, deleting, and switching modes
Press Esc to return here from any other mode
🔹 Examples:
dd # delete current line
yy # yank (copy) current line
p # paste after the current line
u # undo
/word # search for "word"
2. Insert Mode
Used for typing and editing text
Enter from Normal Mode using:
Command What It Does
I Insert before cursor
I Insert at beginning of line
A Append after cursor
A Append at end of line
O Open new line below
O Open new line above
🔹 Exit back to Normal Mode by pressing Esc.
3. Command-Line Mode (Ex Mode)
Used for saving, quitting, search and replace, etc.
Enter from Normal Mode by typing : (colon)
🔹 Common commands:
:w # write (save) file
:q # quit
:wq # write and quit
:q! # quit without saving
:%s/old/new/g # replace all occurrences of "old" with "new"
Loops in Unix
In Unix (especially in shell scripting with bash or sh), loops allow you to execute a block of
code repeatedly. Here are the main types of loops with examples:
� 1. for Loop
✅ Syntax:
bash
CopyEdit
for var in item1 item2 item3
do
# commands
done
Example:
for file in *.txt
do
echo "Processing $file"
done
➡ This loops over all .txt files in the directory.
� 2. while Loop
✅ Syntax:
bash
CopyEdit
while [ condition ]
do
# commands
done
Example:
bash
count=1
while [ $count -le 5 ]
do
echo "Count is $count"
count=$((count + 1))
done
� 3. until Loop
✅ Syntax:
until [ condition ]
do
# commands
done
Example:
count=1
until [ $count -gt 5 ]
do
echo "Count is $count"
count=$((count + 1))
done
4. Infinite Loop
Syntax:
while true
do
# commands
done
1. � Shell Scripting
Automate tasks using logic, variables, loops, and functions.
Example:
#!/bin/bash
for file in *.log; do
if grep -q "ERROR" "$file"; then
echo "Found error in $file"
fi
done
2. Process Management
ps, top, htop, nice, renice, kill, pkill, xargs
View, prioritize, and terminate processes.
Example:
ps aux | grep apache
kill -9 <PID>
3. User and Permission Management
chmod, chown, umask, sudo, /etc/passwd, /etc/shadow
Example:
chmod 750 script.sh # rwxr-x---
chown user:group file
4. Networking & Sockets – Explain each command
netstat, ss, ifconfig, ip, ping, nc, curl, scp, rsync
Example:
nc -zv 192.168.1.100 22 # Check if SSH port is open
File System & Storage
mount, umount, df, du, lsblk, fdisk, lsof, find, inotify
Example:
find /var/log -type f -mtime +7 -exec rm -f {} \;
Text Processing Powerhouse (Pipelines) – Explain each
Combine tools like:
awk, sed, cut, tr, uniq, sort, tee, xargs
Example:
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr
Shows top IP addresses by access count.
Performance Monitoring commands – Explain Each
top, htop, vmstat, iostat, sar, uptime, strace, lsof
Cron Jobs (Scheduling) – Prohibited in Citi due to less security
Use crontab to automate jobs.
Example (Edit Cron Table):
crontab -e
Cron Format:
bash
CopyEdit
# ┌──────── minute (0 - 59)
# │ ┌────── hour (0 - 23)
# │ │ ┌──── day of month (1 - 31)
# │ │ │ ┌── month (1 - 12)
# │ │ │ │ ┌─ day of week (0 - 7)
# │ │ │ │ │
# * * * * * command-to-execute
Writing a Daemon Script (Example)
#!/bin/bash
while true; do
echo "Running..." >> /tmp/mydaemon.log
sleep 60
done
Run in background:
nohup ./mydaemon.sh &