Bookmark this page. Use Ctrl+F (or Cmd+F on Mac) to find what you need. This cheat sheet covers essential terminal commands for macOS and Linux.
Last updated: March 2026
Navigation#
| Command | Description |
|---|
pwd | Print current directory |
ls | List files |
ls -la | List all files with details (including hidden) |
ls -lh | List with human-readable sizes |
cd /path | Change directory |
cd ~ | Go to home directory |
cd .. | Go up one directory |
cd - | Go to previous directory |
tree | Show directory tree (install: brew install tree) |
tree -L 2 | Tree with max depth 2 |
Files and Directories#
| Command | Description |
|---|
touch file.txt | Create empty file |
mkdir mydir | Create directory |
mkdir -p a/b/c | Create nested directories |
cp file.txt copy.txt | Copy file |
cp -r dir/ newdir/ | Copy directory recursively |
mv file.txt newname.txt | Rename/move file |
rm file.txt | Delete file |
rm -r dir/ | Delete directory recursively |
rm -rf dir/ | Force delete (no confirmation) |
ln -s target link | Create symbolic link |
cat file.txt | Print file contents |
head -20 file.txt | First 20 lines |
tail -20 file.txt | Last 20 lines |
tail -f file.log | Follow file in real-time (logs) |
less file.txt | Paginated viewer (q to quit) |
wc -l file.txt | Count lines |
wc -w file.txt | Count words |
diff file1 file2 | Compare two files |
stat file.txt | File details (size, dates, permissions) |
Searching#
| Command | Description |
|---|
grep "text" file.txt | Search for text in file |
grep -r "text" dir/ | Search recursively in directory |
grep -i "text" file.txt | Case-insensitive search |
grep -n "text" file.txt | Show line numbers |
grep -c "text" file.txt | Count matches |
grep -v "text" file.txt | Show lines NOT matching |
find . -name "*.txt" | Find files by name |
find . -type d -name "src" | Find directories by name |
find . -size +10M | Find files larger than 10MB |
find . -mtime -7 | Files modified in last 7 days |
which python | Find where a command lives |
locate file.txt | Fast file search (uses index) |
Pipes and Redirection#
# Pipe — send output of one command to another
ls -la | grep ".txt"
cat file.txt | sort | uniq
ps aux | grep node
# Redirect output to file
echo "hello" > file.txt # overwrite
echo "world" >> file.txt # append
# Redirect errors
command 2> errors.log # stderr to file
command > output.log 2>&1 # stdout + stderr to file
command &> all.log # same (bash shorthand)
# /dev/null — discard output
command > /dev/null 2>&1 # silence everything
cmd1 | cmd2 pipe: stdout of cmd1 → stdin of cmd2
cmd > file redirect stdout to file (overwrite)
cmd >> file redirect stdout to file (append)
cmd 2> file redirect stderr to file
cmd < file use file as stdin
Permissions#
-rwxr-xr-- 1 alex staff 4096 Mar 15 file.txt
│├─┤├─┤├─┤
│ │ │ │
│ │ │ └── Others: read only
│ │ └───── Group: read + execute
│ └───────── Owner: read + write + execute
└─────────── File type (- = file, d = directory, l = link)
| Command | Description |
|---|
chmod 755 file | rwxr-xr-x (owner all, group/others read+exec) |
chmod 644 file | rw-r–r– (owner read+write, others read) |
chmod +x script.sh | Add execute permission |
chmod -w file.txt | Remove write permission |
chown user:group file | Change owner and group |
chown -R user dir/ | Change owner recursively |
| Number | Permission |
|---|
7 | rwx (read + write + execute) |
6 | rw- (read + write) |
5 | r-x (read + execute) |
4 | r– (read only) |
0 | — (no permission) |
Processes#
| Command | Description |
|---|
ps aux | List all processes |
ps aux | grep node | Find a specific process |
top | Live process monitor |
htop | Better process monitor (install separately) |
kill <pid> | Send SIGTERM (graceful stop) |
kill -9 <pid> | Send SIGKILL (force stop) |
killall node | Kill all processes by name |
lsof -i :3000 | Find what is using port 3000 |
jobs | List background jobs |
bg | Resume job in background |
fg | Bring job to foreground |
command & | Run command in background |
nohup command & | Run and keep running after logout |
Disk and System#
| Command | Description |
|---|
df -h | Disk usage (human-readable) |
du -sh dir/ | Directory size |
du -sh * | sort -rh | Largest items in current dir |
free -h | Memory usage (Linux) |
uname -a | System info |
hostname | Machine name |
uptime | System uptime and load |
date | Current date/time |
cal | Calendar |
Networking#
| Command | Description |
|---|
curl https://example.com | Fetch a URL |
curl -o file.html https://example.com | Download to file |
curl -X POST -d '{"key":"value"}' -H "Content-Type: application/json" url | POST JSON |
wget https://example.com/file.zip | Download a file |
ping example.com | Test connectivity |
ifconfig / ip addr | Show network interfaces |
netstat -tlnp / ss -tlnp | Show listening ports |
ssh user@host | Connect via SSH |
scp file.txt user@host:/path | Copy file to remote |
scp user@host:/path/file.txt . | Copy file from remote |
Archives#
| Command | Description |
|---|
tar -czf archive.tar.gz dir/ | Create gzip archive |
tar -xzf archive.tar.gz | Extract gzip archive |
tar -xzf archive.tar.gz -C /dest | Extract to directory |
zip -r archive.zip dir/ | Create zip archive |
unzip archive.zip | Extract zip |
Text Processing#
# Sort lines
sort file.txt
sort -r file.txt # reverse
sort -n file.txt # numeric sort
sort -u file.txt # unique only
# Unique lines (file must be sorted first)
sort file.txt | uniq
sort file.txt | uniq -c # count occurrences
# Cut columns
cut -d',' -f1,3 data.csv # columns 1 and 3 (comma delimiter)
# Replace text
sed 's/old/new/g' file.txt # replace all occurrences
sed -i 's/old/new/g' file.txt # in-place edit (Linux)
sed -i '' 's/old/new/g' file.txt # in-place edit (macOS — needs empty '')
# Print specific lines
awk '{print $1, $3}' file.txt # columns 1 and 3 (space delimiter)
awk -F',' '{print $1}' data.csv # with custom delimiter
# Count and summarize
cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10
# → top 10 IP addresses in an access log
Keyboard Shortcuts (Terminal)#
| Shortcut | Action |
|---|
Ctrl+C | Cancel current command |
Ctrl+D | Exit shell / close terminal |
Ctrl+Z | Suspend current process |
Ctrl+L | Clear screen |
Ctrl+R | Search command history |
Ctrl+A | Move cursor to start of line |
Ctrl+E | Move cursor to end of line |
Ctrl+W | Delete word before cursor |
Ctrl+U | Delete entire line before cursor |
Tab | Auto-complete file/command name |
!! | Repeat last command |
!$ | Last argument of previous command |
Environment Variables#
echo $HOME # print variable
export MY_VAR="value" # set for current session
echo 'export MY_VAR="value"' >> ~/.zshrc # permanent (Zsh)
echo 'export MY_VAR="value"' >> ~/.bashrc # permanent (Bash)
env # list all env variables
printenv PATH # print specific variable
Common Mistakes#
rm -rf / or rm -rf * — There is no recycle bin in the terminal. Deleted files are gone. Always double-check your path before running rm -rf. Use ls first to preview what will be deleted.
Running commands as root unnecessarily — Using sudo for everything is dangerous. Only use sudo when you actually need elevated permissions (installing packages, changing system files).
Not quoting variables in scripts — rm $FILE can delete the wrong thing if $FILE contains spaces. Always use rm "$FILE" with quotes.