Available for day contractsFrom 21st September I have availability for day and half day contracts. Please contact for more information.

Contact →
mikepreston.org

Linux CLI

Essential command-line tools for file operations, text processing, process management, and system administration.

Linux CLI

Essential command-line tools for file operations, text processing, process management, and system administration.

Overview

The Linux command-line interface provides powerful tools for managing files, processes, users, networks, and system resources. These commands form the foundation of system administration, automation, and DevOps workflows. Mastering these tools enables efficient server management, scripting, and troubleshooting across all Linux distributions.

Linux File System Hierarchy//bin/etc/home/var/usr/tmp/opt/dev/procEssential binariesConfiguration filesUser homedirectoriesVariable data, logsUser programsTemporary filesOptional packagesDevice filesProcess informationLinux File System Hierarchy//bin/etc/home/var/usr/tmp/opt/dev/procEssential binariesConfiguration filesUser homedirectoriesVariable data, logsUser programsTemporary filesOptional packagesDevice filesProcess information

File Operations

File management is fundamental to Linux system administration, involving listing, copying, moving, removing, and searching for files.

Key Concepts

  • Paths: Absolute paths start with /, relative paths start from the current directory
  • Wildcards: * matches any characters, ? matches single character, [] matches character ranges
  • Hidden files: Files starting with . are hidden from normal listings
  • Inodes: Each file has a unique inode containing metadata (permissions, timestamps, size)

Common Commands

# ls - List directory contents
ls                          # Basic listing
ls -la                      # Long format with hidden files
ls -lh                      # Human-readable sizes
ls -lt                      # Sort by modification time
ls -lS                      # Sort by size
ls -R                       # Recursive listing
ls -d */                    # List directories only

# cp - Copy files and directories
cp file1 file2              # Copy file
cp -r dir1 dir2             # Copy directory recursively
cp -p file1 file2           # Preserve permissions and timestamps
cp -i file1 file2           # Interactive (prompt before overwrite)
cp -v file1 file2           # Verbose output
cp file1 file2 dir/         # Copy multiple files to directory

# mv - Move or rename files
mv file1 file2              # Rename file
mv file dir/                # Move file to directory
mv -i file1 file2           # Interactive (prompt before overwrite)
mv -n file1 file2           # No clobber (don't overwrite)
mv dir1 dir2                # Rename directory

# rm - Remove files and directories
rm file                     # Remove file
rm -r dir                   # Remove directory recursively
rm -f file                  # Force removal (no prompts)
rm -rf dir                  # Force remove directory
rm -i file                  # Interactive (prompt for each file)
rm -v file                  # Verbose output

# mkdir and rmdir - Directory operations
mkdir dir                   # Create directory
mkdir -p path/to/dir        # Create parent directories as needed
mkdir -m 755 dir            # Create with specific permissions
rmdir dir                   # Remove empty directory

Finding Files

# find - Search for files in directory hierarchy
find /path -name "*.txt"                    # Find by name
find /path -iname "*.txt"                   # Case-insensitive name search
find /path -type f                          # Find files only
find /path -type d                          # Find directories only
find /path -type l                          # Find symbolic links
find /path -size +10M                       # Files larger than 10MB
find /path -size -1k                        # Files smaller than 1KB
find /path -mtime -7                        # Modified in last 7 days
find /path -mmin -60                        # Modified in last 60 minutes
find /path -user username                   # Files owned by user
find /path -perm 644                        # Files with exact permissions
find /path -perm -u+x                       # Files with user execute permission
find /path -empty                           # Empty files and directories
find /path -name "*.log" -delete            # Find and delete
find /path -name "*.txt" -exec cat {} \;    # Execute command on results
find /path -name "*.sh" -exec chmod +x {} + # Efficient exec with +

# locate - Find files by name (uses database)
locate filename                             # Find file by name
locate -i filename                          # Case-insensitive
locate -c filename                          # Count matching entries
updatedb                                    # Update locate database (run as root)

# which and whereis - Locate commands
which python                                # Show full path of command
whereis python                              # Show binary, source, and man page

Examples

# Find all Python files modified in the last day
find /home/user/projects -name "*.py" -mtime -1

# Copy directory preserving structure and permissions
cp -rp /source/dir /backup/dir

# Find and remove all .tmp files older than 7 days
find /tmp -name "*.tmp" -mtime +7 -delete

# List top 10 largest files in a directory
ls -lhS /var/log | head -10

# Move all .log files to archive directory
mv /var/log/*.log /var/log/archive/

# Find files with specific permission and fix them
find /var/www -type f -perm 777 -exec chmod 644 {} \;

Text Processing

Linux provides powerful tools for viewing, searching, filtering, and transforming text data.

Key Concepts

  • Streams: stdin (0), stdout (1), stderr (2) for input/output
  • Pipes: Connect output of one command to input of another with |
  • Regular expressions: Pattern matching for text searching and manipulation
  • Field-based processing: Many commands can work with delimited fields

Common Commands

# cat - Concatenate and display files
cat file                    # Display file contents
cat file1 file2             # Concatenate files
cat -n file                 # Number all lines
cat -b file                 # Number non-empty lines
cat -s file                 # Squeeze multiple blank lines

# less and more - Pagers
less file                   # View file with scrolling
less +F file                # Follow mode (like tail -f)
more file                   # Basic pager

# head and tail - Display parts of files
head file                   # First 10 lines
head -n 20 file             # First 20 lines
head -c 100 file            # First 100 bytes
tail file                   # Last 10 lines
tail -n 20 file             # Last 20 lines
tail -f file                # Follow file updates
tail -F file                # Follow with retry (handles rotation)

# wc - Word, line, and character count
wc file                     # Lines, words, bytes
wc -l file                  # Line count only
wc -w file                  # Word count only
wc -c file                  # Byte count only
wc -m file                  # Character count

Searching and Filtering

# grep - Search for patterns
grep "pattern" file                 # Search for pattern
grep -i "pattern" file              # Case-insensitive
grep -v "pattern" file              # Invert match (exclude)
grep -n "pattern" file              # Show line numbers
grep -c "pattern" file              # Count matches
grep -l "pattern" *.txt             # List files with matches
grep -L "pattern" *.txt             # List files without matches
grep -r "pattern" /path             # Recursive search
grep -w "word" file                 # Match whole words only
grep -A 3 "pattern" file            # Show 3 lines after match
grep -B 3 "pattern" file            # Show 3 lines before match
grep -C 3 "pattern" file            # Show 3 lines context
grep -E "pat1|pat2" file            # Extended regex (egrep)
grep -P "\d{3}-\d{4}" file          # Perl regex
grep --include="*.py" -r "def" .    # Search specific file types

Text Transformation

# sed - Stream editor
sed 's/old/new/' file               # Replace first occurrence per line
sed 's/old/new/g' file              # Replace all occurrences
sed -i 's/old/new/g' file           # Edit file in place
sed -i.bak 's/old/new/g' file       # In place with backup
sed -n '5p' file                    # Print line 5
sed -n '5,10p' file                 # Print lines 5-10
sed '5d' file                       # Delete line 5
sed '/pattern/d' file               # Delete matching lines
sed '/start/,/end/d' file           # Delete range
sed '5i\New text' file              # Insert before line 5
sed '5a\New text' file              # Append after line 5
sed 's/^/    /' file                # Add indentation
sed 's/[[:space:]]*$//' file        # Remove trailing whitespace

# awk - Pattern scanning and processing
awk '{print $1}' file               # Print first field
awk '{print $1, $3}' file           # Print multiple fields
awk -F: '{print $1}' file           # Custom field separator
awk '/pattern/ {print}' file        # Print matching lines
awk '$3 > 100 {print $1}' file      # Conditional printing
awk '{sum += $1} END {print sum}' file  # Calculate sum
awk 'NR > 1' file                   # Skip header line
awk '{print NR, $0}' file           # Add line numbers
awk 'BEGIN {FS=":"} {print $1}' file    # Set separator in BEGIN
awk '{printf "%-20s %s\n", $1, $2}' file    # Formatted output

# cut - Extract columns
cut -d: -f1 /etc/passwd             # Extract field 1
cut -d, -f1,3 file.csv              # Multiple fields
cut -c1-10 file                     # Extract characters 1-10
cut -c-5 file                       # First 5 characters
cut -f2- file                       # Field 2 onwards

# sort - Sort lines
sort file                           # Alphabetical sort
sort -n file                        # Numeric sort
sort -r file                        # Reverse order
sort -k2 file                       # Sort by field 2
sort -t: -k3 -n file                # Custom delimiter, numeric
sort -u file                        # Unique lines only
sort -h file                        # Human-readable numbers (1K, 2M)
sort -M file                        # Sort by month name
sort -f file                        # Ignore case

# uniq - Report or omit repeated lines
sort file | uniq                    # Remove duplicates
sort file | uniq -c                 # Count occurrences
sort file | uniq -d                 # Show duplicates only
sort file | uniq -u                 # Show unique only
sort file | uniq -i                 # Ignore case

# tr - Translate characters
tr 'a-z' 'A-Z' < file               # Lowercase to uppercase
tr -d '\r' < file                   # Remove carriage returns
tr -s ' ' < file                    # Squeeze multiple spaces
tr '[:lower:]' '[:upper:]' < file   # Using character classes
tr -d '[:punct:]' < file            # Remove punctuation

Examples

# Count unique IP addresses in access log
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10

# Extract email addresses from file
grep -oE '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' file

# Replace multiple spaces with single space
sed 's/  */ /g' file

# Calculate average of numbers in a column
awk '{sum += $1; count++} END {print sum/count}' numbers.txt

# Extract specific columns from CSV
cut -d, -f2,5 data.csv | sort -t, -k2 -n

# Find lines containing both patterns
grep "pattern1" file | grep "pattern2"

# Remove blank lines and comments
sed '/^$/d; /^#/d' config.file

Process Management

Managing running processes is essential for system administration and troubleshooting.

fork()AdmittedScheduledPreemptedI/O or EventI/O CompleteSIGSTOPSIGCONTexit()wait()CreatedReadyRunningWaitingStoppedZombiefork()AdmittedScheduledPreemptedI/O or EventI/O CompleteSIGSTOPSIGCONTexit()wait()CreatedReadyRunningWaitingStoppedZombie

Key Concepts

  • PID: Process ID, unique identifier for each process
  • PPID: Parent process ID
  • Signals: Software interrupts sent to processes (SIGTERM, SIGKILL, SIGSTOP)
  • Process states: Running (R), Sleeping (S), Stopped (T), Zombie (Z)
  • Foreground/Background: Processes can run in foreground or background

Common Commands

# ps - Report process status
ps                          # Current user's processes
ps aux                      # All processes, full format
ps -ef                      # All processes, standard format
ps -u username              # Processes for specific user
ps -p 1234                  # Specific process by PID
ps --forest                 # Show process tree
ps -eo pid,ppid,cmd,%cpu,%mem --sort=-%cpu   # Custom format, sorted

# top - Dynamic process viewer
top                         # Interactive process viewer
top -u username             # Processes for specific user
top -p 1234                 # Monitor specific PID
top -n 1 -b                 # Batch mode, one iteration

# htop - Enhanced interactive process viewer
htop                        # Interactive viewer (if installed)
htop -u username            # Filter by user
htop -p 1234,5678           # Monitor specific PIDs

# pgrep and pkill - Process lookup
pgrep nginx                 # Find PIDs by name
pgrep -u root nginx         # By user and name
pgrep -l nginx              # Show names with PIDs
pkill nginx                 # Kill by name
pkill -9 nginx              # Force kill by name
pkill -u username           # Kill all processes by user

Process Control

# kill - Send signals to processes
kill 1234                   # Send SIGTERM (graceful)
kill -9 1234                # Send SIGKILL (force)
kill -15 1234               # Send SIGTERM (same as default)
kill -STOP 1234             # Pause process
kill -CONT 1234             # Resume process
kill -HUP 1234              # Reload configuration
kill -l                     # List all signals
killall nginx               # Kill all processes by name

# Job control
command &                   # Run in background
jobs                        # List background jobs
jobs -l                     # List with PIDs
fg                          # Bring last job to foreground
fg %1                       # Bring job 1 to foreground
bg                          # Resume last job in background
bg %1                       # Resume job 1 in background
Ctrl+Z                      # Suspend current foreground job
Ctrl+C                      # Terminate current foreground job
disown %1                   # Detach job from shell

# nohup and screen
nohup command &             # Run immune to hangups
nohup command > output.log 2>&1 &   # With output redirection

# nice and renice - Process priority
nice -n 10 command          # Start with lower priority
nice -n -10 command         # Start with higher priority (root)
renice 10 -p 1234           # Change priority of running process
renice -10 -u username      # Change priority for user's processes

Monitoring

# lsof - List open files
lsof                        # All open files
lsof -u username            # Files opened by user
lsof -p 1234                # Files opened by process
lsof -i :80                 # Processes using port 80
lsof +D /var/log            # Files in directory
lsof -c nginx               # Files opened by command

# strace - Trace system calls
strace command              # Trace system calls
strace -p 1234              # Attach to running process
strace -f command           # Follow child processes
strace -e open command      # Trace specific syscalls

Examples

# Find memory-intensive processes
ps aux --sort=-%mem | head -10

# Kill all processes matching pattern
pkill -f "python script.py"

# Monitor specific process CPU and memory
top -p $(pgrep -d, nginx)

# Find process using a specific port
lsof -i :8080

# Run long command that survives logout
nohup ./backup.sh > backup.log 2>&1 &
disown

# Suspend and resume a process
kill -STOP 1234
kill -CONT 1234

System Information

Commands for viewing system configuration, resource usage, and performance metrics.

Common Commands

# uname - System information
uname -a                    # All system info
uname -r                    # Kernel release
uname -m                    # Hardware architecture
uname -n                    # Hostname
uname -s                    # Kernel name
uname -v                    # Kernel version

# hostname
hostname                    # Show hostname
hostname -f                 # Fully qualified domain name
hostname -I                 # All IP addresses
hostnamectl                 # Detailed host info (systemd)

# uptime
uptime                      # System uptime and load averages
uptime -p                   # Pretty format
uptime -s                   # System boot time

# Memory information
free                        # Memory usage
free -h                     # Human-readable
free -m                     # In megabytes
free -g                     # In gigabytes
free -s 5                   # Update every 5 seconds
cat /proc/meminfo           # Detailed memory info

# Disk space
df                          # Disk space usage
df -h                       # Human-readable sizes
df -T                       # Show filesystem type
df -i                       # Show inode usage
df /home                    # Specific mount point

# Directory size
du -sh /path                # Total size of directory
du -h /path                 # Size of each subdirectory
du -sh *                    # Size of items in current directory
du -ah /path                # All files and directories
du -h --max-depth=1 /path   # Limit depth
du -ch *.log | tail -1      # Total size of matching files

# CPU information
lscpu                       # CPU architecture info
cat /proc/cpuinfo           # Detailed CPU info
nproc                       # Number of processors

# Hardware information
lshw                        # Detailed hardware info
lshw -short                 # Summary
lspci                       # PCI devices
lsusb                       # USB devices
dmidecode                   # DMI/SMBIOS info (root)

Examples

# Quick system overview
echo "Hostname: $(hostname)"
echo "Kernel: $(uname -r)"
echo "Uptime: $(uptime -p)"
echo "Memory: $(free -h | awk '/Mem:/ {print $3 "/" $2}')"
echo "Disk: $(df -h / | awk 'NR==2 {print $3 "/" $2}')"

# Find directories consuming most space
du -h /var --max-depth=2 | sort -rh | head -20

# Monitor system resources
watch -n 2 'free -h; echo; df -h /'

User Management

Creating, modifying, and managing user accounts and authentication.

Key Concepts

  • UID: User ID, unique numeric identifier (root is 0)
  • GID: Group ID, numeric identifier for primary group
  • Primary group: Default group for new files created by user
  • Secondary groups: Additional group memberships
  • /etc/passwd: User account information
  • /etc/shadow: Encrypted passwords
  • /etc/group: Group information

Common Commands

# User creation and modification
useradd username                    # Create user
useradd -m username                 # Create with home directory
useradd -m -s /bin/bash username    # Specify shell
useradd -m -G sudo,docker username  # Add to groups
useradd -u 1001 username            # Specify UID
useradd -d /custom/home username    # Specify home directory

usermod -aG groupname username      # Add user to group
usermod -G group1,group2 username   # Set groups (replaces existing)
usermod -s /bin/zsh username        # Change shell
usermod -d /new/home username       # Change home directory
usermod -l newname oldname          # Rename user
usermod -L username                 # Lock account
usermod -U username                 # Unlock account

userdel username                    # Delete user
userdel -r username                 # Delete user and home directory

# Password management
passwd                              # Change own password
passwd username                     # Change user's password (root)
passwd -l username                  # Lock password
passwd -u username                  # Unlock password
passwd -e username                  # Expire password (force change)
passwd -S username                  # Show password status
chage -l username                   # Password aging info
chage -M 90 username                # Max password age 90 days
chage -E 2024-12-31 username        # Account expiry date

# Group management
groupadd groupname                  # Create group
groupadd -g 1001 groupname          # Specify GID
groupmod -n newname oldname         # Rename group
groupdel groupname                  # Delete group
gpasswd -a username groupname       # Add user to group
gpasswd -d username groupname       # Remove user from group

# User information
id                                  # Current user's ID info
id username                         # Specific user's ID info
whoami                              # Current username
who                                 # Logged in users
w                                   # Who and what they're doing
last                                # Login history
lastlog                             # Last login times
groups username                     # User's groups

# Switch user
su - username                       # Switch user (login shell)
su username                         # Switch user (non-login shell)
sudo command                        # Run command as root
sudo -u username command            # Run command as user
sudo -i                             # Interactive root shell
sudo -l                             # List allowed commands
visudo                              # Edit sudoers file safely

Examples

# Create new user with full setup
useradd -m -s /bin/bash -G sudo,docker newuser
passwd newuser

# Add existing user to multiple groups
usermod -aG wheel,developers username

# Force password change on next login
chage -d 0 username

# View user's groups and permissions
id username
groups username
sudo -l -U username

# Lock an account temporarily
usermod -L username
# or
passwd -l username

Permissions

Linux file permissions control access for users, groups, and others.

Permission ModelFile/DirectoryOwner (User)GroupOthersr - Read (4)w - Write (2)x - Execute (1)r - Read (4)w - Write (2)x - Execute (1)r - Read (4)w - Write (2)x - Execute (1)Permission ModelFile/DirectoryOwner (User)GroupOthersr - Read (4)w - Write (2)x - Execute (1)r - Read (4)w - Write (2)x - Execute (1)r - Read (4)w - Write (2)x - Execute (1)

Key Concepts

  • Read (r/4): View file contents or list directory
  • Write (w/2): Modify file or create/delete in directory
  • Execute (x/1): Run file as program or access directory
  • Numeric notation: Sum of permission values (e.g., 755 = rwxr-xr-x)
  • Symbolic notation: Letters and operators (e.g., u+x, g-w, o=r)
  • Special permissions: SUID, SGID, Sticky bit
  • umask: Default permission mask for new files

Common Commands

# chmod - Change file mode/permissions
# Symbolic mode
chmod u+x file                  # Add execute for owner
chmod g-w file                  # Remove write for group
chmod o=r file                  # Set others to read only
chmod a+r file                  # Add read for all
chmod u+x,g+x file              # Multiple changes
chmod -R u+rw dir               # Recursive

# Numeric mode
chmod 755 file                  # rwxr-xr-x
chmod 644 file                  # rw-r--r--
chmod 700 dir                   # rwx------
chmod 600 file                  # rw-------
chmod -R 755 dir                # Recursive

# Special permissions
chmod u+s file                  # Set SUID
chmod g+s dir                   # Set SGID
chmod +t dir                    # Set sticky bit
chmod 4755 file                 # SUID with numeric
chmod 2755 dir                  # SGID with numeric
chmod 1777 dir                  # Sticky bit with numeric

# chown - Change owner
chown user file                 # Change owner
chown user:group file           # Change owner and group
chown :group file               # Change group only
chown -R user:group dir         # Recursive

# chgrp - Change group
chgrp group file                # Change group
chgrp -R group dir              # Recursive

# umask - Set default permissions
umask                           # Show current umask
umask 022                       # New files: 644, dirs: 755
umask 077                       # New files: 600, dirs: 700
umask -S                        # Show in symbolic form

# View permissions
ls -l file                      # Show permissions
stat file                       # Detailed file info
getfacl file                    # ACL permissions

# ACL (Access Control Lists)
setfacl -m u:username:rwx file  # Add ACL entry
setfacl -m g:groupname:rx file  # Add group ACL
setfacl -x u:username file      # Remove ACL entry
setfacl -b file                 # Remove all ACLs
getfacl file                    # Show ACLs

Permission Reference

Numeric Symbolic Meaning
0 --- No permissions
1 --x Execute only
2 -w- Write only
3 -wx Write and execute
4 r-- Read only
5 r-x Read and execute
6 rw- Read and write
7 rwx Full permissions

Examples

# Secure a private key file
chmod 600 ~/.ssh/id_rsa

# Web server directory permissions
chmod 755 /var/www/html
chown -R www-data:www-data /var/www/html

# Shared directory with group write
mkdir /shared
chmod 2775 /shared
chown :developers /shared

# Make script executable
chmod +x script.sh

# Find files with insecure permissions
find /var/www -type f -perm 777

# Copy permissions from another file
chmod --reference=file1 file2

# Set default permissions for new files
umask 027  # Files: 640, Directories: 750

Networking

Commands for network configuration, diagnostics, and data transfer.

Network Configuration

# ip - Modern network configuration
ip addr                         # Show all IP addresses
ip addr show eth0               # Show interface info
ip link                         # Show network interfaces
ip link set eth0 up             # Enable interface
ip link set eth0 down           # Disable interface
ip route                        # Show routing table
ip route add default via 192.168.1.1    # Add default route
ip neigh                        # Show ARP cache
ip -s link                      # Show statistics

# Legacy ifconfig (deprecated but common)
ifconfig                        # Show all interfaces
ifconfig eth0                   # Show specific interface
ifconfig eth0 up                # Enable interface
ifconfig eth0 192.168.1.100     # Set IP address

# DNS
cat /etc/resolv.conf            # View DNS configuration
nslookup domain.com             # Query DNS
dig domain.com                  # DNS lookup
dig +short domain.com           # Brief output
host domain.com                 # DNS lookup

Connection Information

# ss - Socket statistics (modern replacement for netstat)
ss                              # All connections
ss -t                           # TCP connections
ss -u                           # UDP connections
ss -l                           # Listening sockets
ss -p                           # Show process using socket
ss -n                           # Numeric output (no DNS)
ss -tuln                        # TCP/UDP listening, numeric
ss -tlnp                        # TCP listening with process
ss state established            # Established connections
ss dst 192.168.1.1              # Connections to specific host
ss sport = :22                  # Connections from port 22

# netstat - Network statistics (legacy but widely available)
netstat -tuln                   # Listening ports
netstat -an                     # All connections, numeric
netstat -p                      # Show PID/program
netstat -r                      # Routing table
netstat -s                      # Protocol statistics
netstat -i                      # Interface statistics

Network Diagnostics

# ping - Test connectivity
ping host                       # Continuous ping
ping -c 4 host                  # Send 4 packets
ping -i 0.5 host                # Interval 0.5 seconds
ping -W 2 host                  # Timeout 2 seconds

# traceroute - Trace route to host
traceroute host                 # Show route
traceroute -n host              # Numeric output
tracepath host                  # No root required

# mtr - Combined ping and traceroute
mtr host                        # Interactive
mtr --report host               # Report mode

Data Transfer

# curl - Transfer data from/to servers
curl url                        # GET request
curl -o file url                # Save to file
curl -O url                     # Save with original filename
curl -L url                     # Follow redirects
curl -I url                     # Headers only
curl -X POST url                # POST request
curl -d "data" url              # POST with data
curl -d @file url               # POST from file
curl -H "Content-Type: application/json" url    # Custom header
curl -u user:pass url           # Basic auth
curl -k url                     # Ignore SSL errors
curl -s url                     # Silent mode
curl -w "%{http_code}" url      # Show response code
curl --retry 3 url              # Retry on failure

# wget - Download files
wget url                        # Download file
wget -O file url                # Save with specific name
wget -c url                     # Continue interrupted download
wget -r url                     # Recursive download
wget -q url                     # Quiet mode
wget --limit-rate=1m url        # Limit download speed
wget -b url                     # Background download
wget --mirror url               # Mirror website

# nc (netcat) - Network utility
nc -zv host 80                  # Port scan
nc -l 8080                      # Listen on port
nc host 80                      # Connect to port
nc -u host 53                   # UDP connection
echo "test" | nc host 80        # Send data
nc -l 8080 < file               # Serve file
nc host 8080 > file             # Receive file

# scp - Secure copy
scp file user@host:/path        # Copy to remote
scp user@host:/path file        # Copy from remote
scp -r dir user@host:/path      # Copy directory
scp -P 2222 file user@host:/path    # Custom port

# rsync - Efficient file sync
rsync -av source/ dest/         # Archive mode, verbose
rsync -avz source/ user@host:/dest/     # Compress, remote
rsync -av --delete source/ dest/        # Mirror (delete extra)
rsync -av --exclude="*.tmp" source/ dest/   # Exclude patterns
rsync -avP source/ dest/        # Show progress
rsync -n source/ dest/          # Dry run

Examples

# Check if port is open
nc -zv host 443 && echo "Port open" || echo "Port closed"

# Find process using port 80
ss -tlnp | grep :80

# Download file with progress
curl -LO --progress-bar https://example.com/file.tar.gz

# Test HTTP endpoint
curl -w "\nStatus: %{http_code}\nTime: %{time_total}s\n" https://api.example.com

# Sync directory to remote server
rsync -avz --progress /local/dir/ user@host:/remote/dir/

# Check external IP
curl -s ifconfig.me

Archives

Creating and extracting compressed archives.

Key Concepts

  • tar: Tape archive, combines files into single archive
  • gzip: GNU zip compression (.gz)
  • bzip2: Higher compression ratio (.bz2)
  • xz: Best compression ratio (.xz)
  • zip: Cross-platform archive format

Common Commands

# tar - Tape archiver
# Create archives
tar -cvf archive.tar dir/               # Create tar archive
tar -czvf archive.tar.gz dir/           # Create gzip compressed
tar -cjvf archive.tar.bz2 dir/          # Create bzip2 compressed
tar -cJvf archive.tar.xz dir/           # Create xz compressed
tar -cvf archive.tar file1 file2 dir/   # Multiple items

# Extract archives
tar -xvf archive.tar                    # Extract tar
tar -xzvf archive.tar.gz                # Extract gzip
tar -xjvf archive.tar.bz2               # Extract bzip2
tar -xJvf archive.tar.xz                # Extract xz
tar -xvf archive.tar -C /destination    # Extract to directory

# List contents
tar -tvf archive.tar                    # List contents
tar -tzvf archive.tar.gz                # List gzip archive

# Extract specific files
tar -xvf archive.tar file.txt           # Extract specific file
tar -xvf archive.tar --wildcards "*.txt"    # Extract by pattern

# Append to archive
tar -rvf archive.tar newfile            # Append file

# Exclude files
tar -czvf archive.tar.gz --exclude="*.tmp" dir/
tar -czvf archive.tar.gz --exclude-vcs dir/     # Exclude VCS dirs

# gzip - Compress files
gzip file                       # Compress file (replaces original)
gzip -k file                    # Keep original file
gzip -d file.gz                 # Decompress
gunzip file.gz                  # Decompress (alias)
gzip -r dir/                    # Compress directory contents
gzip -l file.gz                 # List compression info
zcat file.gz                    # View compressed file

# bzip2 - Higher compression
bzip2 file                      # Compress
bzip2 -k file                   # Keep original
bzip2 -d file.bz2               # Decompress
bunzip2 file.bz2                # Decompress (alias)
bzcat file.bz2                  # View compressed file

# xz - Best compression
xz file                         # Compress
xz -k file                      # Keep original
xz -d file.xz                   # Decompress
unxz file.xz                    # Decompress (alias)
xzcat file.xz                   # View compressed file

# zip/unzip - Cross-platform archives
zip archive.zip file1 file2     # Create zip
zip -r archive.zip dir/         # Recursive zip
zip -e archive.zip file         # Encrypted zip
unzip archive.zip               # Extract zip
unzip archive.zip -d /dest      # Extract to directory
unzip -l archive.zip            # List contents
unzip -t archive.zip            # Test integrity

Examples

# Create compressed backup with date
tar -czvf backup-$(date +%Y%m%d).tar.gz /home/user/data

# Extract specific directory from archive
tar -xzvf archive.tar.gz path/to/dir

# Create archive excluding certain patterns
tar -czvf project.tar.gz --exclude="node_modules" --exclude=".git" project/

# Compare compression methods
tar -cvf - dir/ | gzip > archive.tar.gz
tar -cvf - dir/ | bzip2 > archive.tar.bz2
tar -cvf - dir/ | xz > archive.tar.xz
ls -lh archive.tar.*

# Extract and strip leading directory
tar -xzvf archive.tar.gz --strip-components=1

Disk Management

Managing disks, partitions, and filesystems.

Key Concepts

  • Block devices: Disks and partitions (/dev/sda, /dev/nvme0n1)
  • Partitions: Divisions of a disk (sda1, sda2)
  • Filesystems: Structure for organising data (ext4, xfs, btrfs)
  • Mount points: Directories where filesystems are attached
  • fstab: Configuration file for automatic mounting

Common Commands

# lsblk - List block devices
lsblk                           # List all block devices
lsblk -f                        # Show filesystem info
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT      # Custom columns

# blkid - Block device attributes
blkid                           # Show all UUIDs
blkid /dev/sda1                 # Specific device

# fdisk - Partition management
fdisk -l                        # List all partitions
fdisk -l /dev/sda               # List disk partitions
fdisk /dev/sda                  # Interactive partitioning

# parted - Modern partition tool
parted -l                       # List partitions
parted /dev/sda print           # Print partition table
parted /dev/sda mklabel gpt     # Create GPT table
parted /dev/sda mkpart primary ext4 0% 100%     # Create partition

# mkfs - Create filesystem
mkfs.ext4 /dev/sda1             # Create ext4
mkfs.xfs /dev/sda1              # Create XFS
mkfs -t ext4 /dev/sda1          # Alternative syntax
mkfs.ext4 -L mylabel /dev/sda1  # With label

# mount/umount - Mount filesystems
mount                           # Show mounted filesystems
mount /dev/sda1 /mnt            # Mount device
mount -t ext4 /dev/sda1 /mnt    # Specify filesystem type
mount -o ro /dev/sda1 /mnt      # Mount read-only
mount -o remount,rw /mnt        # Remount with options
mount -a                        # Mount all in fstab
mount UUID=xxx /mnt             # Mount by UUID
umount /mnt                     # Unmount by mount point
umount /dev/sda1                # Unmount by device
umount -l /mnt                  # Lazy unmount (when busy)

# fstab - Filesystem table
# Format: <device> <mount point> <type> <options> <dump> <fsck>
# /dev/sda1  /mnt  ext4  defaults  0  2
# UUID=xxx   /data xfs   defaults  0  2

# Disk information
tune2fs -l /dev/sda1            # ext filesystem info
xfs_info /mnt                   # XFS filesystem info

# Filesystem check
fsck /dev/sda1                  # Check filesystem
fsck -y /dev/sda1               # Auto-fix errors
e2fsck -f /dev/sda1             # Force check ext filesystem
xfs_repair /dev/sda1            # Repair XFS

# Resize filesystem
resize2fs /dev/sda1             # Resize ext filesystem
xfs_growfs /mnt                 # Grow XFS filesystem
lvextend -r -L +10G /dev/vg/lv  # Extend LVM and filesystem

LVM (Logical Volume Management)

# Physical volumes
pvs                             # List physical volumes
pvcreate /dev/sda1              # Create PV
pvdisplay                       # Detailed PV info

# Volume groups
vgs                             # List volume groups
vgcreate vgname /dev/sda1       # Create VG
vgextend vgname /dev/sdb1       # Extend VG
vgdisplay                       # Detailed VG info

# Logical volumes
lvs                             # List logical volumes
lvcreate -L 10G -n lvname vgname    # Create LV
lvextend -L +5G /dev/vgname/lvname  # Extend LV
lvreduce -L -5G /dev/vgname/lvname  # Reduce LV
lvdisplay                       # Detailed LV info

Examples

# Check disk space usage
df -h
du -sh /* 2>/dev/null | sort -rh | head -10

# Mount USB drive
lsblk
mount /dev/sdb1 /mnt/usb

# Create and mount new filesystem
mkfs.ext4 /dev/sdb1
mkdir /data
mount /dev/sdb1 /data
# Add to /etc/fstab for persistence

# Find what's using a mount point
lsof +D /mnt
fuser -vm /mnt

# Check filesystem for errors (unmount first)
umount /dev/sda1
fsck -y /dev/sda1
mount /dev/sda1 /mnt

Systemd

Managing services and viewing logs with systemd, the default init system on modern Linux distributions.

Key Concepts

  • Units: Resources managed by systemd (services, mounts, timers)
  • Targets: Groups of units (similar to runlevels)
  • Journal: Centralised logging system
  • Unit files: Configuration files in /etc/systemd/system or /usr/lib/systemd/system

Service Management

# systemctl - Control system and services
# Service status
systemctl status nginx              # Show service status
systemctl is-active nginx           # Check if active
systemctl is-enabled nginx          # Check if enabled
systemctl is-failed nginx           # Check if failed

# Start/stop services
systemctl start nginx               # Start service
systemctl stop nginx                # Stop service
systemctl restart nginx             # Restart service
systemctl reload nginx              # Reload configuration
systemctl reload-or-restart nginx   # Reload if supported, else restart

# Enable/disable services
systemctl enable nginx              # Enable at boot
systemctl disable nginx             # Disable at boot
systemctl enable --now nginx        # Enable and start
systemctl disable --now nginx       # Disable and stop
systemctl mask nginx                # Prevent service from starting
systemctl unmask nginx              # Remove mask

# List services
systemctl list-units                # All loaded units
systemctl list-units --type=service # All services
systemctl list-units --failed       # Failed units
systemctl list-unit-files           # All unit files
systemctl list-dependencies nginx   # Show dependencies

# System state
systemctl get-default               # Default target
systemctl set-default multi-user.target     # Set default
systemctl isolate rescue.target     # Switch to rescue mode
systemctl daemon-reload             # Reload unit files
systemctl reset-failed              # Reset failed state

# Power management
systemctl poweroff                  # Shutdown
systemctl reboot                    # Reboot
systemctl suspend                   # Suspend
systemctl hibernate                 # Hibernate

Journal and Logging

# journalctl - Query systemd journal
journalctl                          # All logs
journalctl -u nginx                 # Logs for service
journalctl -u nginx -u php-fpm      # Multiple services
journalctl -f                       # Follow logs (like tail -f)
journalctl -f -u nginx              # Follow service logs
journalctl -n 100                   # Last 100 lines
journalctl -p err                   # Priority: err and higher
journalctl -p warning               # Priority: warning and higher
journalctl -b                       # Current boot only
journalctl -b -1                    # Previous boot
journalctl --since "2024-01-01"     # Since date
journalctl --since "1 hour ago"     # Relative time
journalctl --until "2024-01-31"     # Until date
journalctl --since "09:00" --until "10:00"  # Time range
journalctl -k                       # Kernel messages
journalctl _PID=1234                # Specific process
journalctl _UID=1000                # Specific user
journalctl --disk-usage             # Journal disk usage
journalctl --vacuum-size=1G         # Reduce journal to 1GB
journalctl -o json                  # JSON output
journalctl -o json-pretty           # Pretty JSON

Unit File Structure

# View unit file
systemctl cat nginx

# Edit unit file (creates override)
systemctl edit nginx

# Edit full unit file
systemctl edit --full nginx

# Example service unit file
# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=myapp
ExecStart=/usr/bin/myapp
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

Examples

# Check why a service failed
systemctl status nginx
journalctl -u nginx --since "10 minutes ago"

# Find all enabled services
systemctl list-unit-files --type=service --state=enabled

# Restart service and follow logs
systemctl restart nginx && journalctl -fu nginx

# Find boot-related errors
journalctl -b -p err

# See service resource usage
systemctl status nginx -l
systemd-cgtop

# Create and enable a custom service
# 1. Create unit file at /etc/systemd/system/myapp.service
# 2. Reload daemon and enable
systemctl daemon-reload
systemctl enable --now myapp

Quick Reference

Essential File Operations

Command Description
ls -la List all files with details
cp -r src dst Copy directory recursively
mv old new Move/rename files
rm -rf dir Remove directory recursively
find /path -name "*.txt" Find files by name
chmod 755 file Set permissions
chown user:group file Change ownership

Text Processing

Command Description
grep -r "pattern" /path Recursive text search
sed 's/old/new/g' file Find and replace
awk '{print $1}' file Print first column
sort | uniq -c Count unique occurrences
head -n 20 file First 20 lines
tail -f file Follow file updates

Process Management

Command Description
ps aux All processes
top / htop Interactive process viewer
kill -9 PID Force kill process
pkill -f pattern Kill by pattern
bg / fg Background/foreground jobs

System Information

Command Description
df -h Disk space usage
du -sh dir Directory size
free -h Memory usage
uptime System uptime
uname -a System information

Networking

Command Description
ip addr Show IP addresses
ss -tuln Listening ports
curl -O url Download file
ping -c 4 host Test connectivity
nc -zv host port Port check

Archives

Command Description
tar -czvf archive.tar.gz dir/ Create compressed archive
tar -xzvf archive.tar.gz Extract compressed archive
zip -r archive.zip dir/ Create zip archive
unzip archive.zip Extract zip archive

Systemd

Command Description
systemctl status service Service status
systemctl restart service Restart service
systemctl enable service Enable at boot
journalctl -fu service Follow service logs

Common Issues and Solutions

Issue: Permission Denied

Problem: Cannot access file or directory despite being the owner.

# Check permissions
ls -la file

# Check if directory has execute permission
ls -ld /path/to/dir

# Fix file permissions
chmod 644 file
chmod 755 directory

# Check ACLs
getfacl file

# Check SELinux context (if enabled)
ls -Z file
restorecon -v file

Issue: Disk Space Full

Problem: System reports no space left on device.

# Find large files
du -h / --max-depth=1 2>/dev/null | sort -rh | head -20

# Find large log files
find /var/log -type f -size +100M

# Clear old journal logs
journalctl --vacuum-size=500M

# Find and remove old files
find /tmp -type f -mtime +7 -delete

# Check for deleted files still in use
lsof | grep deleted

Issue: Process Won't Die

Problem: Process doesn't respond to kill command.

# Try SIGTERM first
kill PID

# If unresponsive, use SIGKILL
kill -9 PID

# If still not working, check process state
ps aux | grep PID
cat /proc/PID/status | grep State

# For zombie processes, kill parent
ps -eo pid,ppid,state,cmd | grep Z
kill -9 PPID

Issue: Cannot Mount Filesystem

Problem: Mount command fails or filesystem is read-only.

# Check for filesystem errors
dmesg | tail -20
fsck -y /dev/sda1

# Check if already mounted
mount | grep sda1

# Force remount as read-write
mount -o remount,rw /mnt

# Check fstab for errors
mount -a

# Device busy - find what's using it
lsof +D /mnt
fuser -vm /mnt

Issue: Network Connection Problems

Problem: Cannot reach network hosts.

# Check interface status
ip link
ip addr

# Check routing
ip route
ping -c 4 gateway_ip

# Check DNS
cat /etc/resolv.conf
dig google.com

# Check firewall
iptables -L -n
ss -tuln

# Check if service is listening
ss -tlnp | grep :80

Issue: Service Won't Start

Problem: Systemd service fails to start.

# Check service status
systemctl status service

# View recent logs
journalctl -u service -n 50

# Check unit file syntax
systemd-analyze verify /etc/systemd/system/service.service

# Check for dependency issues
systemctl list-dependencies service

# Reload after unit file changes
systemctl daemon-reload
systemctl restart service

Issue: High CPU or Memory Usage

Problem: System is slow due to resource exhaustion.

# Find CPU-intensive processes
top -o %CPU
ps aux --sort=-%cpu | head -10

# Find memory-intensive processes
top -o %MEM
ps aux --sort=-%mem | head -10

# Check for memory leaks
watch -n 5 'free -h'

# Find process consuming most file descriptors
ls /proc/*/fd 2>/dev/null | wc -l
lsof | awk '{print $2}' | sort | uniq -c | sort -rn | head -10

Related Topics

To expand your Linux command-line skills, explore these related topics:

  • Bash - Shell scripting for automation and complex workflows
  • zsh - Advanced shell with powerful completion and plugins
  • tmux - Terminal multiplexer for session management
  • vim - Modal text editor for efficient file editing
  • systemd - In-depth service management and system configuration
  • Ansible - Automation tool that uses many Linux CLI concepts