Linux command cheat sheet
The Linux commands you will reach for most, grouped by the job you are doing. Each entry gives the command, a plain-English summary and a realistic example. Options follow the official manual pages, so run man followed by the command name for the full detail.
- Getting around
- Files and directories
- Viewing and searching text
- Editing text in pipelines
- Permissions and ownership
- Processes
- Disks and storage
- Networking
- Packages
- Services and logs
- Archives and compression
- Shell shortcuts and history
Getting around
Find out where you are and move between folders.
pwdPrint the full path of the folder you are in.
pwdLearn it: Navigating directories and wildcards →ls -lahList all files, hidden too, with details and readable sizes. -t sorts by time.
ls -lah ~/projectsLearn it: Basic commands →cd dirChange folder. cd .. goes up one level; cd alone goes home.
cd ~/projectsLearn it: Navigating directories and wildcards →cd -Jump back to the folder you were in before.
cd -Learn it: Navigating directories and wildcards →tree -L 2Draw the folder structure as a tree, two levels deep. May need installing.
tree -L 2 ~/projectsLearn it: Navigating directories and wildcards →realpath fileShow the full absolute path of a file.
realpath report.txtLearn it: Navigating directories and wildcards →
Files and directories
Create, copy, move, link, find and remove files and folders.
cp src destCopy a file. -r copies a folder; -i asks before overwriting.
cp report.txt report.bakLearn it: Basic commands →mv src destMove or rename a file or folder. -i asks before overwriting.
mv draft.txt report.txtLearn it: Basic commands →rm fileDelete a file permanently; there is no bin. Add -i to confirm.
rm -i old-notes.txtLearn it: Basic commands →rm -r dirCaution: deletes a folder and everything inside it, permanently.
rm -r ~/projects/old-siteLearn it: Basic commands →mkdir -p pathMake a folder, creating any missing parent folders too.
mkdir -p ~/projects/site/cssLearn it: Navigating directories and wildcards →touch fileCreate an empty file, or update an existing file's timestamp.
touch notes.txtLearn it: Basic commands →ln -s target linkCreate a symbolic link (a shortcut) pointing at another file or folder.
ln -s ~/projects/site ~/siteLearn it: Inside a traditional filesystem: inodes and links →find dir -name patternSearch a folder tree by name, type or age.
find ~/projects -name "*.txt" -mtime -7Learn it: Important shell script utilities →stat fileShow a file's size, permissions, owner and timestamps.
stat report.txtLearn it: Inside a traditional filesystem: inodes and links →
Viewing and searching text
Read files, follow logs and pick out the lines you need.
cat filePrint a whole file to the screen. Best for short files.
cat report.txtLearn it: Basic commands →less fileScroll through a long file. / searches, q quits. For system logs, use journalctl.
less app.logLearn it: Intermediate commands: grep, less, diff, find, sort →head -n 20 fileShow the first lines of a file (10 by default).
head -n 20 users.csvLearn it: Intermediate commands: grep, less, diff, find, sort →tail -f fileShow the end of a file and keep printing new lines. Ctrl+C stops.
tail -f app.logLearn it: Intermediate commands: grep, less, diff, find, sort →grep -rni text dirFind matching lines. -r searches folders, -n numbers lines, -i ignores case.
grep -rni "error" ~/projectsLearn it: Intermediate commands: grep, less, diff, find, sort →wc -l fileCount lines. Use -w for words or -c for bytes.
wc -l app.logLearn it: Intermediate commands: grep, less, diff, find, sort →sort fileSort lines alphabetically. -n sorts numbers, -r reverses, -k picks a field.
sort -t, -k2,2 -n users.csvLearn it: Intermediate commands: grep, less, diff, find, sort →uniq -cCollapse repeated adjacent lines and count them. Sort first.
sort fruit.txt | uniq -cLearn it: Intermediate commands: grep, less, diff, find, sort →cut -d, -f1,3 filePull out chosen columns, split on a delimiter.
cut -d, -f1,3 users.csvLearn it: Intermediate commands: grep, less, diff, find, sort →diff -u old newShow line-by-line differences between two files.
diff -u report.txt report-v2.txtLearn it: Intermediate commands: grep, less, diff, find, sort →
Editing text in pipelines
Transform text as it flows from one command to the next.
sed 's/old/new/g'Replace text on every line and print the result.
sed 's/http:/https:/g' links.txtLearn it: Important shell script utilities →sed -i 's/old/new/' fileEdit the file in place. Use -i.bak to keep a backup copy.
sed -i.bak 's/draft/final/' report.txtLearn it: Important shell script utilities →awk '{print $1}'Print chosen columns. -F sets the separator.
awk -F, '{print $1}' users.csvLearn it: Important shell script utilities →awk '{sum += $2} END {print sum}'Add up a column of numbers.
awk -F, '{sum += $2} END {print sum}' users.csvLearn it: Important shell script utilities →tr set1 set2Swap or delete characters. -d deletes them.
tr -d '\r' < users.csv > users-unix.csvLearn it: Intermediate commands: grep, less, diff, find, sort →tee fileShow output on screen and save it to a file at once. -a appends.
ls -l | tee listing.txtLearn it: Shell input and output: redirection and pipes →xargs cmdTurn input lines into arguments for another command.
find . -name "*.log" -print0 | xargs -0 wc -lLearn it: Shell input and output: redirection and pipes →
Permissions and ownership
Control who can read, change and run each file.
chmod 644 fileSet owner, group, others by number: 7 all, 6 read+write, 5 read+run, 4 read.
chmod 755 deploy.shLearn it: File modes and permissions →chmod u+x fileChange permissions by letter: u/g/o for who, + or - for add or remove.
chmod u+x deploy.shLearn it: File modes and permissions →chmod -R mode dirCaution: changes every file under a folder. Check the path first.
chmod -R g+w ~/projects/siteLearn it: File modes and permissions →chown user:group fileChange a file's owner and group. Usually needs sudo.
sudo chown alex:developers report.txtLearn it: File modes and permissions →chgrp group fileChange only the group a file belongs to.
sudo chgrp developers report.txtLearn it: File modes and permissions →umaskShow or set the default permissions for new files. 022 is common.
umask 027Learn it: File modes and permissions →sudo cmdCaution: runs one command as root, with no safety net. Check it first.
sudo nano /etc/hostsLearn it: Running commands as the superuser →idShow your user ID and the groups you belong to.
id alexLearn it: User IDs and user access →
Processes
See what is running and stop, pause or reprioritise it.
ps auxList every running process with its owner, ID and resource use.
ps aux | grep nginxLearn it: Listing and manipulating processes: signals and jobs →topLive view of processes and load. q quits. htop is a friendlier add-on.
topLearn it: Tracking processes →kill PIDAsk a process to stop cleanly (sends TERM).
kill 4821Learn it: Listing and manipulating processes: signals and jobs →kill -9 PIDCaution: force-kills at once; unsaved work in that process is lost.
kill -9 4821Learn it: Listing and manipulating processes: signals and jobs →pkill -f patternStop every process matching a command line. Preview with pgrep -af first.
pkill -f "python app.py"Learn it: Listing and manipulating processes: signals and jobs →jobs, fg %1, bg %1List shell jobs. Ctrl+Z pauses; fg resumes in front, bg behind.
fg %1Learn it: Listing and manipulating processes: signals and jobs →nohup cmd &Keep a command running after you log out.
nohup ./backup.sh > backup.log 2>&1 &Learn it: Listing and manipulating processes: signals and jobs →nice -n 10 cmdStart a command at lower priority so it does not hog the CPU.
nice -n 10 tar -czf backup.tar.gz ~/projectsLearn it: Resource monitoring: CPU, memory, and I/O →
Disks and storage
Check free space and see what is plugged in and mounted.
df -hShow free and used space on each mounted filesystem.
df -h /Learn it: Mounting filesystems and /etc/fstab →du -sh dirShow the total size of a folder. du -h -d 1 lists each subfolder.
du -sh ~/projectsLearn it: Mounting filesystems and /etc/fstab →lsblkList disks and partitions as a tree. -f adds filesystem details.
lsblk -fLearn it: Device name summary →mount device dirAttach a disk or partition to a folder. Needs sudo.
sudo mount /dev/sdb1 /mnt/usbLearn it: Mounting filesystems and /etc/fstab →umount dirSafely detach a mounted disk before unplugging it.
sudo umount /mnt/usbLearn it: Mounting filesystems and /etc/fstab →free -hShow used and available memory (RAM) and swap.
free -hLearn it: Resource monitoring: CPU, memory, and I/O →
Networking
Check your connection, reach other machines and move files across.
ip aShow network interfaces and their IP addresses. Replaces ifconfig.
ip aLearn it: Configuring interfaces and network managers →ip routeShow the routing table, including the default gateway. Replaces route.
ip routeLearn it: Routes, the routing table, and the default gateway →ss -tulpnList listening ports and the programs using them. Replaces netstat.
sudo ss -tulpnLearn it: The transport layer: TCP, UDP, and services →ping -c 4 hostCheck a host is reachable, sending four packets.
ping -c 4 example.comLearn it: Basic ICMP and DNS tools →curl -LO urlDownload a file, following redirects. -I shows headers only.
curl -LO https://example.com/report.pdfLearn it: The basics of network services →ssh user@hostLog in to a remote machine securely. -p sets the port.
ssh -p 2222 alex@192.0.2.10Learn it: Network servers and SSH →scp src user@host:destCopy files to or from another machine over SSH. -r for folders.
scp report.txt alex@192.0.2.10:~/Learn it: Quick copies and rsync →rsync -avz src destSync folders, sending only changes. A slash after src copies its contents.
rsync -avz ~/projects/ alex@192.0.2.10:/backup/projects/Learn it: Quick copies and rsync →dig name +shortLook up a domain's DNS records. Comes with dnsutils or bind-utils.
dig example.com +shortLearn it: Basic ICMP and DNS tools →
Packages
apt is for Debian and Ubuntu; dnf is for Fedora and RHEL. Changes need sudo.
apt updateDebian/Ubuntu: refresh the package lists. Run before installing or upgrading.
sudo apt updateLearn it: Installation practice and package managers →dnf check-updateFedora/RHEL: list pending updates, refreshing stale package data. Nearest to apt update.
dnf check-updateLearn it: Installation practice and package managers →apt upgradeDebian/Ubuntu: install available updates for installed packages.
sudo apt upgradeLearn it: Installation practice and package managers →dnf upgradeFedora/RHEL: install all available updates.
sudo dnf upgradeLearn it: Installation practice and package managers →apt install pkgDebian/Ubuntu: install a package and anything it depends on.
sudo apt install htopLearn it: Installation practice and package managers →dnf install pkgFedora/RHEL: install a package and anything it depends on.
sudo dnf install htopLearn it: Installation practice and package managers →apt remove pkgDebian/Ubuntu: uninstall a package. Settings stay; apt purge removes them too.
sudo apt remove htopLearn it: Installation practice and package managers →dnf remove pkgFedora/RHEL: uninstall a package. Check the list shown before saying yes.
sudo dnf remove htopLearn it: Installation practice and package managers →apt search termDebian/Ubuntu: search package names and descriptions.
apt search image editorLearn it: Installation practice and package managers →dnf search termFedora/RHEL: search package names and summaries.
dnf search image editorLearn it: Installation practice and package managers →apt autoremoveDebian/Ubuntu: remove dependencies nothing needs any more. Check the list first.
sudo apt autoremoveLearn it: Installation practice and package managers →dnf autoremoveFedora/RHEL: remove dependencies nothing needs any more. Check the list first.
sudo dnf autoremoveLearn it: Installation practice and package managers →
Services and logs
Manage systemd services and read system logs with journalctl.
systemctl status svcShow whether a service is running, plus its latest log lines.
systemctl status nginxLearn it: systemd operation, dependencies, and targets →systemctl start|stop|restart svcStart, stop or restart a service now. Needs sudo.
sudo systemctl restart nginxLearn it: systemd operation, dependencies, and targets →systemctl enable --now svcStart a service at boot, and start it straight away.
sudo systemctl enable --now nginxLearn it: systemd operation, dependencies, and targets →systemctl --failedList services and other units that have failed.
systemctl --failedLearn it: systemd operation, dependencies, and targets →journalctl -u svcShow logs for one service. --since narrows the time window.
journalctl -u nginx --since "1 hour ago"Learn it: System logging: the journal and syslog →journalctl -fFollow the system log live as new entries arrive.
journalctl -u nginx -fLearn it: System logging: the journal and syslog →
Archives and compression
Bundle files together and shrink them for storage or sharing.
tar -czf out.tar.gz filesCreate a gzip-compressed archive from files or folders.
tar -czf backup.tar.gz ~/projectsLearn it: Archiving and compressing files →tar -xzf file.tar.gzExtract an archive. -C picks the destination folder.
tar -xzf backup.tar.gz -C ~/restoreLearn it: Archiving and compressing files →tar -tzf file.tar.gzList what is inside an archive without extracting.
tar -tzf backup.tar.gzLearn it: Archiving and compressing files →gzip fileCompress a single file. -k keeps the original; gunzip reverses it.
gzip -k app.logLearn it: Archiving and compressing files →zip -r out.zip dirCreate a zip archive of a folder. May need installing.
zip -r site.zip ~/projects/siteLearn it: Archiving and compressing files →unzip file.zipExtract a zip. -l lists contents; -d picks the folder.
unzip site.zip -d ~/site-copyLearn it: Archiving and compressing files →
Shell shortcuts and history
Type less and recover faster in the Bash shell.
historyList the commands you have run recently, numbered.
history | grep sshLearn it: Special characters and command-line editing →!!Repeat the last command. Handy when you forgot sudo.
sudo !!Learn it: Special characters and command-line editing →Ctrl+RSearch back through history as you type. Enter runs the match.
Ctrl+R, then type rsyncLearn it: Special characters and command-line editing →Ctrl+CCancel the command that is running now.
ping example.com, then Ctrl+CLearn it: Listing and manipulating processes: signals and jobs →Ctrl+DSend end-of-input; on an empty line it closes the shell.
Ctrl+D to leave an SSH sessionLearn it: The Bourne shell and using it →TabComplete a command or file name. Press twice to see options.
cd ~/proj then TabLearn it: Special characters and command-line editing →alias name='cmd'Give a long command a short name. Put it in ~/.bashrc to keep it.
alias ll='ls -lah'Learn it: Dot files, environment variables, and the command path →echo $PATHShow the folders the shell searches for commands.
echo $PATHLearn it: Dot files, environment variables, and the command path →
Need the permissions numbers? Try the chmod calculator or the cron explainer, and see how to set up a practice lab.
