Linux CLI Cheatsheet

7 min read

File & Directory Navigation

CommandDescription
pwdPrint current working directory
ls -laList all files (long format, hidden)
ls -lhSList sorted by size (human readable)
ls -ltList sorted by modification time
cd -Go to previous directory
cd ~Go to home directory
tree -L 2Show directory tree (2 levels deep)
find . -name '*.py'Find files by name pattern
find . -type f -mtime -7Files modified in last 7 days
find . -size +100MFind files larger than 100MB
locate filenameFast file search (uses db)
which / whereisFind command location

File Operations

CommandDescription
cp -r src/ dst/Copy directory recursively
mv old newMove / rename file
rm -rf dir/Force remove directory (CAUTION)
mkdir -p a/b/cCreate nested directories
ln -s target linkCreate symbolic link
touch file.txtCreate empty file / update timestamp
stat fileDetailed file info (size, perms, times)
file myfileDetect file type
basename /path/to/file.txtExtract filename from path
dirname /path/to/file.txtExtract directory from path

File Viewing & Editing

CommandDescription
cat filePrint entire file
less fileScrollable file viewer (q to quit)
head -n 20 fileFirst 20 lines
tail -n 20 fileLast 20 lines
tail -f logfileFollow log in real-time
wc -l fileCount lines in file
diff file1 file2Compare two files
sort file | uniq -cSort and count unique lines
cut -d',' -f1,3 file.csvExtract columns 1 and 3
column -t -s',' file.csvDisplay CSV as aligned table
nano / vim / viTerminal text editors

Text Processing (grep / sed / awk)

CommandDescription
grep -rn 'pattern' .Recursive search with line numbers
grep -i 'pattern' fileCase-insensitive search
grep -v 'exclude' fileInvert match (exclude lines)
grep -c 'pattern' fileCount matching lines
grep -E 'a|b' fileExtended regex (OR)
sed 's/old/new/g' fileReplace all occurrences
sed -i 's/old/new/g' fileIn-place replacement
sed -n '5,10p' filePrint lines 5–10
awk '{print $1, $3}' filePrint columns 1 and 3
awk -F',' '{sum+=$2} END{print sum}'Sum column 2 (CSV)
awk '/pattern/ {print}' filePrint matching lines

Permissions & Ownership

CommandDescription
chmod 755 filerwxr-xr-x (owner rwx, others rx)
chmod +x script.shAdd execute permission
chmod -R 644 dir/Recursive permission change
chown user:group fileChange owner and group
chown -R user dir/Recursive ownership change
umask 022Set default permissions for new files

Process Management

CommandDescription
ps auxList all running processes
ps aux | grep nameFind process by name
top / htopInteractive process monitor
kill PIDTerminate process (graceful)
kill -9 PIDForce kill process
killall nameKill all processes by name
jobsList background jobs
bg / fgMove job to background / foreground
nohup cmd &Run command immune to hangups
cmd & disownRun and detach from terminal
lsof -i :8080Find process using port 8080
pgrep -f patternFind PID by pattern

Networking

CommandDescription
ip addr / ifconfigShow network interfaces
curl -s urlHTTP request (silent mode)
curl -o file urlDownload file from URL
wget urlDownload file
wget -r -l 2 urlRecursive download (2 levels)
ss -tlnp / netstat -tlnpShow listening ports
ping -c 4 hostPing host (4 packets)
traceroute hostTrace network route
dig domainDNS lookup
ssh user@hostSSH into remote server
scp file user@host:/pathCopy file over SSH
rsync -avz src/ user@host:/dst/Sync directories over SSH

Disk & Storage

CommandDescription
df -hDisk usage (human-readable)
du -sh dir/Directory size
du -h --max-depth=1Size of each subdirectory
ncdu /pathInteractive disk usage explorer
mount / umountMount / unmount filesystems
fdisk -lList disk partitions
lsblkList block devices

Compression & Archives

CommandDescription
tar czf archive.tar.gz dir/Create gzipped tarball
tar xzf archive.tar.gzExtract gzipped tarball
tar xzf archive.tar.gz -C /dstExtract to specific directory
tar tf archive.tar.gzList tarball contents
zip -r archive.zip dir/Create zip archive
unzip archive.zipExtract zip archive
gzip / gunzipCompress / decompress .gz
zcat file.gzView compressed file

System Info & Monitoring

CommandDescription
uname -aSystem info (kernel, arch)
cat /etc/os-releaseOS distribution info
uptimeSystem uptime and load
free -hMemory usage
nprocNumber of CPU cores
lscpuCPU architecture info
dmesg | tailKernel messages (recent)
journalctl -u service -fFollow systemd service logs
systemctl status serviceCheck service status
systemctl restart serviceRestart a service

Redirection & Pipes

CommandDescription
cmd > fileRedirect stdout to file (overwrite)
cmd >> fileAppend stdout to file
cmd 2>&1Redirect stderr to stdout
cmd &> fileRedirect all output to file
cmd1 | cmd2Pipe stdout of cmd1 to cmd2
cmd1 | tee file | cmd2Pipe + save to file
cmd | xargs -I{} echo {}Pass piped input as arguments

Handy Shortcuts & One-Liners

CommandDescription
!!Repeat last command
!$Last argument of previous command
Ctrl+RReverse search command history
Ctrl+ZbgSuspend and background a process
Ctrl+A / Ctrl+EJump to start / end of line
history | grep cmdSearch command history
watch -n 2 cmdRun command every 2 seconds
time cmdMeasure execution time
alias ll='ls -la'Create command alias
echo $?Exit code of last command
date +%Y-%m-%dCurrent date (formatted)
env / printenvShow environment variables
export VAR=valueSet environment variable