Linux Commands Cheat Sheet

Essential Linux terminal commands for everyday use. File operations, text processing, networking, system administration, and more. Bookmark this page for quick reference.

File & Directory Operations

CommandDescriptionExample
lsList directory contentsls -la /var/log
cdChange directorycd /etc/nginx
pwdPrint current working directorypwd
mkdirCreate a directorymkdir -p src/components
rmdirRemove an empty directoryrmdir old_folder
rmRemove files or directoriesrm -rf build/
cpCopy files or directoriescp -r src/ backup/
mvMove or rename filesmv old.txt new.txt
touchCreate an empty file or update timestamptouch index.html
findSearch for files in a directory hierarchyfind . -name "*.log" -mtime -7
locateFind files by name using a databaselocate nginx.conf
treeDisplay directory structure as a treetree -L 2 src/

File Viewing & Editing

CommandDescriptionExample
catDisplay file contentscat /etc/hosts
lessView file with scrolling (forward and backward)less /var/log/syslog
moreView file page by page (forward only)more largefile.txt
headDisplay first lines of a filehead -n 20 access.log
tailDisplay last lines of a filetail -f /var/log/syslog
nanoSimple terminal text editornano config.yaml
vimAdvanced terminal text editorvim ~/.bashrc
wcCount lines, words, and byteswc -l *.py
diffCompare two files line by linediff file1.txt file2.txt
sortSort lines of textsort -n -r numbers.txt
uniqRemove or report duplicate linessort data.txt | uniq -c

File Permissions

CommandDescriptionExample
chmodChange file permissionschmod 755 script.sh
chmod u+xAdd execute permission for ownerchmod u+x deploy.sh
chmod go-wRemove write permission for group and otherschmod go-w config.ini
chmod 644Owner read/write, group/others read onlychmod 644 index.html
chmod 700Owner full access, no access for otherschmod 700 ~/.ssh
chownChange file owner and groupchown www-data:www-data /var/www
chgrpChange file group ownershipchgrp developers project/
umaskSet default file creation permissionsumask 022

Text Processing

CommandDescriptionExample
grepSearch for patterns in textgrep -rn "TODO" src/
grep -iCase-insensitive pattern searchgrep -i "error" log.txt
grep -vInvert match (exclude pattern)grep -v "^#" config.ini
sedStream editor for text transformationsed 's/old/new/g' file.txt
sed -iIn-place file editingsed -i 's/http/https/g' urls.txt
awkPattern scanning and processingawk '{print $1, $3}' data.txt
awk -FSet field separatorawk -F: '{print $1}' /etc/passwd
cutExtract sections from linescut -d',' -f1,3 data.csv
trTranslate or delete charactersecho "hello" | tr 'a-z' 'A-Z'
pasteMerge lines of files side by sidepaste names.txt emails.txt
teeRead stdin and write to stdout and filesls | tee filelist.txt
xargsBuild command lines from standard inputfind . -name "*.tmp" | xargs rm

System Information

CommandDescriptionExample
unamePrint system informationuname -a
hostnameShow or set system hostnamehostname -I
uptimeShow how long the system has been runninguptime
whoShow who is logged inwho -a
whoamiPrint the current usernamewhoami
dateDisplay or set the system date and timedate "+%Y-%m-%d %H:%M:%S"
calDisplay a calendarcal 2026
dfReport file system disk space usagedf -h
duEstimate file and directory space usagedu -sh /var/log/*
freeDisplay memory usagefree -h

Process Management

CommandDescriptionExample
psReport current processesps aux | grep nginx
topReal-time process monitoringtop -o %MEM
htopInteractive process viewer (enhanced top)htop
killSend a signal to a processkill -9 12345
killallKill processes by namekillall node
jobsList background jobs in current shelljobs -l
bgResume a suspended job in the backgroundbg %1
fgBring a background job to the foregroundfg %1
nohupRun a command immune to hangupsnohup python server.py &
&Run a command in the background./build.sh &

Networking

CommandDescriptionExample
pingTest network connectivityping -c 4 google.com
curlTransfer data from or to a servercurl -I https://example.com
wgetDownload files from the webwget -O file.zip https://example.com/dl
sshSecure shell remote loginssh user@192.168.1.10
scpSecure copy files between hostsscp file.txt user@host:/tmp/
rsyncFast, versatile remote file syncrsync -avz src/ user@host:/backup/
netstatNetwork statistics (legacy)netstat -tlnp
ssSocket statistics (modern netstat)ss -tlnp | grep :80
ipShow/manipulate network interfacesip addr show
digDNS lookup utilitydig example.com A +short
ifconfigConfigure network interfaces (legacy)ifconfig eth0

Package Management (Debian/Ubuntu)

CommandDescriptionExample
apt updateUpdate the package indexsudo apt update
apt upgradeUpgrade all installed packagessudo apt upgrade -y
apt installInstall a packagesudo apt install nginx
apt removeRemove a package (keep config files)sudo apt remove nginx
apt purgeRemove a package and its config filessudo apt purge nginx
apt searchSearch for a packageapt search nodejs
apt list --installedList all installed packagesapt list --installed | grep python
snap installInstall a snap packagesudo snap install code --classic
dpkg -iInstall a .deb package filesudo dpkg -i package.deb
dpkg -lList installed packagesdpkg -l | grep nginx

Compression & Archiving

CommandDescriptionExample
tar -czfCreate a gzipped tar archivetar -czf backup.tar.gz /var/www
tar -xzfExtract a gzipped tar archivetar -xzf backup.tar.gz
tar -cjfCreate a bzip2 tar archivetar -cjf archive.tar.bz2 folder/
tar -xjfExtract a bzip2 tar archivetar -xjf archive.tar.bz2
tar -tvfList contents of a tar archivetar -tvf archive.tar.gz
gzipCompress a file with gzipgzip access.log
gunzipDecompress a gzip filegunzip access.log.gz
zipCreate a zip archivezip -r project.zip src/
unzipExtract a zip archiveunzip project.zip -d output/
bzip2Compress a file with bzip2bzip2 largefile.txt
xzCompress a file with xz (high compression)xz -9 database.sql

User Management

CommandDescriptionExample
useraddCreate a new user accountsudo useradd -m -s /bin/bash john
usermodModify a user accountsudo usermod -aG docker john
userdelDelete a user accountsudo userdel -r john
passwdChange a user passwordsudo passwd john
groupsShow group memberships for a usergroups john
sudoExecute a command as superusersudo systemctl restart nginx
suSwitch to another user accountsu - john

System Services (systemd)

CommandDescriptionExample
systemctl startStart a servicesudo systemctl start nginx
systemctl stopStop a servicesudo systemctl stop nginx
systemctl restartRestart a servicesudo systemctl restart nginx
systemctl statusCheck the status of a servicesystemctl status nginx
systemctl enableEnable a service to start at bootsudo systemctl enable nginx
systemctl disableDisable a service from starting at bootsudo systemctl disable nginx
systemctl list-unitsList all active systemd unitssystemctl list-units --type=service
journalctlView systemd logsjournalctl -u nginx --since "1 hour ago"
journalctl -fFollow system logs in real timejournalctl -fu nginx

Useful Keyboard Shortcuts

ShortcutDescriptionExample Usage
Ctrl + CKill the current foreground process (SIGINT)Stop a running command like ping
Ctrl + ZSuspend the current process (SIGTSTP)Pause vim, return with fg
Ctrl + DSend EOF — close shell or end inputExit a shell session or end cat input
Ctrl + RReverse search through command historyType to find previous commands interactively
Ctrl + LClear the terminal screenSame as typing clear
!!Repeat the last commandsudo !! to re-run with sudo
!$Last argument of the previous commandmkdir dir && cd !$
TabAuto-complete commands, paths, and filenamesType sys then Tab for systemctl

Frequently Asked Questions

What is the difference between rm and rmdir in Linux?

rmdir only removes empty directories and will fail if the directory contains any files or subdirectories. rm -r (recursive) removes directories along with all their contents. Use rmdir for safety when you expect a directory to be empty, and rm -r when you intentionally want to delete everything inside. Always double-check before using rm -rf as it will not prompt for confirmation.

How do I find files by name in Linux?

Use the find command: find /path -name "filename". Use -iname for case-insensitive search. For example, find / -name "*.log" finds all .log files on the system. You can combine with other criteria like -type f (files only), -mtime -7 (modified in last 7 days), or -size +100M (larger than 100MB). The locate command is faster but uses a pre-built database that needs updating with sudo updatedb.

What do the chmod numbers like 755 and 644 mean?

Each digit represents permissions for owner, group, and others respectively. The values are sums of: 4 (read), 2 (write), 1 (execute). So 755 means the owner has full access (7=4+2+1), while group and others can read and execute (5=4+1). 644 means the owner can read and write (6=4+2), while group and others can only read (4). Use 755 for scripts and directories, 644 for regular files.

How do I check which process is using a specific port in Linux?

Use ss -tlnp | grep :PORT or netstat -tlnp | grep :PORT to find which process is listening on a specific port. For example, ss -tlnp | grep :80 shows what is using port 80. You can also use lsof -i :PORT. These commands typically require sudo to show full process details including the PID and program name.