Notes

Notes // linux-commands

Linux Commands Cheatsheet

2026-04-02 · 6 min read
linuxlinux-commandsclireference

Linux Commands Cheatsheet

CommandDescription
pwdPrint working directory
cd ~ / cdHome directory
cd -Previous directory (OLDPWD)
cd ..Parent directory
lsList files
ls -laAll files, long format, human sizes often ls -lah
ls -ltSort by time, newest first
tree -L 2Directory tree (install tree if missing)
realpath fileCanonical absolute path
basename /path/to/file.txtFilename only
dirname /path/to/file.txtDirectory portion

File operations

CommandDescription
touch fileCreate empty file or update mtime
cp src dstCopy file
cp -r src/ dst/Copy directory recursively
cp -a src/ dst/Archive mode: preserve perms, links, times
mv old newMove or rename
rm fileRemove file
rm -r dir/Remove directory
rm -rf dir/Force recursive (dangerous)
ln -s target linknameSymbolic link
ln target linknameHard link
mkdir -p a/b/cCreate parents as needed
rmdir dirRemove empty directory
install -m 755 src dstCopy with mode (common in scripts)

Viewing & editing

CommandDescription
cat filePrint entire file
tac filePrint lines reversed order
less filePager (scroll, search with /)
head -n 20 fileFirst 20 lines
tail -f fileFollow log file live
tail -n 100 fileLast 100 lines
wc -l fileLine count
file /bin/shGuess file type
stat fileInode metadata, timestamps
vim / nanoEditors

Find & locate

CommandDescription
find . -name "*.log"By name pattern
find /var -type f -mtime -1Files modified in last day
find . -maxdepth 2 -type dDirectories, depth limit
find . -size +100MFiles larger than 100MB
find . -exec grep -l foo {} \;Run command per file
find . -print0 | xargs -0 rmSafe with spaces in names
locate passwdFast DB search (updatedb)
which python3Executable on PATH
whereis lsBinary, man, source

Permissions & ownership

CommandDescription
chmod 755 script.shrwxr-xr-x
chmod u+x fileAdd execute for owner
chmod -R g+w dir/Recursive group write
chown user:group fileChange owner and group
chown -R www-data: dir/Recursive
umask 022Default file mode mask (shell)
getfacl filePOSIX ACLs
setfacl -m u:bob:rwx fileSet ACL entry
sudo chown root fileElevated ownership change

Text processing

CommandDescription
grep pattern fileSearch lines
grep -r "term" .Recursive
grep -nLine numbers
grep -EExtended regex
grep -iCase insensitive
sed -n '5,10p' filePrint lines 5–10
sed -i 's/old/new/g' fileIn-place replace (GNU sed)
awk '{print $1}' fileFirst column (whitespace)
awk -F: '{print $1}' /etc/passwdField separator
cut -d: -f1 /etc/passwdCut fields
sort fileSort lines
sort -uUnique lines
uniqAdjacent duplicate collapse (often after sort)
sort | uniq -c | sort -rnCount occurrences
tr 'a-z' 'A-Z'Translate characters
paste f1 f2Merge lines side by side
diff -u a bUnified diff

Processes & jobs

CommandDescription
ps auxAll processes (BSD style)
ps -efFull listing (POSIX)
pgrep -af nginxFind PIDs by name
pkill nginxSignal by name
kill PIDSIGTERM
kill -9 PIDSIGKILL (last resort)
kill -lList signal names
top / htopInteractive monitors
nice -n 10 cmdLower CPU priority
renice +5 -p PIDChange running process nice
jobsShell background jobs
fg / bgForeground / background
nohup cmd &Survive terminal close
lsof -i :3000Process using port
lsof -p PIDOpen files for process

systemd & services

CommandDescription
systemctl status nginxService status
systemctl start|stop|restart nginxControl service
systemctl enable nginxStart at boot
systemctl disable nginxDisable at boot
systemctl daemon-reloadAfter unit file edits
journalctl -u nginx -fFollow unit logs
journalctl --since "1 hour ago"Time window
journalctl -bCurrent boot only

Disk & filesystem

CommandDescription
df -hFilesystem space human-readable
df -iInode usage
du -sh dir/Total size of directory
du -h --max-depth=1Per-subdirectory sizes
lsblkBlock devices tree
mountShow mounts
mount /dev/sdb1 /mntMount device
umount /mntUnmount
fdisk -lPartition tables (needs root)
blkidUUID / type of block devs

Networking

CommandDescription
ip addrIP addresses (replaces ifconfig)
ip routeRouting table
ip linkInterfaces state
ss -tlnpTCP listening + process
ss -tulpnAll listening UDP/TCP
netstat -tulpnLegacy sockets listing
ping -c 4 hostICMP echo
traceroute hostPath hops
mtr hostCombined ping/trace
dig +short A example.comDNS lookup
nslookup hostDNS (simple)
curl -I URLHTTP headers only
curl -v URLVerbose
wget -O- URLDownload to stdout
nc -zv host 443Port check (netcat)
tcpdump -i eth0 port 80Packet capture (root)

Users & sudo

CommandDescription
whoamiCurrent user
idUID, GID, groups
su - userSwitch user (login shell)
sudo -iRoot login shell
sudo -u www-data cmdRun as specific user
sudo -lList allowed sudo commands
passwdChange password
getent passwd userLookup user (NSS)
groups userGroup membership

Package managers

CommandDescription
apt update && apt upgradeDebian/Ubuntu refresh & upgrade
apt install pkgInstall
apt remove pkgRemove
apt search termSearch packages
dpkg -l | grep fooInstalled .deb packages
yum install pkg / dnf installRHEL/Fedora family
rpm -qaList installed RPMs

Archives & compression

CommandDescription
tar czf archive.tar.gz dir/Create gzip tarball
tar xzf archive.tar.gzExtract
tar cjf archive.tar.bz2 dir/bzip2
zip -r out.zip dir/Zip recursive
unzip file.zipExtract zip
gzip -k fileCompress file (keep original with -k GNU)
zcat file.gzCat gzipped file

Environment & shell

CommandDescription
echo $PATHShow PATH
export VAR=valueSet env var (bash)
envPrint environment
printenv HOMESingle variable
source script.shRun in current shell
. script.shSame as source
historyCommand history
alias ll='ls -la'Shell alias
watch -n 2 'df -h'Re-run every 2s

SSH & remote

# Connect
ssh user@host
ssh -i ~/.ssh/key.pem user@host

# Jump host (ProxyJump)
ssh -J bastion user@internal

# Copy file
scp file.txt user@host:/path/
rsync -avz ./local/ user@host:/remote/

# Local port forward (local 8080 → remote 3000)
ssh -L 8080:localhost:3000 user@host

# Remote forward (remote port → your local)
ssh -R 9090:localhost:9090 user@host

# Copy SSH key to server
ssh-copy-id user@host

# Disable strict host key check (avoid except debug)
ssh -o StrictHostKeyChecking=no user@host

Misc one-liners

CommandDescription
curl ifconfig.mePublic IP (third-party service)
timeout 5 cmdKill command after 5s
xargs -n1One argument per invocation
tee fileWrite stdin to file and stdout
sudo !!Re-run last command with sudo (bash)

Keyboard shortcuts (bash)

  • Ctrl+A / Ctrl+E — start / end of line
  • Ctrl+U / Ctrl+K — delete to start / end of line
  • Ctrl+W — delete word backward
  • Ctrl+R — reverse search history
  • Ctrl+L — clear screen
  • Ctrl+Z — suspend; bg / fg
  • !! — repeat last command
  • !$ — last argument of previous command
  • !n — history event number n