Linux Commands Cheatsheet

Essential commands for Linux system administration

Quick Reference

Linux is a family of open-source Unix-like operating systems based on the Linux kernel.

1 File Operations

ls [options] [directory]
List directory contents
ls # List files
ls -l # Long listing
ls -a # Include hidden files
ls -lh # Human-readable sizes
cd [directory]
Change directory
cd /path/to/dir # Absolute path
cd .. # Parent directory
cd ~ # Home directory
cd - # Previous directory
pwd
Print working directory
pwd # Show current directory
cp [options] source destination
Copy files and directories
cp file1 file2 # Copy file
cp -r dir1 dir2 # Recursive copy
cp -v file* dest/ # Verbose copy
mv [options] source destination
Move or rename files and directories
mv oldname newname # Rename
mv file /target/dir/ # Move file
mv -i file* dest/ # Interactive (prompt before overwrite)
rm [options] file
Remove files or directories
rm file.txt # Remove file
rm -r directory # Recursive remove
rm -f file # Force remove
rm -i file* # Interactive remove
Warning: Be extremely careful with rm -rf as it can permanently delete files without confirmation.
mkdir [options] directory
Create directories
mkdir newdir # Create directory
mkdir -p path/to/newdir # Create parent directories as needed
touch [options] file
Create empty file or update timestamps
touch file.txt # Create empty file
touch -t 202301010000 file.txt # Set specific timestamp
cat [options] file
Concatenate and display file contents
cat file.txt # Display file
cat file1 file2 > combined # Combine files
cat -n file.txt # Show line numbers
less/more [options] file
View file contents page by page
less largefile.log # View with navigation
more largefile.log # Basic pager
head/tail [options] file
Display beginning/end of file
head -n 10 file.txt # First 10 lines
tail -n 20 file.txt # Last 20 lines
tail -f logfile # Follow log in real-time
find [path] [expression]
Search for files in directory hierarchy
find /home -name "*.txt" # Find .txt files
find . -type f -mtime -7 # Files modified in last 7 days
find / -size +100M # Files larger than 100MB

2 System Information

uname [options]
Print system information
uname -a # All system info
uname -s # Kernel name
uname -r # Kernel release
hostname
Show or set system hostname
hostname # Show hostname
hostname newhost # Set hostname (temporary)
df [options]
Report file system disk space usage
df -h # Human-readable format
df -i # Show inode usage
du [options] [file]
Estimate file space usage
du -sh /path # Summary for directory
du -ah /path # All files with sizes
du --max-depth=1 # Limit depth
free [options]
Display memory usage
free -h # Human-readable format
free -m # Show in megabytes
uptime
Show how long system has been running
uptime # Shows uptime, users, load average
date [options]
Display or set date and time
date # Current date/time
date +"%Y-%m-%d" # Custom format
date --set="2023-01-01 12:00:00" # Set system date/time
cal [options]
Display calendar
cal # Current month
cal -y # Whole year
cal 9 1752 # Historic month (famous 11-day skip)

3 Process Management

ps [options]
Report process status
ps aux # Show all processes
ps -ef # Full format listing
ps -u username # User's processes
top/htop
Display and manage processes interactively
top # Basic process monitor
htop # Enhanced interactive process viewer
kill [options] PID
Terminate processes
kill 1234 # Terminate process
kill -9 1234 # Force kill
kill -l # List signals
pkill [options] pattern
Kill processes by name
pkill firefox # Kill by process name
pkill -u username # Kill user's processes
killall [options] process_name
Kill processes by name
killall nginx # Kill all nginx processes
bg/fg [job_spec]
Control background/foreground jobs
bg %1 # Resume job 1 in background
fg %2 # Bring job 2 to foreground
jobs [options]
List active jobs
jobs # List jobs
jobs -l # List with PIDs

4 Networking

ip [options]
Show/manipulate routing, devices, policy routing and tunnels
ip addr show # Show network interfaces
ip route show # Show routing table
ip link set eth0 up # Bring interface up
ifconfig [interface]
Configure network interfaces (deprecated, use ip)
ifconfig # Show all interfaces
ifconfig eth0 up # Bring interface up
ping [options] host
Test network connectivity
ping google.com # Basic ping
ping -c 4 8.8.8.8 # Ping 4 times
traceroute [options] host
Print route packets take to network host
traceroute google.com # Trace route
traceroute -n 8.8.8.8 # Don't resolve names
netstat [options]
Print network connections, routing tables, interface statistics
netstat -tulnp # Listening ports
netstat -r # Routing table
ss [options]
Another utility to investigate sockets (modern netstat replacement)
ss -tulnp # Listening ports
ss -s # Summary statistics
dig [options] domain
DNS lookup utility
dig google.com # Basic DNS lookup
dig +short google.com # Short output
dig -x 8.8.8.8 # Reverse lookup
wget [options] URL
Non-interactive network downloader
wget https://example.com/file.zip # Download file
wget -c https://example.com/bigfile.iso # Continue interrupted download
curl [options] URL
Transfer data from or to a server
curl https://example.com # Fetch URL
curl -o file.txt https://example.com # Save to file
curl -X POST -d "data=value" https://example.com/api # POST request

5 User Management

useradd [options] username
Create new user account
useradd -m johndoe # Create user with home directory
useradd -g developers -s /bin/bash johndoe # Specify group and shell
usermod [options] username
Modify user account
usermod -aG sudo johndoe # Add to sudo group
usermod -s /bin/zsh johndoe # Change shell
userdel [options] username
Delete user account
userdel johndoe # Delete user
userdel -r johndoe # Delete user and home directory
passwd [options] [username]
Change user password
passwd # Change own password
passwd johndoe # Change another user's password (root only)
groupadd [options] groupname
Create new group
groupadd developers # Create group
groupadd -g 1001 developers # Create with specific GID
groups [username]
Show group memberships
groups # Current user's groups
groups johndoe # Another user's groups

6 Permissions

chmod [options] mode file
Change file mode bits
chmod 755 script.sh # Numeric mode
chmod u+x file.txt # Add execute for owner
chmod -R 644 /path/to/dir # Recursive change
chown [options] owner[:group] file
Change file owner and group
chown johndoe file.txt # Change owner
chown johndoe:developers file.txt # Change owner and group
chown -R www-data:www-data /var/www # Recursive change
chgrp [options] group file
Change group ownership
chgrp developers file.txt # Change group
umask [value]
Set file mode creation mask
umask # Show current umask
umask 022 # Set umask
getfacl/setfacl [options] file
Get/set file access control lists
getfacl file.txt # View ACLs
setfacl -m u:johndoe:rw file.txt # Add ACL for user

7 Compression

tar [options] file
Tape archiver utility
tar -cvf archive.tar files/ # Create archive
tar -xvf archive.tar # Extract archive
tar -czvf archive.tar.gz files/ # Create gzipped archive
tar -xzvf archive.tar.gz # Extract gzipped archive
gzip/gunzip [options] file
Compress/decompress files
gzip file.txt # Compress file
gunzip file.txt.gz # Decompress file
gzip -9 file.txt # Maximum compression
zip/unzip [options] file
Package and compress files
zip archive.zip files/ # Create zip archive
unzip archive.zip # Extract zip archive
bzip2/bunzip2 [options] file
Block-sorting file compressor
bzip2 file.txt # Compress file
bunzip2 file.txt.bz2 # Decompress file
xz/unxz [options] file
Compress/decompress files with LZMA algorithm
xz file.txt # Compress file
unxz file.txt.xz # Decompress file
grep [options] pattern [file]
Print lines matching a pattern
grep "error" logfile.txt # Search for pattern
grep -r "function" /path/to/code # Recursive search
grep -i "warning" file.txt # Case-insensitive search
find [path] [expression]
Search for files in directory hierarchy
find /home -name "*.txt" # Find .txt files
find . -type f -mtime -7 # Files modified in last 7 days
find / -size +100M # Files larger than 100MB
locate [options] pattern
Find files by name (uses database)
locate filename.txt # Find file
locate -i "*.jpg" # Case-insensitive search
updatedb # Update locate database (as root)
which [command]
Locate a command
which python # Show path to python
whereis [options] command
Locate binary, source, and manual page files
whereis python # Show all python files