Module 1
04 July 2025 03:54 PM
UNIX Programming Module 1 - Complete Study Notes
Table of Contents
1. UNIX Architecture and Command Usage
2. General-Purpose Utilities
3. UNIX File System
UNIX Architecture and Command Usage
What is an Operating System?
• Definition: Software that manages computer hardware and provides a convenient, safe
environment for running programs
• Interface: Acts as an interface between user programs and hardware resources (processor,
memory, hard disk, printer)
• Functions:
○ Allocates memory for programs
○ Loads CPU registers with control information
○ Tracks instruction execution
○ Handles hardware access requests
○ Cleans up memory after program completion
Key Features of Modern Operating Systems
• Multi-programming: Allow multiple programs in memory
• Time-sharing: Programs run for small time slices, then switch
• Process creation: Creates a process for each program
• Process control: Manages switching between processes
The UNIX Operating System
Definition and History
• Full Form: UNiplexed Information Computing System (UNICS)
• Developers: Ken Thompson, Dennis Ritchie, and others at Bell Labs (1970s)
• Key Innovation: First portable operating system (rewritten in C in 1973)
Historical Evolution
1. 1969: Thompson and Ritchie start building flexible OS
2. 1973: Dennis Ritchie rewrites system in C (first portable UNIX)
3. BSD UNIX: University of California Berkeley creates their version
4. Commercial Variants: Sun (SunOS/Solaris), IBM (AIX), HP (HP-UX)
5. SVR4: AT&T releases System V Release 4 as standard UNIX
6. Linux: Created by Linus Torvalds, open-source alternative
UNIX Architecture
Division of Labor: Kernel and Shell
Kernel:
• Core of the operating system
• Collection of routines written in C
• Loaded into memory at boot time
• Communicates directly with hardware
• Provides services through system calls
• Manages memory, processes, scheduling, priorities
New Section 1 Page 1
• Manages memory, processes, scheduling, priorities
Shell:
• Command interpreter
• Interface between user and kernel
• Translates commands into actions
• Examines input for special characters
• Rebuilds simplified command lines
• Multiple shells can exist (only one kernel)
The File and Process
File:
• Array of bytes
• Hierarchical arrangement
• Directories and devices treated as files
• Located with reference to predetermined place
Process:
• Name given to a file when executed as program
• Time image of executable file
• Have parent-child relationships
• Belong to hierarchical tree structure
System Calls
• Functions used by applications to communicate with kernel
• Examples: write(), open(), read()
• Interface between user programs and kernel services
Features of UNIX
1. Multi-user System
• Multiple users can use system simultaneously
• Resources shared between users
• Time-division multiplexing
• Supports multi-programming:
○ Multiple users run separate jobs
○ Single user can run multiple jobs
2. Multi-tasking System
• Single user can run multiple tasks concurrently
• Foreground and background jobs
• Job switching capabilities
• Suspend and terminate jobs
3. Building Block Approach
• Each command performs one simple job
• Commands connected with pipes (|) for complex operations
• Commands that can be connected are called filters
• Example: ls | wc (count files in directory)
4. UNIX Tool Kit
• General purpose tools
• Text manipulation utilities (filters)
• Compilers and interpreters
• Networked applications
• System administration tools
5. Pattern Matching
• Special characters for matching strings
• Example: * matches any string
• ls chap* lists files starting with "chap"
6. Programming Facility
• Shell is also a programming language
New Section 1 Page 2
• Shell is also a programming language
• Control structures, variables, loops
• Used to design shell scripts
7. Documentation
• man pages for each command
• Reference for commands and configuration files
• Online documentation available
UNIX Environments
1. Personal Environment
• UNIX installed on personal computers
• Single user system
• All resources available to one user
2. Time-sharing Environment
• Multiple users connected to central computer
• Non-programmable terminals
• Shared output devices and storage
• All computing done by central computer
• System can become slow due to central load
3. Client-Server Environment
• Computing split between server and clients
• Server: Central powerful computer
• Clients: User workstations/PCs
• Workload shared between server and clients
• Faster system performance
UNIX Structure Components
1. Kernel
• Bridge between hardware and user
• Central software application
• Handles files, memory, devices, processes, network
• Ensures correct task execution
2. Shell
• Program between user and kernel
• Translates commands into actions
• Two major types:
○ Bourne Shell: Default for Version 7 UNIX, prompt $
○ C Shell: Text window command processor, prompt %
3. Application
• Word processors, graphics programs
• Database management programs
• Commands and utilities
• Application Programming Interface (API)
POSIX and Single UNIX Specification
POSIX
• Full Form: Portable Operating System Standard for Computer Environment
• Developer: IEEE standards for operating systems
• POSIX.1: C application programming interface
• POSIX.2: Shell and utilities
Command Structure
Basic Syntax
$command [options] [arguments]
Components
New Section 1 Page 3
Components
• Command: First word in command line
• Arguments: Subsequent words
• Options: Special arguments with - sign
• Spacing: At least one space between command and arguments
Examples
$ ls -x chap*
# ls = command, -x = option, chap* = argument
Options
• Special arguments preceded by -
• Predetermined list for each command
• Can be combined: ls -l -a -t = ls -lat
Filename Arguments
• Many commands use filenames as arguments
• Multiple filenames possible: ls -l chap1 chap2
Exceptions
• Some commands don't accept arguments: pwd
• Some commands may/may not take arguments: who
• Some commands flexible: ls (no args), ls -l (options only), ls chap1 (files only)
Internal and External Commands
Internal Commands
• Built into the shell
• Examples: cd, echo, type
• Check with: type echo → "echo is a built-in"
External Commands
• Not built into shell
• Have independent existence in directories
• Examples: ls, cat
• Check with: type ls → "ls is /bin/ls"
Flexibility of Command Usage
Combining Commands
• Multiple commands in one line
• Separated by semicolon (;)
• Example: wc note; ls -l note
Grouping Commands
• Use parentheses to group commands
• Example: (wc note; ls -l note) > list
General-Purpose Utilities
1. cal - Calendar Display
Basic Usage
$ cal # Current month
$ cal 2006 # Entire year 2006
$ cal 03 2006 # March 2006
Features
• Arguments optional
• Month optional but year mandatory for specific dates
• Single argument interpreted as year
• Can combine with pager: (cal 2003; cal 2006) | more
2. date - Date and Time Display
Basic Usage
New Section 1 Page 4
Basic Usage
$ date
# Output: Tue Aug 28 1:58:03 IST 2018
Output Fields
1. Day of week
2. Month name
3. Date
4. Time
5. Time zone
6. Year
Format Options
• +%a - Day
• +%h - Month name
• +%d - Date
• +%T - Time
• +%Z - Time zone
• +%Y - Year
• +%H - Hour
• +%M - Minute
• +%S - Second
Examples
$ date +%h # Aug
$ date +"%h %d" # Aug 28
3. echo - Display Messages
Basic Usage
$ echo "Hello World" # Display message
$ echo $HOME # Display variable value
Escape Sequences (use with -e option)
• \a - Beep
• \b - Backspace
• \c - No newline
• \f - Form feed
• \n - New line
• \r - Carriage return
• \t - Tab
• \v - Vertical tab
• \\ - Backslash
• \0n - ASCII character (octal value n)
Examples
$ echo -e "Hello\nWorld" # Hello on line 1, World on line 2
$ echo -e '\07' # Produces beep sound
4. printf - Alternative to echo
Differences from echo
• Does not insert newline by default
• More formatting control
• Accepts format specifiers
Format Specifiers
• %s - String
• %30s - String in 30-character field
• %d - Decimal integer
• %6d - Integer in 6-character field
• %o - Octal integer
New Section 1 Page 5
• %o - Octal integer
• %x - Hexadecimal integer
• %f - Floating point number
Examples
$ printf "5th" # Output: 5th$ (no newline)
$ printf "Value: %d\n" 255 # Value: 255
$ printf "Octal: %o, Hex: %x\n" 255 255 # Octal: 377, Hex: ff
5. ls - List Files
Basic Usage
$ ls # List files in current directory
$ ls chap* # List files starting with "chap"
$ ls a.txt # Check if file exists
Important Options
• -a - Show all files including hidden files
• -A - Show hidden files but not . and ..
• -d dirname - Search for specific directory
• -i - Display inode numbers
• -l - Long listing (7 attributes)
• -r - Reverse alphabetical order
• -R - Recursive listing
• -t - Sort by modification time
• -u - Sort by access time
• -x - Multi-columnar output
Hidden Files
• Files starting with . are hidden
• . represents current directory
• .. represents parent directory
6. Output Redirection and Pipes
Output Redirection
$ ls > list # Save ls output to file 'list'
Pipes
$ ls | wc # Count files in directory
7. wc - Word Count
Usage
$ wc filename # Count lines, words, characters
$ wc list.txt
# Output: 6 6 42 list.txt (6 lines, 6 words, 42 characters)
8. who - Display Logged Users
Basic Usage
$ who # Show logged users
Output Fields
1. Username
2. Terminal device name
3. Login date
4. Login time
5. Machine name (for remote logins)
Options
• -H - Display header
• -u - Detailed list with idle time and PID
Examples
$ who -Hu
New Section 1 Page 6
$ who -Hu
# Shows: NAME, LINE, TIME, IDLE, PID, COMMENTS
$ who am i # Show current user info
9. passwd - Change Password
Usage
$ passwd # Change password for current user
Process
1. Enter current password
2. Enter new password
3. Confirm new password
4. System encrypts and stores in /etc/shadow
Password Rules
• Don't use similar to old password
• Avoid common names
• Use mix of alphabetic/numeric characters
• Don't write in accessible documents
• Change regularly
10. bc - Calculator
Usage
$ bc # Start calculator
>2+3 # Performs calculations
> quit # Exit calculator
11. script - Record Terminal Session
Usage
$ script # Start recording
# ... perform commands ...
$ exit # Stop recording
Features
• Records all terminal activities
• Creates typescript file
• Useful for documentation and debugging
12. uname - System Information
Basic Usage
$ uname # Display system name
Options
• -a or --all - All available information
• -s or --kernel-name - Kernel name
• -n or --nodename - Network/domain name
• -r or --kernel-release - Kernel release
• -v or --kernel-version - Kernel version
• -m or --machine - Machine hardware name
• -p or --processor - Processor type
• -i or --hardware-platform - Hardware platform
• -o or --operating-system - Operating system
13. tty - Terminal Information
Usage
$ tty # Display current terminal device
Purpose
• Shows which terminal device you're using
• Useful for multiple terminal sessions
• Debugging scripts for correct environment
New Section 1 Page 7
• Debugging scripts for correct environment
14. stty - Terminal Characteristics
Usage
$ stty # Display terminal settings
$ stty -a # Display all settings
Purpose
• Display and change terminal line settings
• Control terminal characteristics
• Modify terminal behavior
UNIX File System
File Concepts
What is a File?
• Building blocks of any operating system
• Used for command execution, data storage, process creation
• Fundamental to efficient file management system
File Naming Rules
• Length: Up to 255 characters
• Allowed Characters: Any ASCII except / and NULL (ASCII 0)
• Recommended Characters:
○ Alphabetic characters and numerals
○ Period (.), hyphen (-), underscore (_)
• Extensions: No rules for extensions
• Case Sensitivity: UNIX is case-sensitive
Valid Filename Examples
.last_time
list.
a.b.c.d.e
-{}[]
@#$%*abcd
Key Differences from DOS/Windows
• Multiple dots allowed in filename
• Files can begin or end with dots
• Case-sensitive (Chap01, chap01, CHAP01 are different)
File Types/Categories
1. Ordinary (Regular) Files
Most common file type, divided into:
Text Files:
• Contains only printable characters
• Viewable and understandable content
• Lines terminated with newline (LF) character
• Examples: C programs, Java sources, shell scripts, Perl scripts
Binary Files:
• Contains printable and unprintable characters (ASCII 0-255)
• Examples: UNIX commands, object code, executables, pictures, sound, video
• Cannot be displayed with simple cat command
2. Directory Files
• Contains no actual data
• Stores details of files and subdirectories
• Organized in hierarchical structure
• Each entry has two components:
New Section 1 Page 8
• Each entry has two components:
○ Filename
○ Unique identification number (inode number)
3. Device Files
• Represent hardware devices
• Located in /dev directory
• Special files that don't contain actual data
• Allow uniform access to devices using file operations
File System Organization
Hierarchical Structure
• Files organized in tree-like structure
• Root directory (/) at the top
• All other files are descendants of root
• Multi-level hierarchy called directory tree
Directory Tree Analogy
Root (/)
├── bin/
├── home/
│ └── username/
├── usr/
│ ├── bin/
│ └── lib/
└── var/
└── log/
Standard Directories
Root Level Directories
• / - Root of filesystem tree
• /bin - Essential user binaries (ls, cp, etc.)
• /boot - Boot loader files
• /dev - Device files
• /etc - System configuration files
• /home - User home directories
• /lib - System libraries and kernel modules
• /media - Mount point for removable devices
• /mnt - Temporary mount points
• /proc - Virtual filesystem showing process information
• /root - Root user's home directory
• /tmp - Temporary files
• /usr - User programs and data
• /var - Variable data files
Important Subdirectories
• /usr/bin - Non-essential user binaries
• /usr/include - Development headers
• /usr/lib - Libraries for /usr programs
• /var/log - System log files
• /var/mail - User mail storage
• /var/spool - Print jobs and mail queues
• /var/tmp - Temporary files preserved between reboots
Parent-Child Relationships
Hierarchical Organization
New Section 1 Page 9
Hierarchical Organization
• All files related to one another
• Root (/) serves as reference point
• Every file must have a parent
• Ultimate parentage traces to root
Relationship Rules
• Parent is always a directory
• Children can be files or directories
• Ordinary files cannot have children
• Directory structure forms tree
Example Structure
/
├── home/
│ └── mthomas/
│ └── login.sql
└── bin/
├── cp
└── pwd
Home Directory and Environment Variables
Home Directory
• Definition: Directory where user is placed upon login
• Creation: Created when user account is opened
• Typical Path: /home/username
• Usage: Primary workspace for user files and subdirectories
HOME Variable
• Purpose: Shell variable that stores home directory path
• Usage: echo $HOME displays home directory
• Example: /home/sharma
Environment Variables
• Dynamic named values affecting process behavior
• Set of variables available to running processes
• HOME is an environment variable
PATH Variable
• Definition: Colon-delimited list of directories
• Purpose: Shell searches these directories for commands
• Display: echo $PATH
• Example: /usr2/username/bin:/usr/local/bin:/usr/bin:
Command Search Order
When you enter a command, shell searches directories in PATH order:
1. /usr2/username/bin
2. /usr/local/bin
3. /usr/bin
4. Current directory (.)
Navigation Commands
pwd - Print Working Directory
$ pwd # Display current directory path
/home/username/documents
cd - Change Directory
$ cd # Go to home directory
$ cd /usr/bin # Go to /usr/bin
New Section 1 Page 10
$ cd /usr/bin # Go to /usr/bin
$ cd .. # Go to parent directory
$ cd ../.. # Go up two levels
$ cd directory_name # Go to subdirectory
mkdir - Make Directory
$ mkdir dirname # Create directory
$ mkdir -p path/to/dir # Create parent directories if needed
rmdir - Remove Directory
$ rmdir dirname # Remove empty directory
$ rmdir -p path/to/dir # Remove directory and empty parents
Pathnames
Absolute Pathnames
• Definition: Complete path from root directory
• Start: Always begins with /
• Example: /home/username/documents/file.txt
• Advantage: Unambiguous, works from any location
• Usage: When exact location needs to be specified
Relative Pathnames
• Definition: Path relative to current directory
• Start: Does not begin with /
• Examples:
○ documents/file.txt (subdirectory)
○ ../file.txt (parent directory)
○ ../../bin/command (grandparent then bin)
• Advantage: Shorter, convenient for nearby files
• Special Symbols:
○ . - Current directory
○ .. - Parent directory
Choosing Between Absolute and Relative
• Use Absolute: When specifying exact location, in scripts, for system files
• Use Relative: For nearby files, daily navigation, shorter commands
Hidden Files
• Definition: Files beginning with . (dot)
• Purpose: System configuration files, user preferences
• Visibility: Not shown by default ls command
• Display: Use ls -a to show all files including hidden
• Examples: .bashrc, .profile, .vimrc
File System Navigation Tips
1. Use pwd to check current location
2. Use ls to see directory contents
3. Use cd without arguments to return home
4. Use tab completion for faster navigation
5. Use ls -la to see all files with details
Summary
This Module 1 covers the fundamental concepts of UNIX:
1. Architecture: Understanding kernel-shell division, system calls, and UNIX structure
2. Commands: Essential utilities for system interaction and information display
3. File System: Hierarchical organization, navigation, and file management concepts
Key Concepts to Remember:
• UNIX is multi-user, multi-tasking with hierarchical file system
New Section 1 Page 11
• UNIX is multi-user, multi-tasking with hierarchical file system
• Kernel manages hardware, shell interprets commands
• Files are organized in tree structure with root (/) at top
• Commands have structure: command [options] [arguments]
• Pathnames can be absolute (from root) or relative (from current location)
• Environment variables like HOME and PATH are crucial for system operation
Exam Tips:
1. Know the difference between kernel and shell
2. Understand file types and directory structure
3. Practice command syntax and options
4. Remember standard directory purposes
5. Know how to navigate using absolute and relative paths
6. Understand environment variables and their uses
From <https://claude.ai/chat/b08cb399-42e3-45b9-86c9-b7b90be606f3>
UNIX Operating System - Q&A Answers
Q.1 a] Explain the architecture of the UNIX operating
system. Outline its salient features and role of each layer.
UNIX Architecture
The UNIX architecture is based on a layered approach with clear division of labor between different
components:
Main Components:
1. Hardware Layer (Bottom)
• Physical components: processor, memory, hard disk, printer, etc.
• Provides the foundation for the operating system
2. Kernel Layer (Core)
• Role: Core of the operating system - collection of routines written in C
• Functions:
○ Interacts directly with hardware
○ Manages system memory
○ Schedules processes and decides their priorities
○ Provides gateway to computer resources
○ Handles system calls from user applications
• Key Feature: Loaded into memory when system boots and remains active
3. Shell Layer (Interface)
• Role: Command interpreter that acts as interface between user and kernel
• Functions:
○ Translates commands into actions
○ Examines keyboard input for special characters
○ Rebuilds simplified command line
○ Communicates with kernel for command execution
• Note: System has one kernel but may have several shells
4. Application Layer (Top)
• Components: Word processors, graphics programs, database management, utilities
• Role: Provides applications to end users
• Examples: Web browsers, gaming software, text editors
New Section 1 Page 12
• Examples: Web browsers, gaming software, text editors
Division of Labor: Kernel and Shell
The main concept in UNIX architecture is the division of labor:
• Kernel: Interacts with hardware
• Shell: Interacts with user
Salient Features of UNIX:
1. Multi-user System
• Multiple users can use the system simultaneously
• Resources are shared between all users
• Time-sharing approach: machine segments time for different users
2. Multi-tasking System
• Single user can run multiple tasks concurrently
• Jobs can run in foreground and background
• Ability to switch, suspend, or terminate jobs
3. Building Block Approach
• Each command performs one simple job
• Commands can be connected using pipes (|)
• Commands that can be connected are called filters
4. Pattern Matching
• Special characters for matching strings
• Example: ls chap* matches filenames starting with 'chap'
5. Programming Facility
• Shell acts as programming language
• Has control structures, variables, loops
• Enables shell script creation
6. Documentation
• 'man' pages for each command
• References for commands and configuration files
7. Unix Tool Kit
• General purpose tools
• Text manipulation utilities (filters)
• Compilers and interpreters
• System administration tools
• Networked applications
Q.1 b] Compare internal and external UNIX commands and
summarize their differences with examples.
Internal Commands
Definition: Commands that are built into the shell
Characteristics:
• Part of the shell program itself
• No separate executable file
• Faster execution (no file system access needed)
• Always available when shell is running
Examples:
• cd - Change directory
• echo - Display message
• type - Show command type
• pwd - Print working directory
Verification:
$ type echo
echo is a built-in
New Section 1 Page 13
echo is a built-in
$ type cd
cd is a built-in
External Commands
Definition: Commands that aren't built into the shell
Characteristics:
• Separate executable files stored in directories
• Have independent existence in file system
• Slower execution (requires file system access)
• Located in directories like /bin, /usr/bin
Examples:
• ls - List files
• cat - Display file contents
• wc - Word count
• who - Show logged users
Verification:
$ type ls
ls is /bin/ls
$ type cat
cat is /bin/cat
Key Differences Summary:
Aspect Internal Commands External Commands
Location Built into shell Separate executable files
Storage Part of shell program Stored in /bin, /usr/bin directories
Execution Speed Faster Slower (file access required)
Availability Always available with shell Depends on file system
Examples cd, echo, type, pwd ls, cat, wc, who
Q.2 a] List general-purpose UNIX utilities like cal, date,
echo, uname, who, passwd with their usage syntax.
General-Purpose UNIX Utilities:
1. cal - Calendar
Purpose: Display calendar of specific month/year
Syntax:
$ cal [[month] year]
Examples:
$ cal # Current month calendar
$ cal 2003 # Year 2003 calendar
$ cal 03 2006 # March 2006 calendar
$ (cal 2003; cal 2006) | more # Multiple years with pager
2. date - Display Date and Time
Purpose: Display system date and time
Syntax:
$ date [+format]
Options:
• +%a - day
• +%h - Month name
• +%d - date
• +%T - time
• +%Z - Time Zone
New Section 1 Page 14
• +%Z - Time Zone
• +%Y - year
• +%H - Hour
• +%M - Minute
• +%S - Second
Examples:
$ date # Full date and time
$ date +%h # Month name only
$ date +"%h%d" # Month and date
3. echo - Display Message
Purpose: Display text or evaluate shell variables
Syntax:
$ echo [options] [string]
Options:
• -e - Enable interpretation of backslash escapes
Escape Sequences:
• \a - Beep
• \b - Backspace
• \n - New line
• \t - Tab
• \r - Carriage return
• \\ - Backslash
Examples:
$ echo "Hello World"
$ echo -e "Line1\nLine2"
$ echo $HOME
4. uname - System Information
Purpose: Display system information
Syntax:
$ uname [options]
Options:
• -a or --all - All available information
• -s or --kernel-name - Kernel name
• -n or --nodename - Network name
• -r or --kernel-release - Kernel release
• -v or --kernel-version - Kernel version
• -m or --machine - Machine hardware name
• -p or --processor - Processor type
• -o or --operating-system - Operating system
5. who - Show Logged Users
Purpose: Display list of users currently logged in
Syntax:
$ who [options]
Options:
• -H - Display header
• -u - Detailed list with idle time
• am i - Show current user information
Examples:
$ who # List all users
$ who -Hu # Detailed list with header
$ who am i # Current user info
Output Format:
• Username
• Terminal device name
New Section 1 Page 15
• Terminal device name
• Login date and time
• Machine name (for remote logins)
6. passwd - Change Password
Purpose: Change user password
Syntax:
$ passwd
Process:
1. Enter current password
2. Enter new password
3. Re-enter new password for confirmation
Password Rules:
• Don't use similar to old password
• Avoid common names
• Use mix of alphabetic and numeric characters
• Don't write in accessible documents
• Change regularly
Q.2 b] Interpret the UNIX file system structure and
demonstrate the use of absolute and relative pathnames.
UNIX File System Structure
The UNIX file system is organized as a hierarchical tree structure with the following characteristics:
Key Concepts:
1. Root Directory (/): Top-most directory, ancestor of all files
2. Tree Structure: All files organized in multi-level hierarchy
3. Parent-Child Relationship: Every file has a parent directory
4. Directory Tree: Directories can contain files and subdirectories
Standard Directories:
Root Level Directories:
• / - Root directory (top of filesystem tree)
• /bin - Essential user binaries (ls, cp, etc.)
• /boot - Boot loader files
• /dev - Device files
• /etc - System configuration files
• /home - User home directories
• /lib - System libraries
• /media - Removable media mount points
• /mnt - Temporary filesystem mount points
• /proc - Process information (virtual filesystem)
• /root - Root user's home directory
• /tmp - Temporary files
• /usr - User programs and data
• /var - Variable data (logs, mail, etc.)
Important Subdirectories:
• /usr/bin - Non-essential user binaries
• /usr/include - Development headers
• /usr/lib - Libraries for /usr programs
• /var/log - System log files
• /var/mail - User mail storage
• /var/spool - Print jobs and mail queues
Pathnames in UNIX
Absolute Pathnames
Definition: Complete path from root directory (/) to target file/directory
New Section 1 Page 16
Definition: Complete path from root directory (/) to target file/directory
Characteristics:
• Always starts with '/'
• Unambiguous - same meaning regardless of current location
• Complete specification of file location
Examples:
/home/user/documents/file.txt
/usr/bin/ls
/etc/passwd
/var/log/messages
Relative Pathnames
Definition: Path relative to current working directory
Characteristics:
• Does not start with '/'
• Depends on current directory location
• Uses special symbols:
○ . (dot) - Current directory
○ .. (dot dot) - Parent directory
Examples:
documents/file.txt # file.txt in documents subdirectory
../other_dir/file.txt # file.txt in sibling directory
./script.sh # script.sh in current directory
../../home/user/file.txt # file.txt two levels up then down
Practical Demonstration:
Directory Navigation Examples:
# Show current directory (absolute path)
$ pwd
/home/user
# List files in current directory
$ ls
documents pictures scripts
# Access file using absolute pathname
$ cat /home/user/documents/readme.txt
# Access same file using relative pathname
$ cat documents/readme.txt
# Navigate to parent directory
$ cd ..
$ pwd
/home
# Navigate using absolute path
$ cd /usr/bin
# Navigate using relative path
$ cd ../local/bin
Path Examples in Directory Tree:
/
├── home/
│ ├── user1/
│ │ ├── documents/
│ │ │ └── file.txt
│ │ └── pictures/
│ └── user2/
├── usr/
│ ├──
New Section 1 Page 17
│ ├── bin/
│ │ ├── ls
│ │ └── cat
│ └── local/
└── var/
└── log/
From /home/user1:
• Absolute: /home/user1/documents/file.txt
• Relative: documents/file.txt
From /usr/bin:
• Absolute: /home/user1/documents/file.txt
• Relative: ../../home/user1/documents/file.txt
Parent-Child Relationships:
Key Points:
1. Root (/) is the ultimate parent of all files
2. Parent is always a directory (ordinary files cannot have children)
3. Every file has exactly one parent (except root)
4. Directories can have multiple children
Example Hierarchy:
/ (root)
├── home (parent of user directories)
│ └── user (parent of user files)
│ └── file.txt (child of user)
└── usr (parent of system directories)
└── bin (parent of system binaries)
└── ls (child of bin)
Special Directory References:
• / - Root directory
• . - Current directory
• .. - Parent directory
• ~ - User's home directory (shorthand)
• $HOME - Environment variable for home directory
This hierarchical organization allows for efficient file management and clear navigation paths in the
UNIX filesystem.
From <https://claude.ai/chat/700cd0a6-6ec3-4847-9bbb-f3ac5b5ea4b1>
New Section 1 Page 18