← All cheat sheets · 🪟 Windows shell
🐧 Linux shell
The commands you reach for daily on Linux servers and in WSL: files, text, search, processes, disks, networking, packages and services.
80 commands in 12 sections
Move around and manage files
pwdPrint the directory you are in
ls -lahList files with sizes, permissions and hidden files
e.g.
ls -lah /var/logcd -Jump back to the previous directory
cd with no argument goes home (~).
mkdir -p a/b/cCreate a directory and any missing parents
cp -r src destCopy a file or a whole directory
e.g.
cp -r site/ site-backup/mv old newMove or rename a file or directory
rm -r dirDelete a directory and everything in it
There is no recycle bin. Add -i to confirm each file.
touch file.txtCreate an empty file, or update its timestamp
ln -s target linkMake a symbolic link
e.g.
ln -s /opt/app/current apptree -L 2Show a directory tree two levels deep
Install with your package manager if missing.
Read and compare text
cat filePrint a whole file
less filePage through a file: / searches, q quits
head -n 20 fileShow the first 20 lines
tail -f app.logFollow a log as it grows
e.g.
tail -n 100 -f /var/log/syslogwc -l fileCount lines
diff -u old newShow the differences between two files
nano fileEdit a file in a simple terminal editor
In vim: i to type, Esc then :wq to save and quit.
Search
grep -rn 'TODO' src/Find text in files, with file names and line numbers
e.g.
grep -rni 'timeout' /etc/nginxfind . -name '*.log'Find files by name under a directory
find . -type f -mtime +7Find files changed more than 7 days ago
find . -type f -size +100MFind files bigger than 100 MB
which python3Show which program a command runs
history | grep sshSearch the commands you ran before
Ctrl+R searches history as you type.
Slice and transform text
sort file | uniq -c | sort -rnCount repeated lines, most frequent first
e.g.
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | headcut -d: -f1 /etc/passwdTake one field from each line, split on a delimiter
awk '{print $1}' filePrint the first column of each line
e.g.
awk -F, '{print $2}' data.csvsed -i 's/old/new/g' fileReplace text in a file in place
On macOS use sed -i '' (BSD sed).
tr 'a-z' 'A-Z'Translate characters, here to upper case
e.g.
echo hello | tr 'a-z' 'A-Z'xargsTurn input lines into arguments for another command
e.g.
find . -name '*.tmp' -print0 | xargs -0 rmjq '.items[].name' data.jsonQuery JSON
Install jq with your package manager.
Permissions and users
chmod +x script.shMake a file executable
chmod 600 ~/.ssh/id_ed25519Owner can read and write; nobody else can do anything
SSH refuses private keys that others can read.
chown -R user:group dirChange who owns files
sudo commandRun one command as root
e.g.
sudo !!sudo !! reruns your last command with sudo.
whoamiPrint your user name
idShow your user id and groups
Processes and jobs
ps auxList every running process
e.g.
ps aux | grep '[n]ginx'topLive view of CPU and memory use
htop is friendlier if installed. q quits.
kill PIDAsk a process to stop (SIGTERM)
kill -9 PIDForce a process to stop (SIGKILL)
Last resort: the process gets no chance to clean up.
pgrep -f nameFind process ids by name or command line
also
pkill -f namecommand &Run a command in the background
nohup command > out.log 2>&1 &Keep a command running after you log out
jobsList this shell's background jobs
e.g.
fg %1Ctrl+Z pauses the foreground job; bg resumes it in the background.
Disks and system
df -hFree space on each mounted disk
du -sh * | sort -hSize of each item here, biggest last
free -hMemory and swap in use
lsblkList disks and partitions
uname -aKernel and architecture
cat /etc/os-releaseWhich distribution and version this is
uptimeHow long the machine has run, and its load averages
Network
curl -I https://example.comFetch only the response headers
curl -X POST -H 'Content-Type: application/json' -d '{"a":1}' URLSend JSON to an API
curl -LO URLDownload a file, following redirects
also
wget URLss -tulpnWhich ports are listening, and by which process
Needs sudo to see other users’ processes.
lsof -i :8080Which process is using port 8080
ping -c 4 hostCheck a host answers, four times
dig +short example.comLook up a DNS record
also
nslookup example.comip addrShow network interfaces and IP addresses
Remote machines
ssh user@hostOpen a shell on another machine
e.g.
ssh -i ~/.ssh/key.pem ubuntu@203.0.113.10scp file user@host:/path/Copy a file to another machine
e.g.
scp user@host:/var/log/app.log .rsync -avz src/ user@host:/srv/app/Sync a directory, sending only what changed
A trailing / on src copies its contents, not the folder itself.
ssh-keygen -t ed25519Create an SSH key pair
also
ssh-copy-id user@host
Archives
tar -czf site.tar.gz site/Pack a directory into a .tar.gz
tar -xzf site.tar.gz -C /tmpUnpack a .tar.gz into a directory
tar -tzf site.tar.gzList what is inside a .tar.gz
zip -r site.zip site/Pack a directory into a .zip
also
unzip site.zip
Packages and services
sudo apt update && sudo apt install -y nginxInstall a package on Debian or Ubuntu
also
sudo dnf install -y nginxsudo systemctl status nginxIs a service running, and its latest log lines
sudo systemctl restart nginxRestart a service
sudo systemctl enable --now nginxStart a service now and on every boot
journalctl -u nginx -fFollow a service's logs
e.g.
journalctl -u nginx --since "1 hour ago"crontab -eEdit your scheduled jobs
e.g.
0 3 * * * /usr/local/bin/backup.shFields: minute hour day-of-month month day-of-week.
Shell tricks
command > out.txtSend output to a file, replacing it
also
command >> out.txtcommand 2>&1 | tee run.logShow output and errors and save them to a file
$(command)Use a command's output inside another
e.g.
echo "Today is $(date +%F)"export NAME=valueSet an environment variable for this shell and its programs
e.g.
echo $PATHalias ll='ls -lah'Make a shortcut
Put it in ~/.bashrc to keep it.
command --helpQuick help for most commands
also
man commandwatch -n 2 'df -h'Rerun a command every 2 seconds