Skip to content
$ lynuxpro
← Linux

CHEAT SHEET · 26 LESSONS IN ONE PAGE

Linux cheat sheet

The Linux commands DevOps engineers use every day, grouped by task, with the options that matter.

Saves a clean, printable copy (Ctrl+P or ⌘P also works).

The 25 commands you will use most

CommandWhat it does
ls -lahlist files with sizes, permissions and hidden files
cd DIR / cd - / cd ~change folder / go back / go home
pwdprint the current folder
cat FILE / less FILEprint a file / page through it (q quits, /word searches)
tail -f FILEfollow a log as it grows
grep -rn "text" DIRsearch text in files, with line numbers
find DIR -name "*.log"find files by name
cp -r SRC DST / mv SRC DST / rm -r DIRcopy / move or rename / delete
mkdir -p a/b/ccreate folders, including parents
chmod +x script.sh / chown user:group FILEchange permissions / owner
sudo COMMANDrun as root
ps aux | grep NAMEfind a process
top / htoplive CPU and memory per process
kill PID / kill -9 PIDstop a process / force it
df -h / du -sh *free disk space / size of each item here
free -hmemory use
systemctl status NAMEis a service running?
journalctl -u NAME -ffollow a service's logs
ss -tulpnwhich ports are listening, and by which process
ip aIP addresses of each interface
curl -I URLHTTP status and headers
ssh user@hostlog in to a remote server
tar -czf out.tgz DIR / tar -xzf in.tgzcreate / extract a compressed archive
history | grep WORDfind a command you ran before
man COMMAND / COMMAND --helpread the manual
CommandWhat it does
ls -llong listing: permissions, owner, size, date
ls -lainclude hidden files (names starting with .)
ls -lt / ls -lSsort by time / by size
ls -Rlist folders recursively
tree -L 2folder tree, two levels deep
cd / / cd .. / cd -root / parent / previous folder
touch FILEcreate an empty file or update its time
mkdir -p DIRcreate a folder and missing parents
cp FILE DIR/copy a file into a folder
cp -a SRC DSTcopy keeping permissions, owners and times
mv OLD NEWrename or move
rm FILE / rm -r DIR / rm -rf DIRdelete a file / a folder / without asking (careful)
rmdir DIRdelete an empty folder
ln -s TARGET LINKcreate a symbolic link
stat FILEsize, permissions, times, inode
file FILEwhat kind of file it is
realpath FILEfull absolute path
basename PATH / dirname PATHlast part / folder part of a path

Viewing files

CommandWhat it does
cat FILEprint the whole file
cat -n FILEwith line numbers
less FILEpage through; G end, g start, /text search, n next
head -n 20 FILEfirst 20 lines
tail -n 50 FILElast 50 lines
tail -f FILEfollow new lines (logs)
tail -F FILEfollow even if the file is rotated
wc -l FILEcount lines (-w words, -c bytes)
diff -u A Bcompare two files
nl FILEnumber the lines
tac FILEprint in reverse order

Searching

CommandWhat it does
grep "text" FILElines containing text
grep -i / -v / -c / -n / -wignore case / invert / count / line numbers / whole word
grep -rn "text" DIRsearch recursively
grep -E "err|warn" FILEextended regex, either word
grep -A 3 -B 3 "text" FILE3 lines after and before each match
grep -l "text" *.confonly names of files that match
find DIR -name "*.log"by name (-iname ignores case)
find DIR -type f -size +100Mfiles over 100 MB
find DIR -mtime +7modified more than 7 days ago
find DIR -name "*.tmp" -deletefind and delete
find DIR -type f -exec chmod 644 {} \;run a command on each match
which CMD / type CMDwhere a command comes from
locate NAMEfast search in a file index (updatedb refreshes it)

Permissions and ownership

CommandWhat it does
ls -lshows -rwxr-xr--: type, then user, group, others
chmod 755 FILErwx for owner, r-x for group and others
chmod 644 FILErw- for owner, r-- for the rest
chmod 600 key.pemprivate file: only the owner can read and write
chmod u+x FILE / chmod g-w FILE / chmod o-r FILEadd or remove one permission
chmod -R 750 DIRrecursively
chown user FILE / chown user:group FILEchange owner / owner and group
chown -R app:app /srv/apprecursively
umaskdefault permissions for new files
sudo -u USER CMDrun as another user
sudo -iroot shell

Numbers: read 4, write 2, execute 1. 7 = rwx, 6 = rw-, 5 = r-x, 4 = r--.

Users and groups

CommandWhat it does
whoami / idcurrent user / user, groups and IDs
sudo useradd -m -s /bin/bash NAMEcreate a user with a home folder
sudo passwd NAMEset a password
sudo usermod -aG docker NAMEadd a user to a group (log in again to apply)
sudo userdel -r NAMEdelete a user and home folder
groups NAMEa user's groups
getent passwd NAMEa user's account line
su - NAMEswitch user
last / who / wrecent logins / who is logged in / and what they run
sudo visudosafely edit sudo rules

Processes

CommandWhat it does
ps auxevery process with CPU, memory and command
ps -ef --forestprocess tree
pgrep -a NAMEfind processes by name
toplive view; P sort by CPU, M by memory, k kill, q quit
htopfriendlier top
kill PIDask a process to stop (SIGTERM)
kill -9 PIDforce stop (SIGKILL), last resort
pkill NAME / killall NAMEstop by name
CMD & / jobs / fg / bgrun in background / list / foreground / background
Ctrl+Zsuspend the running command
nohup CMD &keep running after logout
nice -n 10 CMD / renice 5 -p PIDlower priority
lsof -p PIDfiles a process has open
lsof -i :8080which process uses port 8080
strace -p PIDsystem calls of a running process

System information and monitoring

CommandWhat it does
uname -akernel and architecture
cat /etc/os-releasedistribution and version
hostnamectlhostname, OS, kernel
uptimerunning time and load average (1, 5, 15 min)
nproc / lscpunumber of CPUs / CPU details
free -hmemory; "available" is what apps can use
df -hdisk space per filesystem
df -iinodes (full inodes also stop writes)
du -sh DIRsize of a folder
du -h --max-depth=1 / | sort -hwhat fills the disk
vmstat 1CPU, memory, swap, I/O every second
iostat -xz 1disk utilisation and latency
mpstat -P ALL 1CPU per core
sar -n DEV 1network throughput
dmesg -T | tailkernel messages (OOM kills, disk errors)
lsblkdisks and partitions
lsmodloaded kernel modules

Services and logs (systemd)

CommandWhat it does
systemctl status NAMEstate, recent logs, PID
sudo systemctl start / stop / restart / reload NAMEcontrol a service
sudo systemctl enable --now NAMEstart now and at boot
sudo systemctl disable NAMEdo not start at boot
systemctl is-active NAME / is-enabled NAMEquick checks for scripts
systemctl list-units --type=service --state=failedfailed services
systemctl list-timersscheduled timers
systemctl cat NAMEshow the unit file
sudo systemctl daemon-reloadafter editing unit files
journalctl -u NAMEa service's logs
journalctl -u NAME -ffollow them
journalctl -u NAME --since "1 hour ago"recent only
journalctl -p err -berrors since boot
journalctl --disk-usagejournal size
/var/log/classic log files (syslog, auth.log, nginx/)

Networking

CommandWhat it does
ip a / ip -br aaddresses (brief)
ip rrouting table, default gateway
ss -tulpnlistening TCP/UDP ports with process
ss -tan state establishedopen connections
ping -c 4 HOSTis it reachable?
traceroute HOST / mtr HOSTpath and where it slows down
dig NAME / dig +short NAMEDNS lookup
nslookup NAMEDNS lookup (simple)
cat /etc/resolv.confDNS servers in use
curl -I URLheaders and status only
curl -sS -o /dev/null -w "%{http_code}" URLjust the status code
curl -v URLfull request and response, TLS details
wget URLdownload a file
nc -zv HOST 5432is a TCP port open?
ssh -i key.pem user@hostlog in with a key
ssh -L 8080:localhost:80 user@hostforward a local port through SSH
scp FILE user@host:/path/copy over SSH
rsync -avz SRC/ user@host:/DST/sync folders efficiently
sudo ufw status / sudo iptables -L -nfirewall rules
cat /etc/hostslocal name overrides

Archives and compression

CommandWhat it does
tar -czf out.tar.gz DIRcreate a gzip archive
tar -xzf in.tar.gz -C /destextract into a folder
tar -tzf in.tar.gzlist contents
tar -cJf out.tar.xz DIRsmaller, slower (xz)
gzip FILE / gunzip FILE.gzcompress / decompress one file
zip -r out.zip DIR / unzip in.zipzip archives
zcat FILE.gz | lessread a compressed file
sha256sum FILE / sha256sum -c FILE.sha256checksum / verify

Packages

Ubuntu / DebianRHEL / Fedora / Amazon LinuxWhat it does
sudo apt updatesudo dnf check-updaterefresh package lists
sudo apt install -y PKGsudo dnf install -y PKGinstall
sudo apt upgradesudo dnf upgradeupgrade everything
sudo apt remove PKGsudo dnf remove PKGremove
apt search WORDdnf search WORDsearch
apt show PKGdnf info PKGdetails
dpkg -lrpm -qalist installed
dpkg -L PKGrpm -ql PKGfiles a package installed

Disks and filesystems

CommandWhat it does
lsblk -fdisks, partitions, filesystems, mount points
sudo fdisk -lpartition tables
sudo mkfs.ext4 /dev/xvdfformat a disk (destroys data)
sudo mount /dev/xvdf /datamount it
sudo umount /dataunmount
cat /etc/fstabmounts at boot (use UUIDs from blkid)
sudo growpart /dev/xvda 1 && sudo resize2fs /dev/xvda1grow a partition and filesystem after enlarging a cloud disk
findmntmount tree

Text processing

CommandWhat it does
cut -d: -f1 /etc/passwdpick a field
sort / sort -n / sort -rhsort text / numbers / human sizes, reversed
uniq -ccount repeated lines (sort first)
sort | uniq -c | sort -rn | headtop values of anything
tr 'a-z' 'A-Z' / tr -d '\r'translate / delete characters
awk '{print $1}' FILEfirst column (space separated)
awk -F, '{s+=$3} END {print s}' f.csvsum a CSV column
sed -n '10,20p' FILEprint lines 10–20
sed -i 's/old/new/g' FILEreplace in place
xargs -I{} CMD {}run a command per input line
tee FILEsave and show output
jq '.items[].name' f.jsonread JSON
column -talign columns

Redirection and pipes

SyntaxWhat it does
CMD > FILE / >> FILEoutput to a file / append
CMD 2> err.logerrors to a file
CMD > all.log 2>&1output and errors together
CMD > /dev/null 2>&1discard everything
CMD < FILEfile as input
A | Boutput of A into B
A && B / A || BB only if A succeeded / failed
CMD1 ; CMD2one after the other
$(CMD)insert a command's output

Scheduling

CommandWhat it does
crontab -l / crontab -elist / edit your cron jobs
*/5 * * * * CMDevery 5 minutes (minute hour day month weekday)
0 2 * * * CMD >> /var/log/job.log 2>&1daily at 02:00, with a log
systemctl list-timerssystemd timers
at 18:00run once at a time

Keyboard shortcuts in the shell

KeysWhat they do
Tab / Tab Tabcomplete / show options
Ctrl+Cstop the running command
Ctrl+Lclear the screen
Ctrl+Rsearch command history
Ctrl+A / Ctrl+Estart / end of the line
Ctrl+U / Ctrl+Kdelete before / after the cursor
Ctrl+Dlog out / end of input
!! / sudo !!repeat the last command / with sudo
!$last argument of the previous command

Troubleshooting in five minutes

SymptomRun
Server slowuptime, top, vmstat 1, iostat -xz 1, free -h
Disk fulldf -h, df -i, du -h --max-depth=1 / | sort -h, journalctl --vacuum-size=500M
Service downsystemctl status NAME, journalctl -u NAME -n 100, ss -tulpn
Cannot connectping, dig, nc -zv HOST PORT, curl -v URL, firewall rules
Process killeddmesg -T | grep -i kill (out of memory)
Permission deniedls -l, id, namei -l /path/to/file

Get every new cheat sheet by email

We're adding cheat sheets for Terraform, AWS, GitLab CI/CD and more.

One email a week at most. Unsubscribe with one click.