tips
116 tips Terminal
One command, setting or habit at a time about Terminal: what it does, the line to copy and a caveat or two.
Mac terminal 27
-
Prints the battery cycle count of a MacBook straight from the terminal.
ioreg -rn AppleSmartBattery | grep CycleCount -
Save a set of Terminal windows and tabs under a name and reopen them in one click.
-
Copy your Terminal colours, fonts and shell settings to another Mac with an exported profile file.
-
Explains why the terminal stops responding after Ctrl-S and how to stop that for good.
stty -ixon -
Opens a man page in a separate Mac Terminal window so your prompt stays free.
open x-man-page://rsync -
Puts the complete, escaped path of a Finder item on your command line.
cd /Users/me/Downloads/Old\ Projects\ 2024 -
Names a terminal tab or window from the command line or from a script.
printf '\033]0;%s\007' "deploy running" -
Finds the command you need when you only know what it should do.
man -k checksum -
Wipes the whole scrollback in Terminal on a Mac instead of just the visible screen.
clear && printf '\033[3J' -
Lets the Option key act as Alt for word jumps and other shell shortcuts.
[x] Use Option as Meta key -
Streams the macOS unified log live, filtered to one process or subsystem.
log stream --predicate 'process == "Finder"' --style compact -
Lists the queued print jobs and printer status from the Mac terminal.
lpstat -o -
Creates a file of an exact size, optionally without allocating the disk space.
mkfile -v 100m /tmp/test-100m.bin -
Launches another instance of an app that is already running.
open -n -a Calculator -
Prints the status of the batteries and power sources of a MacBook from Terminal.
pmset -g batt -
Records where a running or frozen app spends its time, as a text call tree.
sample Safari 5 -file ~/Desktop/safari-sample.txt -
Runs a command or an already running process at low background priority.
taskpolicy -b ./convert-all.sh -
Previews which processes a killall command would hit without sending any signal.
killall -s Finder -
Opens files in a specific app from the terminal, without changing your default app.
open -a Preview *.png -
Lists the recent sleep and wake events of your Mac to explain battery drain.
pmset -g log | grep -i wake -
Schedules your Mac to wake, start, sleep or shut down at fixed times.
sudo pmset repeat wakeorpoweron MTWRF 07:45:00 sleep MTWRFSU 23:30:00 -
Installs Rosetta for Intel apps from Terminal, ready for scripts on a new Mac.
softwareupdate --install-rosetta -
Jumps between command prompts in Terminal and selects one command's output.
Command-Up Arrow jump to previous mark -
Converts a plain text file to a PDF from the terminal, using the Mac's print system.
cupsfilter notes.txt > notes.pdf -
Runs your Shortcuts app shortcuts from the command line, with files as input.
shortcuts run "Combine Images" -i ~/Desktop/*.jpg -o ~/Desktop/combined.png -
Plays an audio file through the default output, so a long command can tell you it has finished.
afplay /System/Library/Sounds/Glass.aiff -
Reads text out loud with a system voice, or writes the speech to an audio file.
say -v '?'
Developer setup 13
-
Lists and removes packages that only came along as a dependency and are unused now.
brew autoremove -n -
Starts, stops and lists background services installed with Homebrew.
brew services start redis -
Shows which CPU architectures a binary on your Mac contains.
lipo -archs /opt/homebrew/bin/gh -
Saves every formula, cask and tap you have, ready to install on another Mac.
brew bundle dump --file=~/dotfiles/Brewfile -
Stops brew upgrade from touching one formula you depend on.
brew pin node -
Turns off Homebrew's anonymous usage analytics and shows whether they are on.
brew analytics off -
Sets how often Homebrew updates itself before install and upgrade commands.
export HOMEBREW_AUTO_UPDATE_SECS=604800 -
Adds a folder to the command search path for all users on a Mac.
sudo sh -c 'echo /opt/mytools/bin > /etc/paths.d/mytools' -
Installs Apple's compilers and build tools for the terminal without the full Xcode app.
xcode-select --install -
Makes Tab complete Homebrew commands and installed tools in zsh.
autoload -Uz compinit -
Scans your Mac for common Homebrew problems and suggests fixes.
brew doctor -
Makes Homebrew commands and man pages work in every new zsh login shell.
echo 'eval "$(/opt/homebrew/bin/brew shellenv zsh)"' >> ~/.zprofile -
Includes self-updating and latest-version apps when Homebrew upgrades your casks.
brew outdated --cask --greedy
Files and Spotlight 10
-
Deletes from the command line in a way you can still undo from Finder.
trash old-notes.txt build/ -
Makes a copy of a large file in a moment without using extra disk space.
cp -c ~/VMs/ubuntu.img ~/VMs/ubuntu-before-upgrade.img -
Finds files by one metadata field, such as content type or author, from Terminal.
mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf'" -
Keeps an mdfind search open and updates the match count as files change.
mdfind -live -onlyin ~/Downloads invoice -
Converts documents between txt, html, rtf, docx and other formats from the Terminal, without opening an app.
textutil -convert html report.rtf -
Copies files and folders on macOS and keeps resource forks, extended attributes and permissions.
ditto ~/Projects/site /Volumes/Backup/site -
Shows the web address a downloaded file came from.
mdls -name kMDItemWhereFroms ~/Downloads/example.pdf -
Opens the Quick Look preview of a file straight from the terminal.
qlmanage -p report.pdf -
Tells Time Machine to skip a folder so it stops backing up files you can rebuild.
tmutil addexclusion ~/projects/site/node_modules -
Finds files by name using the Spotlight index, much faster than walking folders.
mdfind -name invoice -onlyin ~/Documents
Text tools 7
-
Filters a log inside less without quitting and running grep.
less /var/log/nginx/error.log -
Changes how wide tab characters look in your terminal for cat and less.
tabs -4 -
Searches a sorted list by prefix, fast, and doubles as a spelling helper.
look recurs -
Writes a pipeline's result back to the file it was reading from.
sort -u list.txt | sponge list.txt -
Squeezes repeated spaces so cut can pick columns from padded output.
df -h | tr -s ' ' | cut -d' ' -f1,5 -
Joins every input line into one line with a chosen separator.
printf '%s\n' alpha beta gamma | paste -sd, - -
Prints the lines that exist in the first sorted file but not in the second.
comm -23 <(sort old.txt) <(sort new.txt)
Time Machine 7
-
Restores a single file or folder from a Time Machine backup without opening the Time Machine browser.
sudo tmutil restore -v "/Volumes/Backup Disk/Backups.backupdb/My Mac/2026-11-30-120000/Macintosh HD - Data/Users/me/Documents/report.pdf" ~/Desktop/ -
Deletes the local Time Machine snapshots for one date from Terminal to free disk space.
sudo tmutil deletelocalsnapshots 2026-11-30-120000 -
Tells you if a backup is running and how far along it is.
tmutil status -
Lists files that were added, changed or removed compared with a backup.
tmutil compare -
Shows which backup disks Time Machine uses and which one got the latest backup.
tmutil destinationinfo -
Lists the hidden hourly Time Machine snapshots on your Mac's own disk.
tmutil listlocalsnapshots / -
Adds an SMB share as a Time Machine backup disk with one Terminal command.
sudo tmutil setdestination -a -p smb://[email protected]/timemachine
Windows 7
-
Puts two or more shells side by side in one Windows Terminal tab.
Alt+Shift+Plus new pane to the right (vertical split) -
Makes Up and Down in PowerShell return only commands that start with your text.
Set-PSReadLineKeyHandler -Chord UpArrow -Function HistorySearchBackward -
Creates a directory junction so a folder appears under a second path.
mklink /J "C:\Games\SaveData" "D:\Storage\SaveData" -
Prints a few lines before and after every match in a text file.
Select-String -Path C:\Temp\app.log -Pattern 'error' -Context 2,3 -
Finds files by name under a folder, or shows which copy of a program is on your PATH.
where /r C:\Users\Public *.log -
Shows PowerShell's history-based command suggestions as a pick list instead of grey text.
Set-PSReadLineOption -PredictionViewStyle ListView -
Opens a Windows Terminal window that slides down from the top and hides again on a key.
wt -w _quake
Display and control 6
-
Turns the display off right away while everything keeps running.
pmset displaysleepnow -
Toggles macOS dark mode with a command you can bind or schedule.
osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to not dark mode' -
Makes grep print each match immediately when it feeds another command.
tail -f /var/log/syslog | grep --line-buffered ERROR | cut -c1-100 -
Restores a terminal that was scrambled by binary output.
stty sane -
Prints the number of rows and columns of the current terminal window.
read -r rows cols <<< "$(stty size)" -
Keeps wide output on one line per row and lets you scroll sideways.
ps aux | less -S
Network and DNS 5
-
Queries the Mac's Directory Service cache for a host name or an IP address.
dscacheutil -q host -a name example.com -
Prints the address, router, DNS servers and lease time your Mac got from DHCP.
ipconfig getpacket en0 -
Lists every process with its live network traffic, one line per app.
nettop -P -d -t external -
Saves separate sets of network settings and switches between them in one step.
networksetup -listlocations -
Shows which devices on your local network advertise SSH, file sharing or web pages.
dns-sd -B _ssh._tcp
Recording and logging 5
-
Records a terminal session so you can replay it later at its original speed.
script -r demo.rec -
Records the screen for a fixed number of seconds without opening any app.
screencapture -V 20 -k ~/Desktop/demo.mov -
Adds a time to every line of command output, for slow-step hunting.
sudo apt install moreutils -
Shows output on screen and appends the same output to a log file.
./backup.sh 2>&1 | tee -a backup.log -
Saves a full terminal session, input and output, to a text file.
script -q -a session.log
Settings and services 4
-
Shows the names, versions and install dates of past system updates.
softwareupdate --history -
Restarts a stuck background job through launchd instead of killing it blindly.
launchctl kickstart -kp gui/$(id -u)/com.example.agent -
Connects, checks and disconnects a macOS VPN service from Terminal or a script.
scutil --nc list -
Shows which preference key a setting changes, so you can script it later.
defaults read > ~/before.txt
Access and security 3
-
Uses your fingerprint for sudo, in a way that survives macOS updates.
sed 's/^#auth/auth/' /etc/pam.d/sudo_local.template | sudo tee /etc/pam.d/sudo_local -
Removes the quarantine mark that makes Gatekeeper block a downloaded tool.
xattr -d com.apple.quarantine ./tool -
Protects passwords and keys you type in Terminal from keyloggers and snooping apps.
Terminal (menu bar) > Secure Keyboard Entry
Keyboard and desktop 3
-
Remaps a key on the Mac keyboard from Terminal, until the next restart.
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' -
Shows the raw escape sequence of any key, which you need for key bindings.
^[[1;5C -
Makes the Caps Lock key work as Control, handy for terminal shortcuts.
Caps Lock key: Control
Screenshots and images 3
-
Turns iPhone HEIC photos into JPEG files that every website accepts.
sips -s format jpeg -s formatOptions 80 *.heic --out jpg/ -
Shrinks images to a maximum size from the terminal without opening an editor.
sips -Z 1600 photo.jpg --out photo-small.jpg -
Captures one window as a clean image without the shadow around it.
screencapture -o -w ~/Desktop/settings.png
Automation 2
-
Shows a macOS notification from a script or after a long command.
osascript -e 'display notification "Backup finished" with title "Terminal" sound name "Glass"' -
Runs JavaScript instead of AppleScript to control Mac apps from Terminal.
osascript -l JavaScript -e 'Application("Mail").inbox.messages.whose({subject: {_contains: "invoice"}}).subject()'
Clipboard 2
-
Sends the result of any PowerShell command straight to the clipboard.
Get-Content $HOME\.ssh\id_ed25519.pub | Set-Clipboard -
Pipes terminal output to your clipboard and pastes clipboard text into commands.
cat ~/.ssh/id_ed25519.pub | pbcopy
Disks and names 2
-
Shrinks a sparse bundle disk image after you deleted files inside it.
hdiutil compact ~/Documents/Projects.sparsebundle -
Creates a compressed disk image from a folder, ready to share, mount or archive.
hdiutil create -volname "Project" -srcfolder ~/Documents/project project.dmg
Shell basics 2
-
Adds colour to script messages in a way that stays quiet on dumb terminals.
red=$(tput setaf 1); green=$(tput setaf 2) -
Marks a running background job so it keeps going after you close the shell.
disown -h %1
Config and keys 1
-
Shows where PowerShell keeps its startup script and how to create it safely.
New-Item -ItemType File -Path $PROFILE -Force
Files and media 1
-
Converts audio, for example WAV to AAC, with a converter that already comes with macOS.
afconvert -f m4af -d aac -b 256000 recording.wav recording.m4a
Inspect and logs 1
-
Keeps only the JSON items that match a condition and prints one field.
echo '[{"n":"a","up":true},{"n":"b","up":false}]' | jq -r '.[] | select(.up) | .n'
Python one-liners 1
-
Picks a random word, whole number or decimal straight from the terminal, no script needed.
python3 -m random pizza sushi pasta "leftovers again"
Shortcuts 1
-
Places the cursor exactly where you click on a long command line.
^ Option-click here
Slash commands 1
-
Adds a Shift+Enter newline binding to terminals where Shift+Enter sends your prompt instead.
/terminal-setup
Standard library 1
-
Prints a text calendar for a year or month without installing anything.
python3 -m calendar -m 4 2027
System 1
-
Starts the built-in Midnight Commander to browse and copy files between shares and disks from the terminal.
mc
Nothing in this filter yet.