Linux Operating System Fundamentals
Table of Contents
1. [Introduction to Linux](#introduction-to-linux) 2. [Linux Architecture](#linux-architecture) 3. [File System Hierarchy](#file-system-hierarchy) 4. [Essential Commands](#essential-commands) 5. [File Operations](#file-operations) 6. [Process Management](#process-management) 7. [User and Group Management](#user-and-group-management) 8. [File Permissions](#file-permissions) 9. [Package Management](#package-management) 10. [System Monitoring](#system-monitoring) 11. [Network Commands](#network-commands) 12. [Text Processing](#text-processing)Introduction to Linux
Linux is an open-source, Unix-like operating system kernel first released by Linus Torvalds in 1991. It forms the foundation for numerous distributions (distros) that provide complete operating systems for servers, desktops, and embedded devices.
Key Characteristics
| Feature | Description | |---------|-------------| | Open Source | Source code is freely available and modifiable | | Multi-user | Supports multiple users simultaneously | | Multi-tasking | Can run multiple processes concurrently | | Portable | Runs on various hardware architectures | | Secure | Built-in security features and permission system | | Stable | Known for reliability and uptime |
Popular Linux Distributions
| Distribution | Base | Target Use Case | Package Manager | |--------------|------|-----------------|-----------------| | Ubuntu | Debian | Desktop/Server | APT | | CentOS/RHEL | Red Hat | Enterprise Server | YUM/DNF | | Fedora | Red Hat | Desktop/Development | DNF | | Debian | Independent | Server/Desktop | APT | | Arch Linux | Independent | Advanced Users | Pacman | | SUSE | Independent | Enterprise | Zypper |
Linux Architecture
Linux follows a layered architecture model consisting of several components:
Architecture Layers
`
+---------------------------+
| User Applications |
+---------------------------+
| System Call |
+---------------------------+
| Linux Kernel |
+---------------------------+
| Hardware Layer |
+---------------------------+
`
Kernel Components
| Component | Function | |-----------|----------| | Process Scheduler | Manages CPU time allocation | | Memory Manager | Handles RAM and virtual memory | | File System | Manages data storage and retrieval | | Device Drivers | Interface with hardware components | | Network Stack | Handles network communications |
File System Hierarchy
Linux uses a hierarchical file system structure starting from the root directory (/).
Standard Directory Structure
| Directory | Purpose | Contents |
|-----------|---------|----------|
| / | Root directory | Top-level directory containing all others |
| /bin | Essential binaries | Basic system commands (ls, cp, mv) |
| /boot | Boot files | Kernel and boot loader files |
| /dev | Device files | Hardware device representations |
| /etc | Configuration files | System-wide configuration files |
| /home | User directories | Individual user home directories |
| /lib | Shared libraries | Essential shared libraries |
| /media | Removable media | Mount points for removable devices |
| /mnt | Mount points | Temporary mount points |
| /opt | Optional software | Third-party software packages |
| /proc | Process information | Virtual filesystem for process data |
| /root | Root user home | Home directory for root user |
| /sbin | System binaries | System administration commands |
| /tmp | Temporary files | Temporary storage space |
| /usr | User programs | User applications and utilities |
| /var | Variable data | Logs, databases, cache files |
Essential Commands
Navigation Commands
#### pwd (Print Working Directory) Displays the current directory path.
`bash
pwd
Output: /home/username
`Options:
- -L : Display logical path (default)
- -P : Display physical path without symbolic links
#### cd (Change Directory) Changes the current working directory.
`bash
cd /home/username # Absolute path
cd Documents # Relative path
cd .. # Parent directory
cd ~ # Home directory
cd - # Previous directory
`
Special Directory References:
- . : Current directory
- .. : Parent directory
- ~ : Home directory
- - : Previous directory
#### ls (List Directory Contents) Lists files and directories in the current or specified directory.
`bash
ls # Basic listing
ls -l # Long format
ls -la # Long format including hidden files
ls -lh # Long format with human-readable sizes
ls -R # Recursive listing
`
Common Options:
| Option | Description |
|--------|-------------|
| -l | Long format with detailed information |
| -a | Show all files including hidden ones |
| -h | Human-readable file sizes |
| -R | Recursive listing of subdirectories |
| -t | Sort by modification time |
| -S | Sort by file size |
| -r | Reverse order |
File Operations
Creating Files and Directories
#### touch Creates empty files or updates timestamps.
`bash
touch filename.txt # Create single file
touch file1.txt file2.txt file3.txt # Create multiple files
touch -t 202301011200 oldfile.txt # Set specific timestamp
`
#### mkdir (Make Directory) Creates directories.
`bash
mkdir directory_name # Create single directory
mkdir -p path/to/nested/directory # Create nested directories
mkdir dir1 dir2 dir3 # Create multiple directories
`
Options:
- -p : Create parent directories as needed
- -m : Set directory permissions
Copying and Moving Files
#### cp (Copy) Copies files and directories.
`bash
cp source.txt destination.txt # Copy file
cp -r source_dir destination_dir # Copy directory recursively
cp *.txt backup/ # Copy all .txt files
cp -u source.txt dest.txt # Copy only if source is newer
`
Important Options:
| Option | Description |
|--------|-------------|
| -r or -R | Copy directories recursively |
| -i | Interactive mode (prompt before overwrite) |
| -u | Copy only when source is newer |
| -v | Verbose output |
| -p | Preserve file attributes |
#### mv (Move/Rename) Moves or renames files and directories.
`bash
mv oldname.txt newname.txt # Rename file
mv file.txt /path/to/destination/ # Move file
mv *.log logs/ # Move all .log files
mv -i source.txt destination.txt # Interactive move
`
Removing Files and Directories
#### rm (Remove) Deletes files and directories.
`bash
rm filename.txt # Remove file
rm -r directory_name # Remove directory recursively
rm -f filename.txt # Force removal without prompt
rm -rf directory_name # Force recursive removal
`
Critical Options:
| Option | Description | Warning Level |
|--------|-------------|---------------|
| -r | Recursive removal | High |
| -f | Force removal without prompts | Very High |
| -i | Interactive confirmation | Safe |
| -v | Verbose output | Safe |
Note: Be extremely careful with rm -rf as it permanently deletes files without confirmation.
Process Management
Viewing Processes
#### ps (Process Status) Displays information about running processes.
`bash
ps # Show processes for current user
ps aux # Show all processes with detailed info
ps -ef # Full format listing
ps -u username # Show processes for specific user
`
Process Information Fields:
| Field | Description | |-------|-------------| | PID | Process ID | | PPID | Parent Process ID | | USER | Process owner | | CPU | CPU usage percentage | | MEM | Memory usage percentage | | VSZ | Virtual memory size | | RSS | Resident set size (physical memory) | | TTY | Terminal type | | STAT | Process state | | START | Start time | | TIME | CPU time used | | COMMAND | Command that started the process |
#### top Real-time process viewer.
`bash
top # Interactive process viewer
top -u username # Show processes for specific user
top -p PID # Monitor specific process
`
Interactive Commands in top:
- q : Quit
- k : Kill process
- r : Renice process
- M : Sort by memory usage
- P : Sort by CPU usage
- h : Help
Process Control
#### kill Terminates processes by PID.
`bash
kill PID # Send TERM signal
kill -9 PID # Send KILL signal (force)
kill -15 PID # Send TERM signal (graceful)
kill -HUP PID # Send HUP signal (restart)
`
Common Signals:
| Signal | Number | Description | |--------|--------|-------------| | TERM | 15 | Graceful termination (default) | | KILL | 9 | Force kill (cannot be caught) | | HUP | 1 | Hang up (restart) | | INT | 2 | Interrupt (Ctrl+C) | | STOP | 19 | Stop process | | CONT | 18 | Continue stopped process |
#### killall Terminates processes by name.
`bash
killall firefox # Kill all Firefox processes
killall -9 unresponsive_app # Force kill by name
killall -u username # Kill all processes for user
`
Background and Foreground Jobs
#### Running Jobs in Background
`bash
command & # Run command in background
nohup command & # Run command immune to hangups
`
#### Job Control Commands
`bash
jobs # List active jobs
fg %1 # Bring job 1 to foreground
bg %1 # Send job 1 to background
Ctrl+Z # Suspend current job
Ctrl+C # Terminate current job
`
User and Group Management
User Information Commands
#### whoami Displays current username.
`bash
whoami
Output: username
`#### id Shows user and group IDs.
`bash
id # Current user info
id username # Specific user info
id -u username # User ID only
id -g username # Primary group ID only
`
#### who and w Show logged-in users.
`bash
who # Show logged-in users
w # Show detailed user activity
last # Show login history
`
User Management (Administrative)
#### useradd Creates new user accounts.
`bash
sudo useradd newuser # Create basic user
sudo useradd -m -s /bin/bash newuser # Create with home directory and shell
sudo useradd -g primarygroup -G secondarygroups newuser
`
Common Options:
| Option | Description |
|--------|-------------|
| -m | Create home directory |
| -s | Specify shell |
| -g | Primary group |
| -G | Secondary groups |
| -d | Home directory path |
| -c | Comment/full name |
#### usermod Modifies user accounts.
`bash
sudo usermod -aG groupname username # Add user to group
sudo usermod -s /bin/zsh username # Change user shell
sudo usermod -l newname oldname # Change username
`
#### userdel Deletes user accounts.
`bash
sudo userdel username # Delete user (keep home directory)
sudo userdel -r username # Delete user and home directory
`
Group Management
#### groups Shows group membership.
`bash
groups # Current user's groups
groups username # Specific user's groups
`
#### groupadd Creates new groups.
`bash
sudo groupadd newgroup # Create new group
sudo groupadd -g 1001 newgroup # Create with specific GID
`
#### groupdel Deletes groups.
`bash
sudo groupdel groupname # Delete group
`
File Permissions
Understanding Permission System
Linux uses a permission system based on three types of access for three categories of users.
#### Permission Types
| Permission | Symbol | Numeric | File Effect | Directory Effect | |------------|--------|---------|-------------|------------------| | Read | r | 4 | View file contents | List directory contents | | Write | w | 2 | Modify file contents | Create/delete files in directory | | Execute | x | 1 | Run file as program | Enter directory |
#### User Categories
| Category | Description | |----------|-------------| | Owner (u) | User who owns the file | | Group (g) | Group that owns the file | | Other (o) | All other users |
Viewing Permissions
`bash
ls -l filename
Output: -rw-r--r-- 1 user group 1024 Jan 1 12:00 filename
`Permission String Breakdown:
- First character: File type (- for file, d for directory, l for link)
- Next 9 characters: Permissions in groups of 3 (owner, group, other)
Changing Permissions
#### chmod (Change Mode) Modifies file permissions.
Symbolic Mode:
`bash
chmod u+x filename # Add execute for owner
chmod g-w filename # Remove write for group
chmod o=r filename # Set other to read only
chmod a+r filename # Add read for all
chmod u+rwx,g+rx,o+r filename # Complex permissions
`
Numeric Mode:
`bash
chmod 755 filename # rwxr-xr-x
chmod 644 filename # rw-r--r--
chmod 600 filename # rw-------
chmod 777 filename # rwxrwxrwx (full access)
`
Common Permission Combinations:
| Numeric | Symbolic | Use Case | |---------|----------|----------| | 755 | rwxr-xr-x | Executable files | | 644 | rw-r--r-- | Regular files | | 600 | rw------- | Private files | | 666 | rw-rw-rw- | Shared writable files | | 777 | rwxrwxrwx | Full access (use carefully) |
Changing Ownership
#### chown (Change Owner) Changes file ownership.
`bash
sudo chown newowner filename # Change owner only
sudo chown newowner:newgroup filename # Change owner and group
sudo chown :newgroup filename # Change group only
sudo chown -R newowner directory # Recursive ownership change
`
#### chgrp (Change Group) Changes group ownership.
`bash
sudo chgrp newgroup filename # Change group
sudo chgrp -R newgroup directory # Recursive group change
`
Package Management
Different Linux distributions use different package management systems.
APT (Advanced Package Tool) - Debian/Ubuntu
#### Package Installation and Removal
`bash
sudo apt update # Update package list
sudo apt upgrade # Upgrade all packages
sudo apt install package_name # Install package
sudo apt remove package_name # Remove package
sudo apt purge package_name # Remove package and config files
sudo apt autoremove # Remove unused dependencies
`
#### Package Information
`bash
apt search keyword # Search for packages
apt show package_name # Show package information
apt list --installed # List installed packages
apt list --upgradable # List upgradable packages
`
YUM/DNF - Red Hat/CentOS/Fedora
#### Package Operations
`bash
sudo dnf update # Update all packages
sudo dnf install package_name # Install package
sudo dnf remove package_name # Remove package
sudo dnf search keyword # Search packages
sudo dnf info package_name # Package information
sudo dnf list installed # List installed packages
`
Package Management Comparison
| Operation | APT (Debian/Ubuntu) | DNF (Fedora) | Pacman (Arch) |
|-----------|-------------------|--------------|---------------|
| Update package list | apt update | dnf check-update | pacman -Sy |
| Install package | apt install pkg | dnf install pkg | pacman -S pkg |
| Remove package | apt remove pkg | dnf remove pkg | pacman -R pkg |
| Search package | apt search term | dnf search term | pacman -Ss term |
| List installed | apt list --installed | dnf list installed | pacman -Q |
System Monitoring
System Information
#### uname System information.
`bash
uname -a # All system information
uname -r # Kernel version
uname -m # Machine architecture
`
#### df (Disk Free) Shows disk space usage.
`bash
df # Basic disk usage
df -h # Human-readable format
df -T # Include filesystem type
df /path/to/directory # Specific filesystem
`
#### du (Disk Usage) Shows directory space usage.
`bash
du # Current directory usage
du -h # Human-readable format
du -s # Summary only
du -sh * # Size of each item in current directory
`
#### free Shows memory usage.
`bash
free # Basic memory info
free -h # Human-readable format
free -m # Show in MB
free -g # Show in GB
`
Memory Information Fields:
| Field | Description | |-------|-------------| | total | Total installed memory | | used | Used memory | | free | Unused memory | | shared | Memory used by tmpfs | | buff/cache | Buffers and cache | | available | Available memory for new applications |
Process Monitoring
#### htop Enhanced version of top (if installed).
`bash
htop # Interactive process viewer
`
htop Advantages: - Color-coded display - Mouse support - Tree view of processes - Easy process killing - System resource meters
#### iostat I/O statistics (part of sysstat package).
`bash
iostat # Basic I/O stats
iostat -x 1 # Extended stats every second
iostat -h # Human-readable format
`
Network Commands
Network Configuration
#### ip Modern network configuration tool.
`bash
ip addr show # Show IP addresses
ip route show # Show routing table
ip link show # Show network interfaces
ip addr add 192.168.1.100/24 dev eth0 # Add IP address
`
#### ifconfig Traditional network interface configuration.
`bash
ifconfig # Show all interfaces
ifconfig eth0 # Show specific interface
ifconfig eth0 up # Enable interface
ifconfig eth0 down # Disable interface
`
Network Connectivity
#### ping Test network connectivity.
`bash
ping google.com # Continuous ping
ping -c 4 google.com # Ping 4 times
ping -i 2 google.com # Ping every 2 seconds
`
#### wget and curl Download files from web.
`bash
wget https://example.com/file.txt # Download file
wget -O newname.txt https://example.com/file.txt # Download with new name
curl https://api.example.com # Make HTTP request
curl -o file.txt https://example.com/file.txt # Download with curl
`
#### netstat Network statistics.
`bash
netstat -tuln # Show listening ports
netstat -r # Show routing table
netstat -i # Show interface statistics
`
SSH (Secure Shell)
#### Basic SSH Usage
`bash
ssh username@hostname # Connect to remote host
ssh -p 2222 username@hostname # Connect using specific port
ssh-keygen -t rsa # Generate SSH key pair
ssh-copy-id username@hostname # Copy public key to remote host
`
#### SCP (Secure Copy)
`bash
scp file.txt username@hostname:/path/ # Copy file to remote host
scp username@hostname:/path/file.txt . # Copy file from remote host
scp -r directory username@hostname:/path/ # Copy directory recursively
`
Text Processing
File Viewing Commands
#### cat Display file contents.
`bash
cat filename.txt # Display entire file
cat file1.txt file2.txt # Display multiple files
cat -n filename.txt # Display with line numbers
`
#### less and more Page through file contents.
`bash
less filename.txt # View file with navigation
more filename.txt # View file page by page
`
less Navigation: - Space: Next page - b: Previous page - /: Search forward - ?: Search backward - q: Quit
#### head and tail View beginning or end of files.
`bash
head filename.txt # First 10 lines
head -n 20 filename.txt # First 20 lines
tail filename.txt # Last 10 lines
tail -n 5 filename.txt # Last 5 lines
tail -f logfile.txt # Follow file changes (live)
`
Text Search and Manipulation
#### grep Search text patterns.
`bash
grep "pattern" filename.txt # Search for pattern
grep -i "pattern" filename.txt # Case-insensitive search
grep -r "pattern" directory/ # Recursive search
grep -n "pattern" filename.txt # Show line numbers
grep -v "pattern" filename.txt # Invert match (exclude pattern)
`
Common grep Options:
| Option | Description |
|--------|-------------|
| -i | Case-insensitive search |
| -r | Recursive search in directories |
| -n | Show line numbers |
| -v | Invert match |
| -c | Count matching lines |
| -l | Show only filenames with matches |
| -w | Match whole words only |
#### sed (Stream Editor) Text transformation tool.
`bash
sed 's/old/new/' filename.txt # Replace first occurrence per line
sed 's/old/new/g' filename.txt # Replace all occurrences
sed '5d' filename.txt # Delete line 5
sed '/pattern/d' filename.txt # Delete lines matching pattern
`
#### awk Pattern scanning and processing.
`bash
awk '{print $1}' filename.txt # Print first column
awk '/pattern/ {print}' filename.txt # Print lines matching pattern
awk 'NR==5' filename.txt # Print line 5
awk '{print NF}' filename.txt # Print number of fields per line
`
File Comparison
#### diff Compare files line by line.
`bash
diff file1.txt file2.txt # Basic comparison
diff -u file1.txt file2.txt # Unified format
diff -r dir1/ dir2/ # Compare directories recursively
`
#### sort Sort file contents.
`bash
sort filename.txt # Alphabetical sort
sort -n filename.txt # Numerical sort
sort -r filename.txt # Reverse sort
sort -k2 filename.txt # Sort by second column
`
#### uniq Remove duplicate lines.
`bash
uniq filename.txt # Remove consecutive duplicates
sort filename.txt | uniq # Remove all duplicates
uniq -c filename.txt # Count occurrences
uniq -d filename.txt # Show only duplicates
`
Input/Output Redirection
#### Redirection Operators
| Operator | Description | Example |
|----------|-------------|---------|
| > | Redirect output (overwrite) | command > file.txt |
| >> | Redirect output (append) | command >> file.txt |
| < | Redirect input | command < input.txt |
| 2> | Redirect stderr | command 2> error.log |
| 2>&1 | Redirect stderr to stdout | command > file.txt 2>&1 |
| | | Pipe output to another command | command1 | command2 |
#### Practical Examples
`bash
ls -l > directory_listing.txt # Save directory listing to file
echo "New line" >> file.txt # Append text to file
grep "error" /var/log/syslog > errors.txt # Save error lines to file
ps aux | grep firefox # Find Firefox processes
cat file.txt | sort | uniq # Sort and remove duplicates
find /home -name "*.txt" 2>/dev/null # Suppress error messages
`
This comprehensive guide covers the fundamental aspects of Linux operating system usage, from basic navigation to advanced text processing and system administration tasks. Each command includes practical examples and important options to help users understand and effectively utilize Linux systems.