Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix A: Command Reference

This appendix provides a quick reference to essential commands for macOS. Commands are organized by category for easy lookup. For detailed information on any command, use man command-name.

Legend

  • $ - Run as regular user
  • # - Requires sudo (administrator privileges)
  • [optional] - Optional parameter
  • <required> - Required parameter

File and Directory Operations

CommandDescriptionExample
pwdPrint working directory$ pwd
cd <dir>Change directory$ cd ~/Documents
cd -Return to previous directory$ cd -
lsList directory contents$ ls -la
ls -laLong format with hidden files$ ls -la ~/
ls -lahHuman-readable sizes$ ls -lah
ls -ltSort by modification time$ ls -lt
treeDirectory tree (install via Homebrew)$ tree -L 2

File Operations

CommandDescriptionExample
cp <src> <dst>Copy file$ cp file.txt backup.txt
cp -r <src> <dst>Copy directory recursively$ cp -r dir1 dir2
cp -a <src> <dst>Archive mode (preserve attributes)$ cp -a /source /dest
mv <src> <dst>Move/rename file$ mv old.txt new.txt
rm <file>Remove file$ rm unwanted.txt
rm -r <dir>Remove directory recursively$ rm -r old_dir
rm -rf <dir>Force remove (dangerous!)$ rm -rf temp/
mkdir <dir>Create directory$ mkdir newdir
mkdir -p <path>Create nested directories$ mkdir -p a/b/c
rmdir <dir>Remove empty directory$ rmdir emptydir
touch <file>Create file or update timestamp$ touch newfile.txt
ln -s <target> <link>Create symbolic link$ ln -s /path/to/file link
ditto <src> <dst>Copy preserving macOS metadata$ ditto src dst

File Information

CommandDescriptionExample
file <file>Determine file type$ file document.pdf
stat <file>Display file status$ stat file.txt
du -sh <path>Disk usage summary$ du -sh ~/Downloads
du -h -d 1Disk usage one level deep$ du -h -d 1 ~/
wc -l <file>Count lines in file$ wc -l file.txt
mdls <file>View Spotlight metadata$ mdls photo.jpg

Permissions

CommandDescriptionExample
chmod <mode> <file>Change permissions$ chmod 755 script.sh
chmod +x <file>Make executable$ chmod +x run.sh
chmod -R <mode> <dir>Recursive permissions$ chmod -R 644 docs/
chown <user> <file>Change owner# chown admin file.txt
chown -R <user>:<group>Recursive ownership# chown -R www:www html/
chgrp <group> <file>Change group$ chgrp staff file.txt
ls -leList with ACLs$ ls -le
chmod +a <acl>Add ACL entry$ chmod +a "user:joe:allow read" file

Text Processing

Viewing Files

CommandDescriptionExample
cat <file>Display file contents$ cat file.txt
cat -n <file>Display with line numbers$ cat -n script.sh
head <file>First 10 lines$ head file.txt
head -n 20 <file>First N lines$ head -n 20 log.txt
tail <file>Last 10 lines$ tail file.txt
tail -n 50 <file>Last N lines$ tail -n 50 log.txt
tail -f <file>Follow file (live updates)$ tail -f /var/log/system.log
less <file>Page through file$ less largefile.txt
more <file>Simple pager$ more file.txt

Searching

CommandDescriptionExample
grep <pattern> <file>Search for pattern$ grep "error" log.txt
grep -i <pattern>Case-insensitive search$ grep -i "warning" file
grep -r <pattern> <dir>Recursive search$ grep -r "TODO" src/
grep -n <pattern>Show line numbers$ grep -n "func" code.py
grep -v <pattern>Invert match (exclude)$ grep -v "debug" log
grep -E <regex>Extended regex$ grep -E "err(or)?s?" log
grep -l <pattern>List matching files only$ grep -l "main" *.c
grep -c <pattern>Count matches$ grep -c "error" log.txt
mdfind <query>Spotlight search$ mdfind "project report"
mdfind -name <name>Search by filename$ mdfind -name "config.yaml"

Text Manipulation

CommandDescriptionExample
sort <file>Sort lines$ sort names.txt
sort -n <file>Numeric sort$ sort -n numbers.txt
sort -r <file>Reverse sort$ sort -r file.txt
sort -u <file>Sort and remove duplicates$ sort -u list.txt
uniqRemove adjacent duplicates$ sort file | uniq
uniq -cCount occurrences$ sort file | uniq -c
cut -d: -f1Extract field$ cut -d: -f1 /etc/passwd
cut -c1-10Extract characters$ cut -c1-10 file.txt
tr 'a-z' 'A-Z'Translate characters$ echo "hi" | tr 'a-z' 'A-Z'
tr -d '\r'Delete characters$ tr -d '\r' < file.txt
sed 's/old/new/'Substitute first match$ sed 's/foo/bar/' file
sed 's/old/new/g'Substitute all matches$ sed 's/foo/bar/g' file
awk '{print $1}'Print first field$ awk '{print $1}' file
awk -F: '{print $1}'Custom delimiter$ awk -F: '{print $1}' passwd

Comparison

CommandDescriptionExample
diff <file1> <file2>Compare files$ diff old.txt new.txt
diff -u <f1> <f2>Unified diff format$ diff -u old new
diff -r <dir1> <dir2>Compare directories$ diff -r src1 src2
comm <f1> <f2>Compare sorted files$ comm list1 list2
cmp <f1> <f2>Byte-by-byte compare$ cmp file1 file2

Finding Files

CommandDescriptionExample
find <path> -name <pattern>Find by name$ find . -name "*.txt"
find <path> -type fFind files only$ find . -type f
find <path> -type dFind directories only$ find /var -type d
find <path> -mtime -7Modified in last 7 days$ find . -mtime -7
find <path> -size +100MFiles larger than 100MB$ find . -size +100M
find <path> -emptyFind empty files/dirs$ find . -empty
find <path> -exec cmd {} \;Execute command on results$ find . -name "*.log" -exec rm {} \;
locate <pattern>Search locate database$ locate nginx.conf
which <command>Show command path$ which python
whereis <command>Locate binary/man/source$ whereis ls
type <command>Describe command type$ type cd

Archiving and Compression

CommandDescriptionExample
tar -cvf <archive> <files>Create tar archive$ tar -cvf backup.tar dir/
tar -xvf <archive>Extract tar archive$ tar -xvf backup.tar
tar -czvf <archive> <files>Create gzipped tar$ tar -czvf backup.tar.gz dir/
tar -xzvf <archive>Extract gzipped tar$ tar -xzvf backup.tar.gz
tar -cjvf <archive> <files>Create bzip2 tar$ tar -cjvf backup.tar.bz2 dir/
tar -xjvf <archive>Extract bzip2 tar$ tar -xjvf backup.tar.bz2
tar -tvf <archive>List tar contents$ tar -tvf backup.tar
gzip <file>Compress with gzip$ gzip file.txt
gunzip <file.gz>Decompress gzip$ gunzip file.txt.gz
zip -r <archive> <dir>Create zip archive$ zip -r archive.zip dir/
unzip <archive>Extract zip archive$ unzip archive.zip
unzip -l <archive>List zip contents$ unzip -l archive.zip

Process Management

Viewing Processes

CommandDescriptionExample
ps auxList all processes$ ps aux
ps aux | grep <name>Find specific process$ ps aux | grep nginx
pgrep <name>Get PIDs by name$ pgrep -l Safari
topInteractive process viewer$ top
htopEnhanced top (install via Homebrew)$ htop
Activity MonitorGUI process managerOpen from Spotlight

Process Control

CommandDescriptionExample
kill <PID>Terminate process$ kill 1234
kill -9 <PID>Force kill process$ kill -9 1234
killall <name>Kill by name$ killall Safari
pkill <pattern>Kill by pattern$ pkill -f "python script"
<command> &Run in background$ long_task &
jobsList background jobs$ jobs
fgBring to foreground$ fg %1
bgContinue in background$ bg %1
nohup <cmd> &Run immune to hangups$ nohup ./script.sh &
Ctrl+ZSuspend current process
Ctrl+CInterrupt current process

Resource Usage

CommandDescriptionExample
vm_statVirtual memory statistics$ vm_stat
iostatI/O statistics$ iostat
fs_usageFilesystem activity# fs_usage -f filesys
sample <PID> <secs>Sample process$ sample Safari 5

System Information

CommandDescriptionExample
uname -aSystem information$ uname -a
sw_versmacOS version$ sw_vers
system_profilerDetailed system info$ system_profiler SPHardwareDataType
hostnameShow hostname$ hostname
whoamiCurrent username$ whoami
idUser ID and groups$ id
uptimeSystem uptime$ uptime
dateCurrent date/time$ date
calCalendar$ cal
df -hDisk free space$ df -h
diskutil listList disks and partitions$ diskutil list
diskutil info <disk>Disk information$ diskutil info disk0
sysctl -aAll kernel parameters$ sysctl -a
sysctl hw.memsizeTotal RAM$ sysctl hw.memsize
sysctl machdep.cpuCPU information$ sysctl machdep.cpu
ioreg -lIOKit registry$ ioreg -l
nvram -pNVRAM variables$ nvram -p

Network Commands

Network Information

CommandDescriptionExample
ifconfigNetwork interface config$ ifconfig
ifconfig en0Specific interface$ ifconfig en0
ipconfig getifaddr en0Get IP address$ ipconfig getifaddr en0
networksetup -listallnetworkservicesList services$ networksetup -listallnetworkservices
networksetup -getinfo "Wi-Fi"Service info$ networksetup -getinfo "Wi-Fi"
scutil --dnsDNS configuration$ scutil --dns
scutil --proxyProxy configuration$ scutil --proxy
netstat -anNetwork connections$ netstat -an
netstat -rnRouting table$ netstat -rn
lsof -iOpen network connections$ lsof -i
lsof -i :80Connections on port 80$ lsof -i :80

Connectivity Testing

CommandDescriptionExample
ping <host>Test connectivity$ ping apple.com
ping -c 5 <host>Send 5 pings$ ping -c 5 google.com
traceroute <host>Trace route to host$ traceroute apple.com
mtr <host>Combined ping/traceroute$ mtr google.com
curl <url>Transfer URL$ curl https://api.example.com
curl -I <url>Fetch headers only$ curl -I https://apple.com
curl -o <file> <url>Download to file$ curl -o file.zip https://url
wget <url>Download file (Homebrew)$ wget https://example.com/file
nc -zv <host> <port>Test port connectivity$ nc -zv google.com 443
nslookup <host>DNS lookup$ nslookup apple.com
dig <host>DNS lookup (detailed)$ dig apple.com
host <host>DNS lookup (simple)$ host apple.com
arp -aARP table$ arp -a

Wi-Fi

CommandDescriptionExample
networksetup -setairportpower en0 onTurn Wi-Fi on$ networksetup -setairportpower en0 on
networksetup -setairportpower en0 offTurn Wi-Fi off$ networksetup -setairportpower en0 off
networksetup -getairportnetwork en0Current network$ networksetup -getairportnetwork en0

Disk and Volume Management

CommandDescriptionExample
diskutil listList all disks$ diskutil list
diskutil info <disk>Disk information$ diskutil info disk0
diskutil mount <vol>Mount volume$ diskutil mount disk2s1
diskutil unmount <vol>Unmount volume$ diskutil unmount disk2s1
diskutil eject <disk>Eject disk$ diskutil eject disk2
diskutil verifyVolume <vol>Verify filesystem# diskutil verifyVolume /
diskutil repairVolume <vol>Repair filesystem# diskutil repairVolume disk1
diskutil apfs listList APFS volumes$ diskutil apfs list
hdiutil attach <dmg>Mount disk image$ hdiutil attach image.dmg
hdiutil detach <vol>Detach disk image$ hdiutil detach /Volumes/Image
hdiutil createCreate disk image$ hdiutil create -size 1g -fs APFS -volname "MyDisk" disk.dmg
tmutilTime Machine utility$ tmutil listbackups

User Management

CommandDescriptionExample
whoamiCurrent user$ whoami
idUser/group IDs$ id
groupsGroup memberships$ groups
dscl . -list /UsersList all users$ dscl . -list /Users
dscl . -list /GroupsList all groups$ dscl . -list /Groups
dscl . -read /Users/<user>User details$ dscl . -read /Users/admin
dscacheutil -q userQuery user cache$ dscacheutil -q user
sysadminctl -addUserAdd user# sysadminctl -addUser joe -password pass
sysadminctl -deleteUserDelete user# sysadminctl -deleteUser joe
sudo -sRoot shell$ sudo -s
su - <user>Switch user$ su - otheruser

Service and Process Management

launchctl

CommandDescriptionExample
launchctl listList loaded services$ launchctl list
launchctl list | grep <name>Find service$ launchctl list | grep apache
launchctl load <plist>Load service (legacy)$ launchctl load ~/Library/LaunchAgents/job.plist
launchctl unload <plist>Unload service (legacy)$ launchctl unload ~/Library/LaunchAgents/job.plist
launchctl bootstrap gui/<uid> <plist>Bootstrap service$ launchctl bootstrap gui/501 job.plist
launchctl bootout gui/<uid> <plist>Remove service$ launchctl bootout gui/501 job.plist
launchctl kickstartStart service$ launchctl kickstart gui/501/com.example.agent
launchctl kill <sig> <service>Send signal# launchctl kill SIGTERM system/com.example.daemon
launchctl print systemPrint system domain$ launchctl print system
launchctl print gui/501Print user domain$ launchctl print gui/501

Homebrew Services

CommandDescriptionExample
brew services listList services$ brew services list
brew services start <name>Start service$ brew services start postgresql
brew services stop <name>Stop service$ brew services stop postgresql
brew services restart <name>Restart service$ brew services restart nginx
brew services run <name>Run once (no restart)$ brew services run redis

Package Management (Homebrew)

CommandDescriptionExample
brew install <pkg>Install package$ brew install wget
brew uninstall <pkg>Uninstall package$ brew uninstall wget
brew upgradeUpgrade all packages$ brew upgrade
brew upgrade <pkg>Upgrade specific package$ brew upgrade node
brew updateUpdate Homebrew$ brew update
brew search <term>Search packages$ brew search python
brew info <pkg>Package information$ brew info python
brew listList installed packages$ brew list
brew list --caskList installed casks$ brew list --cask
brew outdatedShow outdated packages$ brew outdated
brew cleanupRemove old versions$ brew cleanup
brew doctorDiagnose issues$ brew doctor
brew deps <pkg>Show dependencies$ brew deps node
brew uses --installed <pkg>What uses this package$ brew uses --installed openssl
brew install --cask <app>Install GUI app$ brew install --cask firefox

macOS-Specific Commands

Clipboard

CommandDescriptionExample
pbcopyCopy to clipboard$ cat file.txt | pbcopy
pbpastePaste from clipboard$ pbpaste > output.txt
pbcopy < file.txtCopy file contents$ pbcopy < ~/.ssh/id_rsa.pub

Launching and Opening

CommandDescriptionExample
open <file>Open with default app$ open document.pdf
open -a <app> <file>Open with specific app$ open -a "Visual Studio Code" .
open .Open current dir in Finder$ open .
open <url>Open URL in browser$ open https://apple.com
open -R <file>Reveal in Finder$ open -R file.txt

System

CommandDescriptionExample
say <text>Text-to-speech$ say "Hello world"
screencaptureTake screenshot$ screencapture screen.png
screencapture -cScreenshot to clipboard$ screencapture -c
pmset -gPower management status$ pmset -g
pmset -g battBattery status$ pmset -g batt
caffeinatePrevent sleep$ caffeinate -t 3600
softwareupdate -lList software updates$ softwareupdate -l
softwareupdate -iaInstall all updates# softwareupdate -ia
defaults readRead preferences$ defaults read com.apple.finder
defaults writeWrite preferences$ defaults write com.apple.finder ShowHiddenFiles -bool true
osascript -eRun AppleScript$ osascript -e 'display notification "Done"'

Spotlight

CommandDescriptionExample
mdfind <query>Spotlight search$ mdfind "meeting notes"
mdfind -name <name>Search by name$ mdfind -name "config.yaml"
mdfind -onlyin <dir> <query>Search in directory$ mdfind -onlyin ~/Documents "report"
mdls <file>File metadata$ mdls photo.jpg
mdutil -s /Spotlight status$ mdutil -s /
mdutil -E /Rebuild Spotlight index# mdutil -E /

Logging and Debugging

CommandDescriptionExample
log showView unified log$ log show --last 1h
log show --predicateFilter logs$ log show --predicate 'process == "Safari"'
log streamReal-time log stream$ log stream --level debug
consoleOpen Console.app$ open -a Console
syslogLegacy system log (deprecated)$ syslog
dmesgKernel messages$ dmesg
system_profiler SPLogsDataTypeSystem logs info$ system_profiler SPLogsDataType

Security Commands

CommandDescriptionExample
csrutil statusSIP status$ csrutil status
spctl --statusGatekeeper status$ spctl --status
spctl -a -v <app>Verify app signature$ spctl -a -v /Applications/Safari.app
codesign -dv <app>Code signature details$ codesign -dv /Applications/Safari.app
codesign -s <identity> <file>Sign code$ codesign -s "Developer ID" myapp
security list-keychainsList keychains$ security list-keychains
security find-identity -vList signing identities$ security find-identity -v
security find-generic-passwordFind password$ security find-generic-password -s "service"
fdesetup statusFileVault status$ fdesetup status
sudo spctl --master-disableDisable Gatekeeper# spctl --master-disable

Remote Access

CommandDescriptionExample
ssh <user>@<host>SSH connection$ ssh admin@server.local
ssh -i <key> <user>@<host>SSH with key$ ssh -i ~/.ssh/mykey user@host
ssh -L <local>:<remote>SSH tunnel$ ssh -L 8080:localhost:80 user@host
scp <src> <user>@<host>:<dst>Secure copy$ scp file.txt user@host:/path/
scp -r <dir> <user>@<host>:<dst>Recursive copy$ scp -r dir/ user@host:/path/
rsync -avz <src> <dst>Sync files$ rsync -avz dir/ user@host:/path/
sftp <user>@<host>SFTP session$ sftp admin@server.local

Shell Built-ins and History

CommandDescriptionExample
historyShow command history$ history
history | grep <term>Search history$ history | grep git
!!Repeat last command$ !!
!<n>Run command N from history$ !42
!<string>Run last command starting with$ !git
Ctrl+RReverse search history
aliasShow aliases$ alias
alias <name>=<cmd>Create alias$ alias ll='ls -la'
unalias <name>Remove alias$ unalias ll
export <var>=<val>Set environment variable$ export PATH="/usr/local/bin:$PATH"
envShow environment$ env
printenvPrint environment variables$ printenv
echo $<var>Print variable$ echo $HOME

Getting Help

CommandDescriptionExample
man <command>Manual page$ man ls
man -k <term>Search manual pages$ man -k network
apropos <term>Same as man -k$ apropos copy
<command> --helpCommand help$ git --help
<command> -hShort help$ grep -h
whatis <command>One-line description$ whatis curl
info <command>GNU info pages$ info bash

Quick Reference: Keyboard Shortcuts in Commands

ShortcutAction
Ctrl+CCancel/interrupt
Ctrl+DEOF/logout
Ctrl+ZSuspend process
Ctrl+LClear screen
Ctrl+ABeginning of line
Ctrl+EEnd of line
Ctrl+UClear line before cursor
Ctrl+KClear line after cursor
Ctrl+WDelete word before cursor
Ctrl+RReverse search history
TabAuto-complete
Tab TabShow completions