devtools logo
by Ryan Macalandag
Back to cheatsheets
Cheatsheetsterminalunixlinuxmacos

Unix / Linux / Mac Terminal Cheatsheet

Common shell commands across Unix, Linux, and macOS.

Files & directories

CommandDoes
mkdir -p a/b/cCreate nested directories, no error if they exist.
touch file.txtCreate an empty file or update its timestamp.
cp -r src destCopy a file or directory (recursively).
mv old newMove or rename.
rm -rf dirDelete recursively, forcefully. No undo — double-check the path.
cat filePrint a file's contents.
ln -s target linkCreate a symbolic link.

Permissions

Unix permissions are three groups of read/write/execute — for the owner, the group, and everyone else.

chmod anatomy
chmod 755 script.sh
#      ││└─ others: r-x (5)
#      │└── group:  r-x (5)
#      └─── owner:  rwx (7)
CommandDoes
chmod +x fileMake a file executable.
chown user:group fileChange file owner and group.
sudo commandRun a single command as an administrator.

Search & text processing

CommandDoes
grep -rn "text" .Recursively search for text, printing line numbers.
find . -name "*.tsx"Find files matching a name pattern.
rg "pattern"ripgrep — a much faster grep alternative, respects .gitignore.
sed 's/foo/bar/g' fileFind-and-replace text in a stream.
awk '{print $1}' filePrint a specific column of whitespace-separated data.
wc -l fileCount lines in a file.

Commands chain together with pipes — the output of one becomes the input of the next.

cat access.log

read the file

grep "500"

keep matching lines

wc -l

count the result

cat access.log | grep "500" | wc -l

Processes

CommandDoes
ps auxList all running processes.
top / htopLive view of CPU and memory usage per process.
kill -9 pidForce-terminate a process by its ID.
lsof -i :3000Find which process is using port 3000.
command &Run a command in the background.
jobs / fg / bgList, foreground, or background jobs in the current shell.

Networking

CommandDoes
curl -I https://example.comFetch just the response headers.
curl -X POST -d '{}' urlSend a POST request with a body.
ping hostCheck reachability and latency.
ssh user@hostOpen a secure remote shell.
scp file user@host:pathCopy a file over SSH.
dig domain.comQuery DNS records for a domain.

Package managers

PlatformInstallUpdate
macOS (Homebrew)brew install pkgbrew upgrade
Debian / Ubuntusudo apt install pkgsudo apt update && sudo apt upgrade
Fedora / RHELsudo dnf install pkgsudo dnf upgrade
Archsudo pacman -S pkgsudo pacman -Syu

Spot an error or have a suggestion? Open an issue on GitHub.