Linux Command Cheat Sheet

This cheat sheet covers essential Linux commands for file management, system administration, and common tasks.

File Operations

# List directory contents
ls
ls -la          # Show hidden files with details
ls -lh          # List with human-readable sizes

# Navigate directories
cd directory    # Change directory
cd ~            # Go to home directory
cd -            # Go to previous directory
pwd             # Print working directory

# File and directory operations
cp file1 file2                  # Copy file
cp -r dir1 dir2                 # Copy directory recursively
mv file1 file2                  # Move or rename
rm file                         # Delete file
rm -rf directory                # Delete directory recursively
mkdir directory                 # Create new directory
rmdir directory                 # Remove empty directory

# File viewing
cat file                        # Display file contents
more file                       # Display file with pagination
less file                       # Better pagination viewer
head file                       # Show first 10 lines
head -n 20 file                 # Show first 20 lines
tail file                       # Show last 10 lines
tail -f file                    # Follow file as it grows

User and Permission Management

# User information
whoami                          # Current user
id                              # User ID and groups
sudo command                    # Execute as root user
sudo -l                         # List sudo privileges

# File permissions
chmod 755 file                  # Set permissions (rwxr-xr-x)
chmod +x file                   # Make executable
chown user:group file           # Change owner and group
chown -R user:group directory/  # Recursive ownership change

System Information

# System information
uname -a                        # Kernel information
cat /etc/os-release             # OS version
lsb_release -a                  # Linux distribution info
hostnamectl                     # System hostname and OS

# Hardware information
neofetch                        # Pretty system info
lscpu                           # CPU information
free -h                         # Memory usage
df -h                           # Disk space
du -sh directory                # Directory size

Process Management

# Process listing and management
ps aux                          # List all running processes
ps aux | grep process_name      # Find specific process
kill PID                        # Kill process by ID
killall process_name            # Kill all instances
top                             # Interactive process monitor
htop                            # Enhanced process monitor

Network Commands

# Network testing
ping host                       # Test connectivity
traceroute host                 # Show route to host
netstat -an                     # Network statistics
ss -an                          # Socket statistics
ifconfig                        # Network interface info
ip addr                         # IP addresses (newer)

Text Processing

# Search and filter
grep pattern file               # Search in file
grep -r pattern directory/      # Recursive search
grep -i pattern file            # Case-insensitive search
grep -v pattern file            # Invert match
awk '{print $1}' file           # Extract columns
sed 's/old/new/g' file          # String substitution
wc -l file                      # Count lines

# Sorting and manipulation
sort file                       # Sort file contents
uniq file                       # Remove duplicate lines
cut -d: -f1 file                # Extract delimited fields

Compression and Archives

# Compression
tar -czf archive.tar.gz files/  # Create gzip tar archive
tar -xzf archive.tar.gz         # Extract gzip tar
zip -r archive.zip files/       # Create zip archive
unzip archive.zip               # Extract zip

# Compression utilities
gzip file                       # Compress to .gz
gunzip file.gz                  # Decompress
bzip2 file                      # Compress to .bz2

Useful Tricks

# Command history
history                         # Show command history
!!                              # Repeat last command
!n                              # Repeat command number n
Ctrl+R                          # Search command history

# Piping and redirection
command > file                  # Redirect output to file
command >> file                 # Append output to file
command < file                  # Read from file
command1 | command2             # Pipe output to another command

This cheat sheet covers the most commonly used commands. Most commands have additional options available via man command.