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.
pwd # print working directorywhoami # current userid # user and group IDshostname # machine nameuname -a # kernel and system infocat /etc/os-release # distribution detailsdate # current date/timecal # calendaruptime # how long the system has been runninghistory # command historyclear # clear terminalman ls # manual pageman -k keyword # search man pagesls --help # short helpinfo coreutils # GNU info systemcd /path/to/dir # absolutecd dirname # relativecd .. # parentcd ~ # homecd - # previous directorycd # home (shortcut)
ls # listls -l # long formatls -la # including hiddenls -lh # human-readable sizesls -lt # sort by timels -lS # sort by sizels -R # recursivetree # tree view (if installed).= current directory..= parent directory~= home directory/= root of the filesystem
# Createtouch file.txtmkdir dirnamemkdir -p path/to/nested
# Copycp file.txt copy.txtcp -r dir/ newdir/ # recursivecp -a src/ dest/ # archive (preserve attributes)
# Move / Renamemv old.txt new.txtmv file.txt /path/to/dir/
# Removerm file.txtrm -r dirname # recursiverm -rf dirname # force (careful!)rmdir dirname # only if empty
# Viewcat file.txtless file.txt # navigable pager (q to quit)head -n 20 file.txttail -n 20 file.txttail -f logfile # follow (live)* # any string? # single character[abc] # one of a, b, c[a-z] # range{a,b,c} # brace expansionls -l # view permissions# -rwxr-xr-x owner group size date name
chmod 755 script.sh # rwxr-xr-xchmod u+x script.sh # add execute for ownerchmod go-w file.txt # remove write for group & otherschmod -R 755 dirname # recursive
chown user file.txtchown user:group file.txtchown -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–
chmod u+s file # setuidchmod g+s dir # setgidchmod +t dir # sticky bit (e.g. /tmp)find /path -name "*.log"find . -type f -name "*.js"find . -type d -name "node_modules"find /var -size +100Mfind . -mtime -7 # modified in last 7 daysfind . -name "*.tmp" -delete
locate filename # fast (uses database)updatedb # refresh locate database (root)
which python3 # location of executablewhereis ls # binary, source, mantype ls # show how command is resolvedgrep "pattern" file.txtgrep -r "pattern" dir/ # recursivegrep -i "pattern" file # case-insensitivegrep -n "pattern" file # show line numbersgrep -v "pattern" file # invert matchgrep -E "pat1|pat2" file # extended regex
sed 's/old/new/' file.txtsed -i 's/old/new/g' file.txt # in-placesed -n '10,20p' file.txt # print lines 10–20
awk '{print $1}' file.txtawk -F: '{print $1}' /etc/passwdawk 'NR>1 {sum+=$3} END {print sum}' data.csv
cut -d: -f1 /etc/passwdsort file.txtsort -n numbers.txt # numericuniq file.txtwc -l file.txt # line counttr 'a-z' 'A-Z' < file.txtcmd1 | cmd2 # pipecmd > file # overwritecmd >> file # appendcmd 2> error.log # stderrcmd > out.log 2>&1 # bothcmd < input.txt # stdin from fileps aux # all processesps -u usernametop # interactivehtop # nicer (if installed)pgrep nginxpkill -f "python script.py"kill PIDkill -9 PID # forcekillall processname
jobs # background jobsbg # resume in backgroundfg # bring to foregroundCtrl+Z # suspendCtrl+C # terminatenohup cmd & # survive logoutfree -h # memorydf -h # disk spacedu -sh * # directory sizesdu -h --max-depth=1vmstat 1iostatsudo apt updatesudo apt upgradesudo apt install packagesudo apt remove packagesudo apt purge package # remove config toosudo apt search keywordsudo apt show packageapt list --installedsudo dnf updatesudo dnf install packagesudo dnf remove packagesudo dnf search keywordsudo pacman -Syu # full upgradesudo pacman -S packagesudo pacman -R packagesudo pacman -Ss keywordip a # addresses (modern)ip r # routesip link # interfaces
# Older but still commonifconfigroute -n
ss -tlnp # listening portsss -s # summarylsof -i :80
ping -c 4 example.comtraceroute example.comcurl https://example.comcurl -I https://example.com # headers onlywget https://example.com/filedig example.comnslookup example.comhost example.com
# Firewall (ufw – Ubuntu)sudo ufw statussudo ufw allow 22sudo ufw enablelsblk # block devicesfdisk -l # partition tables (root)blkid # UUIDs
mount /dev/sdb1 /mntumount /mntdf -hdu -sh /var/log
# Create filesystemsudo mkfs.ext4 /dev/sdb1
# Swapsudo swapon --showfree -hsudo adduser usernamesudo passwd usernamesudo deluser usernamesudo usermod -aG sudo username # add to groupgroups usernameid username
sudo su - # become rootsudo -i # root shellsudo -u otheruser cmdsystemctl status nginxsystemctl start nginxsystemctl stop nginxsystemctl restart nginxsystemctl reload nginxsystemctl enable nginx # start on bootsystemctl disable nginxsystemctl is-enabled nginxsystemctl list-units --type=service
journalctl -u nginxjournalctl -u nginx -f # followjournalctl --since "1 hour ago"journalctl -p err # errors onlytar -czvf archive.tar.gz dir/tar -xzvf archive.tar.gztar -tjf archive.tar.bz2 # list contents
zip -r archive.zip dir/unzip archive.zip
gzip file.txt # creates file.txt.gzgunzip file.txt.gzssh user@hostssh -p 2222 user@hostssh -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 pairssh-keygen -t ed25519 -C "email@example.com"ssh-copy-id user@hostcrontab -e # edit current user crontabcrontab -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)echo $PATHexport VAR=valueecho $VARenv # all environment variableswhich bashecho $SHELL
# .bashrc / .zshrc aliasesalias 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 bashset -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
# Functionsgreet() { echo "Hello, $1"}greet "Linux"dmesg | tailjournalctl -xetail -f /var/log/syslogtail -f /var/log/auth.log
# Disk full?df -hdu -sh /* 2>/dev/null | sort -h
# Who is using space / inodesdu -x -d 1 / | sort -hdf -i
# Network issuesping -c 3 8.8.8.8ip ass -tlnp# Run command as another usersudo -u www-data cmd
# Watch a commandwatch -n 1 'df -h'
# Parallel SSH / tools (if installed)parallel
# Strace a processstrace -p PIDstrace -e openat ls
# Kernel moduleslsmodmodprobe moduleuname -r
# Limitsulimit -aulimit -n 4096
# Time a commandtime ./script.sh- Prefer
rm -ior trash-cli over rawrm -rf - Double-check paths before destructive commands
- Use
sudoonly when needed - Keep systems updated
- Prefer SSH keys over passwords
- Log important administrative actions
# Navigationpwd cd ls -la tree
# Filescp -a mv rm -r mkdir -p touch find locate
# Permissionschmod 755 chown user:group ls -l
# Processesps aux top htop kill killall jobs bg fg
# Packages (Debian)apt update && apt upgrade apt install apt remove
# Networkip a ss -tlnp curl ping dig
# Diskdf -h du -sh lsblk
# Servicessystemctl status|start|stop|enable journalctl -u
# Archivestar -czvf tar -xzvf zip unzip
# Remotessh scp rsync -avz- Linux Command Library: https://linuxcommandlibrary.com
- Explain Shell: https://explainshell.com
- GNU Coreutils Manual: https://www.gnu.org/software/coreutils/manual/
- systemd documentation: https://systemd.io
- Arch Wiki (excellent for concepts): https://wiki.archlinux.org
- TLDR pages: https://tldr.sh (
tldr ls)
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.