Linux Essential Commands
Linux commands cheat sheet for file operations, permissions, process management, networking, and shell scripting with syntax examples.
File & Directory Operations
List directory contents with various formatting options
# List files
ls
ls -l # Long format with details
ls -la # Include hidden files
ls -lh # Human-readable sizes (KB, MB, GB)
ls -lt # Sort by modification time
ls -lS # Sort by file size
ls -R # Recursive - show subdirectoriesNavigate between directories in the filesystem
# Change directory
cd /path/to/directory
cd ~ # Home directory
cd .. # Parent directory
cd - # Previous directory
cd # Home directory (shortcut)Copy files and directories with options for recursive and interactive copying
# Copy files
cp source.txt destination.txt
cp file1.txt file2.txt /target/dir/
# Copy directories (recursive)
cp -r source_dir/ dest_dir/
# Copy with confirmation
cp -i source.txt dest.txt
# Preserve attributes
cp -p file.txt backup.txtMove or rename files and directories
# Move files
mv source.txt /path/to/destination/
# Rename files
mv oldname.txt newname.txt
# Move multiple files
mv file1.txt file2.txt /target/dir/
# Move with confirmation
mv -i source.txt dest.txtDelete files and directories (use with caution)
# Remove files
rm file.txt
rm file1.txt file2.txt
# Remove directories
rm -r directory/
rm -rf directory/ # Force remove (no confirmation)
# Remove with confirmation
rm -i file.txt
# Remove empty directory
rmdir empty_dir/Create new directories with support for nested paths
# Create directory
mkdir new_directory
# Create nested directories
mkdir -p path/to/nested/directory
# Create multiple directories
mkdir dir1 dir2 dir3
# Create with permissions
mkdir -m 755 directoryCreate empty files or update timestamps
# Create empty file
touch newfile.txt
# Create multiple files
touch file1.txt file2.txt file3.txt
# Update timestamp only
touch existing_file.txtFile Viewing & Editing
Display file contents, concatenate files, or create new files
# Display file contents
cat file.txt
# Display multiple files
cat file1.txt file2.txt
# Display with line numbers
cat -n file.txt
# Create file with content
cat > newfile.txt
(type content, then Ctrl+D to save)View files one screen at a time with navigation
# View file with pagination
less file.txt
# Navigation:
# Space - Next page
# b - Previous page
# / - Search forward
# ? - Search backward
# n - Next search result
# q - QuitView the beginning or end of files
# View first 10 lines
head file.txt
# View first N lines
head -n 20 file.txt
# View last 10 lines
tail file.txt
# View last N lines
tail -n 30 file.txt
# Follow file updates (logs)
tail -f /var/log/syslogUser-friendly terminal text editor for beginners
# Edit file
nano file.txt
# Key shortcuts:
# Ctrl+O - Save file
# Ctrl+X - Exit
# Ctrl+K - Cut line
# Ctrl+U - Paste line
# Ctrl+W - Search
# Ctrl+G - HelpPowerful modal text editor with steep learning curve
# Edit file
vim file.txt
# Basic commands:
# i - Enter insert mode
# Esc - Exit insert mode
# :w - Save
# :q - Quit
# :wq - Save and quit
# :q! - Quit without saving
# dd - Delete line
# yy - Copy line
# p - PasteFile Permissions & Ownership
Modify file and directory access permissions
# Numeric mode
chmod 755 file.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 600 private.key # rw-------
# Symbolic mode
chmod u+x script.sh # Add execute for user
chmod g-w file.txt # Remove write for group
chmod o+r file.txt # Add read for others
chmod a+x file.sh # Add execute for all
# Recursive
chmod -R 755 directory/Change file owner and group
# Change owner
sudo chown username file.txt
# Change owner and group
sudo chown username:groupname file.txt
# Change only group
sudo chown :groupname file.txt
# Recursive
sudo chown -R username:groupname directory/Change group ownership of files
# Change group
chgrp groupname file.txt
# Recursive
chgrp -R groupname directory/
# View current groups
groups
idProcess Management
Display currently running processes
# View all processes
ps aux
# View user processes
ps -u username
# View process tree
ps auxf
# Find specific process
ps aux | grep process_nameReal-time view of system processes and resource usage
# Launch top
top
# Common keys:
# q - Quit
# k - Kill process (prompts for PID)
# M - Sort by memory usage
# P - Sort by CPU usage
# h - HelpSend signals to processes to stop them
# Kill by PID
kill 1234
# Force kill
kill -9 1234
# Kill by name
killall process_name
pkill process_name
# List signals
kill -lRun processes in background and manage jobs
# Run in background
command &
# Background current process
Ctrl+Z
bg
# List jobs
jobs
# Bring to foreground
fg %1
# Keep running after logout
nohup command &System Information
Display system information and kernel details
# System info
uname -a # All info
uname -r # Kernel version
uname -m # Architecture
# OS info
cat /etc/os-release
lsb_release -a
# Hostname
hostnameCheck disk space and directory sizes
# Disk space by filesystem
df -h
# Directory sizes
du -h directory/
du -sh directory/ # Summary only
du -h --max-depth=1 # One level deep
# Largest files/dirs
du -ah | sort -rh | head -20View RAM and swap memory usage
# Memory info
free -h
# Detailed memory info
cat /proc/meminfo
# Memory by process
top
ps aux --sort=-%mem | headCheck how long system has been running and load average
# Uptime and load
uptime
# Who is logged in
who
w
# Current user
whoami
idNetworking
Check network connectivity to a host
# Ping host
ping google.com
# Ping with count
ping -c 4 google.com
# Ping with interval
ping -i 2 google.comMake HTTP requests and download files from command line
# GET request
curl https://api.example.com
# Save to file
curl -o output.html https://example.com
curl -O https://example.com/file.zip
# Follow redirects
curl -L https://example.com
# POST data
curl -X POST -d "key=value" https://api.example.comDownload files from the web with resume capability
# Download file
wget https://example.com/file.zip
# Continue interrupted download
wget -c https://example.com/large-file.iso
# Download quietly
wget -q https://example.com/file.zip
# Mirror website
wget -m https://example.comSecurely connect to remote servers
# Connect to server
ssh username@hostname
# Connect with port
ssh -p 2222 username@hostname
# Execute command
ssh username@hostname 'ls -la'
# SSH with key
ssh -i ~/.ssh/key.pem username@hostnameCopy files between local and remote machines via SSH
# Copy to remote
scp file.txt username@hostname:/path/to/destination/
# Copy from remote
scp username@hostname:/path/to/file.txt ./
# Copy directory
scp -r directory/ username@hostname:/path/
# With custom port
scp -P 2222 file.txt username@hostname:/path/View network interfaces, connections, and routing
# IP address
ip addr
ip a
# Network statistics
netstat -tuln # Listening ports
netstat -ant # All connections
# Modern alternative
ss -tuln # Listening ports
ss -ant # All connectionsSearch & Find
Search for files and directories by name, type, size, and more
# Find by name
find /path -name "filename.txt"
find . -name "*.js"
# Find by type
find . -type f # Files only
find . -type d # Directories only
# Find by size
find . -size +100M # Larger than 100MB
find . -size -1M # Smaller than 1MB
# Find and execute
find . -name "*.log" -delete
find . -type f -exec chmod 644 {} \;Search for patterns in file contents
# Search in file
grep "pattern" file.txt
# Case-insensitive
grep -i "pattern" file.txt
# Recursive search
grep -r "pattern" directory/
# Show line numbers
grep -n "pattern" file.txt
# Count matches
grep -c "pattern" file.txt
# Invert match (exclude)
grep -v "pattern" file.txtQuickly find files by name using pre-built database
# Search for file
locate filename.txt
# Case-insensitive
locate -i filename.txt
# Update database
sudo updatedb
# Limit results
locate -n 10 filename.txtLocate executable commands and their paths
# Find command location
which python
which python3
# Find command, source, and man pages
whereis python
# Show all matches
which -a pythonCompression & Archives
Create and extract tar archives (tape archives)
# Create archive
tar -cvf archive.tar files/
# Create compressed archive
tar -czvf archive.tar.gz files/ # gzip
tar -cjvf archive.tar.bz2 files/ # bzip2
# Extract archive
tar -xvf archive.tar
tar -xzvf archive.tar.gz
# List contents
tar -tvf archive.tar
# Extract to directory
tar -xzvf archive.tar.gz -C /destination/Compress and decompress files with gzip
# Compress file
gzip file.txt # Creates file.txt.gz
# Decompress
gzip -d file.txt.gz
gunzip file.txt.gz
# Keep original
gzip -k file.txt
# Compress multiple files
gzip file1.txt file2.txtCreate and extract zip archives (cross-platform)
# Create zip
zip archive.zip file1.txt file2.txt
# Zip directory
zip -r archive.zip directory/
# Extract zip
unzip archive.zip
# Extract to directory
unzip archive.zip -d /destination/
# List contents
unzip -l archive.zip