UNIX Study Notes
1. Introduction to Unix
• Unix is a multi-user, multitasking operating system
• Developed in the 1970s at Bell Labs
• Known for its portability, stability, and security
• Uses a modular design with tools and utilities
• Basis for Linux, macOS, and other modern OS
Explanation:
Unix is a powerful, stable, and secure operating system originally developed for academic
and industrial use. Its core strengths lie in multitasking and multi-user capabilities, allowing
many users to operate and run processes simultaneously without conflict. It was designed
with simplicity, efficiency, and the philosophy of building small programs that work together.
Unix forms the foundation of many modern operating systems. Tools such as piping,
redirection, shell scripting, and powerful command-line utilities make it ideal for automation,
server administration, and software development. Today, its influence extends through Linux
distributions, BSD, and macOS.
Examples:
1. uname -a prints system information
2. Unix systems include Solaris, AIX, and Linux
2. File System Hierarchy
• Tree-structured with / as the root directory
• Key directories: /bin, /etc, /home, /usr, /var
• Device files are found in /dev
• Hidden files start with . and store configs
• Everything in Unix is treated as a file
Explanation:
The Unix file system is structured as a tree, beginning with the root directory /. All system
components and user data are organized under this structure. Each directory has a specific
purpose, such as /bin for essential binaries, /etc for configuration files, and /home for user-
specific directories.
Understanding the hierarchy is crucial for navigation, system management, and scripting.
Treating everything—including hardware—as files brings consistency to input/output
handling and allows for flexible operations using standard commands.
pg. 1 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
Examples:
1. /etc/hosts maps IP addresses to hostnames
2. /home/user/docs/report.txt is a user document
3. Basic File Navigation
• pwd shows current directory
• cd changes directories
• ls lists files and directories
• Use ls -l or ls -a for detailed/hidden views
• Tab completion improves speed
Explanation:
Navigation in Unix relies on basic commands. pwd displays the current directory path. cd
moves between directories using absolute (/home/user) or relative (../) paths. ls is used to
list files, while its options display more info or hidden files starting with ..
These commands are essential for accessing, organizing, or modifying files. Combined with
keyboard shortcuts and autocompletion, they create a smooth and efficient terminal
experience.
Examples:
1. cd /var/log moves to system logs
2. ls -l ~/Downloads shows contents of the Downloads folder
4. File Creation and Management
• touch creates files
• cp copies files
• mv renames or moves files
• rm deletes files/directories
• mkdir and rmdir manage folders
Explanation:
File management in Unix is done using a set of intuitive commands. touch is used to create
empty files. cp copies files or directories. mv is used for renaming or moving. rm deletes files
and mkdir creates new folders. Options like -r (recursive) extend functionality.
It’s important to be careful with commands like rm -rf, which can delete directories
permanently without confirmation. File operations are fast, scriptable, and easily combinable
with wildcards and filters.
Examples:
pg. 2 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
1. cp notes.txt backup/ copies to backup folder
2. rm -rf temp/ deletes the entire directory
5. File Viewing and Paging
• cat shows entire file content
• more and less display files one page at a time
• head shows the first 10 lines by default
• tail shows the last 10 lines
• tail -f follows live logs
Explanation:
When viewing large files, paging tools like less and more allow users to scroll through
content without loading everything at once. head and tail provide quick access to the start
or end of a file. This is particularly useful for log inspection.
These commands are also commonly used in pipelines with filters, allowing users to extract
specific information from large files or stream real-time outputs during troubleshooting.
Examples:
1. less /var/log/syslog scrolls through logs
2. tail -f app.log displays real-time updates
6. File Permissions and Ownership
• Permissions: read (r), write (w), execute (x)
• Three groups: user, group, others
• chmod changes file permissions
• chown changes file owner
• umask sets default permissions
Explanation:
Unix files have permission sets for the owner (user), group, and others. Each can be allowed
or denied read, write, and execute access. chmod changes these permissions numerically (e.g.,
755) or symbolically (e.g., u+x). chown changes ownership.
Controlling permissions ensures data security and prevents unauthorized actions.
Understanding permission structures is essential for system security, collaborative
environments, and proper automation.
Examples:
1. chmod 700 script.sh gives full permission to the user only
2. chown root config.txt assigns file to the root user
pg. 3 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
7. Filters and Search
• grep searches for patterns
• cut extracts fields from files
• sort and uniq organize text
• wc counts lines, words, or characters
• Filters can be piped for complex tasks
Explanation:
Text filters are essential for analyzing logs, configuration files, and output streams. grep is
powerful for locating patterns using regular expressions. cut, sort, and uniq help organize
and clean data. These commands are often combined via pipes to form efficient data
pipelines.
Used together, these commands reduce the need for scripting or GUI-based tools when
dealing with large datasets or automation workflows.
Examples:
1. grep "error" logfile.txt shows error entries
2. cut -d ',' -f1 users.csv | sort | uniq filters unique names
8. Redirection and Pipes
• > writes output to a file
• >> appends output
• < takes input from a file
• | passes output of one command to another
• Used extensively in scripting
Explanation:
Redirection and piping let Unix users build powerful command chains. You can redirect
command output into a file or read from one using <. Pipes (|) pass output from one
command directly into another, eliminating temporary files and increasing performance.
This concept is foundational to Unix's modularity. Redirection also helps automate
workflows like saving logs, filtering output, and feeding commands into scripts.
Examples:
1. echo "Hello" > greet.txt saves text to a file
2. cat file.txt | grep "name" finds matching lines
pg. 4 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
9. User and Group Management
• whoami, id, groups check user info
• adduser, deluser manage users
• passwd sets passwords
• sudo grants elevated privileges
• usermod updates user info
Explanation:
Unix is built for multi-user environments. Tools exist for creating, modifying, and deleting
users. Permissions and groups control access to system resources. sudo provides temporary
administrative rights without giving full root access.
User management ensures secure access control and isolation between users. Proper group
assignment allows for shared file access without compromising privacy.
Examples:
1. sudo adduser devuser creates a new user
2. groups devuser lists all groups the user belongs to
10. Shell and Shell Types
• The shell is a command-line interface to Unix
• Common shells: sh, bash, zsh, csh
• Shell interprets commands and scripts
• Each shell has unique syntax and features
• Bash is the most widely used shell
Explanation:
The shell in Unix is the interface between the user and the kernel. It interprets typed
commands or scripts and executes them. While sh (Bourne shell) is the original, bash
(Bourne Again Shell) became popular due to its features, scripting abilities, and widespread
support.
Other shells like zsh or ksh offer enhanced scripting or interactive capabilities.
Understanding the shell’s features allows developers to use command-line tools efficiently
and automate tasks.
Examples:
1. echo $SHELL shows the current shell
2. bash script.sh runs a Bash script
11. Processes in Unix
pg. 5 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
• A process is a running instance of a program
• ps lists current processes
• top provides a live view of system activity
• kill terminates processes
• Each process has a PID (Process ID)
Explanation:
Processes represent running programs or scripts in Unix. The system tracks each process with
a unique PID. Tools like ps and top are used to monitor or manage them. Foreground and
background processes can be controlled using &, fg, and bg.
Managing processes is essential for performance, stability, and resource usage.
Understanding how to start, stop, or prioritize processes helps in troubleshooting and system
administration.
Examples:
1. ps aux | grep apache finds the Apache process
2. kill 1234 stops the process with PID 1234
12. Job Control and Background Tasks
• Use & to run tasks in background
• jobs lists current background jobs
• fg brings background job to foreground
• bg resumes paused job in background
• Ctrl+Z suspends current job
Explanation:
Unix supports multitasking even within a single terminal. Commands can be run in the
background using &, allowing users to continue other work. Suspended jobs can be resumed
or moved between foreground and background.
These capabilities are useful when executing long-running scripts or commands that don’t
require immediate attention. Job control improves productivity and system interactivity.
Examples:
1. ./longtask.sh & runs script in background
2. fg %1 resumes first background job
13. Shell Scripting Basics
• Shell scripts automate repetitive tasks
• Start with #!/bin/bash
pg. 6 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
• Use variables with $ and commands with $(...)
• Use if, while, for, and case for logic
• Scripts must be executable with chmod +x
Explanation:
Shell scripting uses shell commands in sequence to perform tasks automatically. Scripts can
include control flow, logic, and text manipulation. They're often used for backups,
monitoring, deployment, and configuration.
By writing reusable scripts, users can avoid repeating tasks, minimize errors, and ensure
consistency in execution. Shell scripting is powerful yet simple for system-level automation.
Examples:
1. #!/bin/bash; echo "Welcome" prints greeting
2. if [ $USER == "root" ]; then echo "Admin"; fi
14. Conditional Statements in Shell
• if, else, and elif provide decision logic
• Conditions use [ ] or [[ ]]
• Use -eq, -ne, -lt for integers
• Use = and != for strings
• Nesting enables complex logic
Explanation:
Conditionals allow scripts to make decisions during execution. Numeric and string
comparisons are common, and conditions can be nested or chained. Shell also supports
logical operators (&&, ||) for combining conditions.
Conditionals enhance automation by tailoring script behavior based on input, environment, or
system state. Proper use improves script flexibility and control.
Examples:
1. if [ $age -gt 18 ]; then echo "Adult"; fi
2. if [ "$status" = "ok" ]; then echo "Success"; else echo "Fail"; fi
15. Looping in Shell Scripts
• for, while, and until loops are supported
• Useful for repeating commands over items or conditions
• break exits a loop early
• continue skips current iteration
• Loops can iterate over files, numbers, or arrays
pg. 7 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited
Explanation:
Loops are used in shell scripts to repeat tasks efficiently. A for loop can iterate over a list of
values, while a while loop runs based on a condition. Loops reduce code duplication and
support batch processing or monitoring.
They’re essential for writing scripts that handle dynamic input or system events. With loops,
scripts become more powerful and capable of handling large-scale tasks automatically.
Examples:
1. for i in 1 2 3; do echo $i; done prints numbers
2. while [ $i -le 5 ]; do echo $i; i=$((i+1)); done
pg. 8 Note: Please do not share this PDF to any other sources.Sharing is strictly prohibited