Essential macOS Terminal Commands

Unlock the power of the command line! Terminal gives you direct access to your Mac's Unix foundation.

⚠️ Warning: Terminal commands have power to modify system files. Always double-check commands before pressing Enter. When in doubt, don't run it!

Opening Terminal

1. Navigation Commands

pwd

Print Working Directory - Shows your current folder location

ls

List - Shows files in current directory

cd ~/Desktop

Change Directory - Navigate to Desktop

2. File Operations

mkdir foldername

Make Directory - Creates new folder

touch filename.txt

Touch - Creates empty file

cp file.txt newfile.txt

Copy - Duplicates a file

mv oldname.txt newname.txt

Move/Rename - Moves or renames file

rm filename.txt

Remove - Deletes file (permanent!)

3. Viewing Files

cat filename.txt

Concatenate - Displays file contents

less filename.txt

Less - View file page by page (press Q to quit)

head -n 10 file.txt

Head - Show first 10 lines of file

tail -n 20 file.txt

Tail - Show last 20 lines of file

4. System Information

top

Top - Shows running processes (like Activity Monitor)

Press Q to quit

df -h

Disk Free - Shows disk space usage

du -sh *

Disk Usage - Shows size of each item in current folder

system_profiler SPHardwareDataType

System Profiler - Hardware information

5. Network Commands

ping google.com

Ping - Test network connectivity

Press Control + C to stop

ifconfig

Interface Config - Shows network interface info

netstat -an

Network Statistics - Shows network connections

6. Process Management

ps aux

Process Status - Lists all running processes

kill 1234

Kill - Stops process with ID 1234

7. File Permissions

chmod 755 script.sh

Change Mode - Modify file permissions

chown user:staff file.txt

Change Owner - Change file ownership

8. Search Commands

find ~/Desktop -name "*.txt"

Find - Search for files by name

grep "search term" file.txt

Grep - Search inside files for text

9. Useful Shortcuts

clear

Clear - Clears terminal screen (or: ⌘ + K)

history

History - Shows command history

!!

Bang Bang - Repeats last command

sudo command

Sudo - Run command as administrator

Will ask for your password

10. macOS Specific Commands

open .

Open - Opens current folder in Finder

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

Defaults - Show hidden files in Finder

Change TRUE to FALSE to hide again

pmset -g batt

Power Management - Shows battery status

caffeinate

Caffeinate - Prevents Mac from sleeping

Press Control + C to stop

11. Advanced Maintenance

sudo purge

Purge - Clears RAM cache (frees memory)

sudo periodic daily weekly monthly

Periodic - Runs system maintenance scripts

diskutil list

Disk Utility - Lists all disks

diskutil verifyVolume /

Verify Volume - Checks disk for errors

12. Tips & Tricks

Tab Completion: Start typing and press Tab to auto-complete

Arrow Up/Down: Cycle through previous commands

Control + C: Cancel current command

Control + A: Move cursor to beginning of line

Control + E: Move cursor to end of line

Control + U: Delete everything before cursor

13. Combining Commands

ls -la | grep "Desktop"

Pipe - Sends output of first command to second

ls -la > files.txt

Redirect - Saves output to file

command1 && command2

AND - Runs second command only if first succeeds

Useful One-Liners

# Find large files (over 1GB)
find ~ -size +1G
# Show top 10 largest folders
du -sh * | sort -rh | head -10
# Count files in directory
ls -1 | wc -l
# Show your IP address
curl ifconfig.me
# Download file
curl -O https://example.com/file.zip

Getting Help

man ls

Manual - Shows manual page for command

Press Q to quit manual

command --help

Help - Shows command options

⚠️ Dangerous Commands to Avoid:
  • rm -rf / - Deletes everything (NEVER run this!)
  • :(){ :|:& };: - Fork bomb (crashes system)
  • Any command from untrusted sources
Always understand what a command does before running it!
← Back to Guides