Skip to content

Linux Cheatsheet

A complete Linux command-line cheatsheet from beginner to advanced — navigation, files, permissions, processes, packages, networking, systemd, text processing, SSH, scripting, and troubleshooting.

A practical, progressive reference for the Linux command line — from first login to advanced administration. Commands work on most distributions (Debian/Ubuntu, Fedora/RHEL, Arch) unless noted.


Terminal window
pwd # print working directory
whoami # current user
id # user and group IDs
hostname # machine name
uname -a # kernel and system info
cat /etc/os-release # distribution details
date # current date/time
cal # calendar
uptime # how long the system has been running
history # command history
clear # clear terminal

Terminal window
man ls # manual page
man -k keyword # search man pages
ls --help # short help
info coreutils # GNU info system

Terminal window
cd /path/to/dir # absolute
cd dirname # relative
cd .. # parent
cd ~ # home
cd - # previous directory
cd # home (shortcut)
ls # list
ls -l # long format
ls -la # including hidden
ls -lh # human-readable sizes
ls -lt # sort by time
ls -lS # sort by size
ls -R # recursive
tree # tree view (if installed)

  • . = current directory
  • .. = parent directory
  • ~ = home directory
  • / = root of the filesystem

Terminal window
# Create
touch file.txt
mkdir dirname
mkdir -p path/to/nested
# Copy
cp file.txt copy.txt
cp -r dir/ newdir/ # recursive
cp -a src/ dest/ # archive (preserve attributes)
# Move / Rename
mv old.txt new.txt
mv file.txt /path/to/dir/
# Remove
rm file.txt
rm -r dirname # recursive
rm -rf dirname # force (careful!)
rmdir dirname # only if empty
# View
cat file.txt
less file.txt # navigable pager (q to quit)
head -n 20 file.txt
tail -n 20 file.txt
tail -f logfile # follow (live)

Terminal window
* # any string
? # single character
[abc] # one of a, b, c
[a-z] # range
{a,b,c} # brace expansion

Terminal window
ls -l # view permissions
# -rwxr-xr-x owner group size date name
chmod 755 script.sh # rwxr-xr-x
chmod u+x script.sh # add execute for owner
chmod go-w file.txt # remove write for group & others
chmod -R 755 dirname # recursive
chown user file.txt
chown user:group file.txt
chown -R user:group dir/
chgrp group file.txt

Value Permission
4 read (r)
2 write (w)
1 execute (x)

Examples: 7 = rwx, 6 = rw-, 5 = r-x, 4 = r–

Terminal window
chmod u+s file # setuid
chmod g+s dir # setgid
chmod +t dir # sticky bit (e.g. /tmp)

Terminal window
find /path -name "*.log"
find . -type f -name "*.js"
find . -type d -name "node_modules"
find /var -size +100M
find . -mtime -7 # modified in last 7 days
find . -name "*.tmp" -delete
locate filename # fast (uses database)
updatedb # refresh locate database (root)
which python3 # location of executable
whereis ls # binary, source, man
type ls # show how command is resolved

Terminal window
grep "pattern" file.txt
grep -r "pattern" dir/ # recursive
grep -i "pattern" file # case-insensitive
grep -n "pattern" file # show line numbers
grep -v "pattern" file # invert match
grep -E "pat1|pat2" file # extended regex
sed 's/old/new/' file.txt
sed -i 's/old/new/g' file.txt # in-place
sed -n '10,20p' file.txt # print lines 10–20
awk '{print $1}' file.txt
awk -F: '{print $1}' /etc/passwd
awk 'NR>1 {sum+=$3} END {print sum}' data.csv
cut -d: -f1 /etc/passwd
sort file.txt
sort -n numbers.txt # numeric
uniq file.txt
wc -l file.txt # line count
tr 'a-z' 'A-Z' < file.txt

Terminal window
cmd1 | cmd2 # pipe
cmd > file # overwrite
cmd >> file # append
cmd 2> error.log # stderr
cmd > out.log 2>&1 # both
cmd < input.txt # stdin from file

Terminal window
ps aux # all processes
ps -u username
top # interactive
htop # nicer (if installed)
pgrep nginx
pkill -f "python script.py"
kill PID
kill -9 PID # force
killall processname
jobs # background jobs
bg # resume in background
fg # bring to foreground
Ctrl+Z # suspend
Ctrl+C # terminate
nohup cmd & # survive logout

Terminal window
free -h # memory
df -h # disk space
du -sh * # directory sizes
du -h --max-depth=1
vmstat 1
iostat

Terminal window
sudo apt update
sudo apt upgrade
sudo apt install package
sudo apt remove package
sudo apt purge package # remove config too
sudo apt search keyword
sudo apt show package
apt list --installed

Terminal window
sudo dnf update
sudo dnf install package
sudo dnf remove package
sudo dnf search keyword

Terminal window
sudo pacman -Syu # full upgrade
sudo pacman -S package
sudo pacman -R package
sudo pacman -Ss keyword

Terminal window
ip a # addresses (modern)
ip r # routes
ip link # interfaces
# Older but still common
ifconfig
route -n
ss -tlnp # listening ports
ss -s # summary
lsof -i :80
ping -c 4 example.com
traceroute example.com
curl https://example.com
curl -I https://example.com # headers only
wget https://example.com/file
dig example.com
nslookup example.com
host example.com
# Firewall (ufw – Ubuntu)
sudo ufw status
sudo ufw allow 22
sudo ufw enable

Terminal window
lsblk # block devices
fdisk -l # partition tables (root)
blkid # UUIDs
mount /dev/sdb1 /mnt
umount /mnt
df -h
du -sh /var/log
# Create filesystem
sudo mkfs.ext4 /dev/sdb1
# Swap
sudo swapon --show
free -h

Terminal window
sudo adduser username
sudo passwd username
sudo deluser username
sudo usermod -aG sudo username # add to group
groups username
id username
sudo su - # become root
sudo -i # root shell
sudo -u otheruser cmd

Terminal window
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl enable nginx # start on boot
systemctl disable nginx
systemctl is-enabled nginx
systemctl list-units --type=service
journalctl -u nginx
journalctl -u nginx -f # follow
journalctl --since "1 hour ago"
journalctl -p err # errors only

Terminal window
tar -czvf archive.tar.gz dir/
tar -xzvf archive.tar.gz
tar -tjf archive.tar.bz2 # list contents
zip -r archive.zip dir/
unzip archive.zip
gzip file.txt # creates file.txt.gz
gunzip file.txt.gz

Terminal window
ssh user@host
ssh -p 2222 user@host
ssh -i ~/.ssh/key.pem user@host
scp file.txt user@host:/path/
scp -r dir/ user@host:/path/
rsync -avz src/ user@host:/path/
rsync -avz --delete src/ dest/ # mirror
# Generate key pair
ssh-keygen -t ed25519 -C "email@example.com"
ssh-copy-id user@host

Terminal window
crontab -e # edit current user crontab
crontab -l # list
# Format: min hour day month weekday command
# 0 2 * * * /path/to/backup.sh → every day at 02:00
# */5 * * * * cmd → every 5 minutes
# 0 0 * * 0 cmd → every Sunday at midnight
systemctl list-timers # systemd timers (modern alternative)

Terminal window
echo $PATH
export VAR=value
echo $VAR
env # all environment variables
which bash
echo $SHELL
# .bashrc / .zshrc aliases
alias ll='ls -la'
alias gs='git status'
source ~/.bashrc

Key Action
Ctrl+A Start of line
Ctrl+E End of line
Ctrl+U Clear line before cursor
Ctrl+K Clear line after cursor
Ctrl+R Search history
Ctrl+L Clear screen
!! Repeat last command
!$ Last argument of previous

#!/usr/bin/env bash
set -euo pipefail # strict mode
NAME="world"
echo "Hello, $NAME"
if [ -f file.txt ]; then
echo "exists"
elif [ -d dir ]; then
echo "directory"
else
echo "missing"
fi
for i in 1 2 3; do
echo "$i"
done
while read -r line; do
echo "$line"
done < file.txt
# Functions
greet() {
echo "Hello, $1"
}
greet "Linux"

Terminal window
dmesg | tail
journalctl -xe
tail -f /var/log/syslog
tail -f /var/log/auth.log
# Disk full?
df -h
du -sh /* 2>/dev/null | sort -h
# Who is using space / inodes
du -x -d 1 / | sort -h
df -i
# Network issues
ping -c 3 8.8.8.8
ip a
ss -tlnp

Terminal window
# Run command as another user
sudo -u www-data cmd
# Watch a command
watch -n 1 'df -h'
# Parallel SSH / tools (if installed)
parallel
# Strace a process
strace -p PID
strace -e openat ls
# Kernel modules
lsmod
modprobe module
uname -r
# Limits
ulimit -a
ulimit -n 4096
# Time a command
time ./script.sh

  • Prefer rm -i or trash-cli over raw rm -rf
  • Double-check paths before destructive commands
  • Use sudo only when needed
  • Keep systems updated
  • Prefer SSH keys over passwords
  • Log important administrative actions

Terminal window
# Navigation
pwd cd ls -la tree
# Files
cp -a mv rm -r mkdir -p touch find locate
# Permissions
chmod 755 chown user:group ls -l
# Processes
ps aux top htop kill killall jobs bg fg
# Packages (Debian)
apt update && apt upgrade apt install apt remove
# Network
ip a ss -tlnp curl ping dig
# Disk
df -h du -sh lsblk
# Services
systemctl status|start|stop|enable journalctl -u
# Archives
tar -czvf tar -xzvf zip unzip
# Remote
ssh scp rsync -avz


Master the fundamentals (navigation, files, pipes, permissions) before diving into advanced topics. Practice on a disposable VM or container. When in doubt, read the man page.

Frequently asked questions

What is the difference between absolute and relative paths?

An absolute path starts from the root (/) and uniquely identifies a location. A relative path is resolved from the current working directory.

What do the permission bits rwx mean?

r = read, w = write, x = execute. They appear in three sets for owner, group, and others (e.g. 755 = rwxr-xr-x).

How do I find which process is using a port?

Use ss -tlnp | grep :PORT or lsof -i :PORT. On older systems netstat -tlnp also works.

What is the difference between soft and hard links?

A hard link points to the same inode (same data). A soft (symbolic) link is a separate file that stores a path to the target.

How should I manage services on modern Linux?

Use systemctl to start, stop, enable, disable, and inspect services. journalctl is used to view their logs.

You're offline Check your connection.