Catalog / macOS Terminal Cheat Sheet

macOS Terminal Cheat Sheet

A comprehensive cheat sheet covering macOS Terminal commands, shortcuts, configurations, and tips for efficient command-line usage.

Basic Navigation & File Management

Navigation Commands

pwd

Print working directory (shows the current directory path).

ls

List directory contents. Use ls -l for detailed list, ls -a to show hidden files, ls -t to sort by modification time.

cd directory

Change directory. Use cd .. to go up one level, cd ~ to go to the home directory, cd - to return to the previous directory.

open .

Opens the current directory in Finder.

pushd directory

Push current directory onto stack and change to new directory.

popd

Pop directory off the stack and change to it.

File Operations

mkdir directory

Create a new directory.

touch file

Create an empty file or update the access and modification times of an existing file.

cp source destination

Copy a file or directory. Use cp -r for recursive copying of directories.

mv source destination

Move or rename a file or directory.

rm file

Remove a file. Use rm -r directory to remove a directory and its contents recursively. Use rm -f to force removal (be careful!).

rmdir directory

Remove an empty directory.

File Viewing

cat file

Display the entire file content.

less file

View file content page by page. Use space to go to the next page, b to go back, q to quit.

head file

Display the first few lines of a file (default 10 lines). Use head -n [number] to specify the number of lines.

tail file

Display the last few lines of a file (default 10 lines). Use tail -n [number] to specify the number of lines. tail -f file to follow the file in real time.

open file

Opens the file with its default application.

file file

Determine file type.

Searching & Text Manipulation

Searching

grep pattern file

Search for a pattern in a file. Use grep -i for case-insensitive search, grep -r for recursive search in directories, grep -v to invert the match.

find directory -name filename

Find files by name within a directory. For example, find . -name "*.txt".

mdfind query

Spotlight search from the command line. For example, mdfind "text content".

locate filename

Find files by name using a pre-built database. You may need to run sudo /usr/libexec/locate.updatedb to update the database first.

Text Manipulation

sed 's/old/new/g' file

Replace all occurrences of ‘old’ with ‘new’ in a file. The g flag means global replacement. Use sed -i to modify the file in-place.

awk '{print $1}' file

Print the first column of each line in a file. $1, $2, etc. refer to the columns, and $0 refers to the entire line.

sort file

Sort the lines in a file. Use sort -n for numerical sorting, sort -r for reverse order.

uniq file

Remove duplicate lines from a sorted file. Use uniq -c to count occurrences.

tr 'a-z' 'A-Z' < file

Convert lowercase characters to uppercase.

cut -d ',' -f 1 file

Cut out sections of each line. -d specifies the delimiter, and -f specifies the field to extract.

System & Process Management

System Information

uname -a

Display system information (kernel name, network node hostname, kernel release, kernel version, machine hardware name, and operating system).

sw_vers

Display macOS software version information.

system_profiler SPSoftwareDataType

Detailed system software information.

df -h

Display disk space usage in a human-readable format.

du -sh directory

Display the disk usage of a directory in a human-readable format.

top

Display a dynamic real-time view of running processes.

Process Management

ps aux

Display all running processes.

kill pid

Terminate a process by its process ID (PID). Use kill -9 pid to forcefully terminate a process.

killall processname

Terminate all processes with the given name.

bg

Move a process to the background.

fg

Move a process to the foreground.

jobs

List active jobs.

Networking

ping hostname

Test network connectivity to a host.

ifconfig

Display network interface configuration.

netstat

Display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

traceroute hostname

Trace the route packets take to a host.

nslookup hostname

Query DNS servers to obtain domain name or IP address information.

Terminal Configuration & Shortcuts

Terminal Configuration

Modify .zshrc or .bash_profile (depending on your shell) to customize your terminal environment.

Common configurations:

  • Aliases: alias shortcut='long command'
  • Environment variables: export VARIABLE_NAME=value
  • Prompt customization: Modifying the PS1 variable.

To apply changes, run source ~/.zshrc or source ~/.bash_profile.

Keyboard Shortcuts

Ctrl+A

Move cursor to the beginning of the line.

Ctrl+E

Move cursor to the end of the line.

Ctrl+K

Cut the line from the cursor to the end.

Ctrl+U

Cut the line from the cursor to the beginning.

Ctrl+Y

Paste the last thing that was cut.

Ctrl+R

Reverse search through command history.

Ctrl+D

Close the terminal or exit the current shell.

Ctrl+C

Interrupt the current process.

Shell Customization

macOS uses Zsh as the default shell. You can change the default shell using chsh -s /bin/bash (for Bash) or chsh -s /bin/zsh (for Zsh).

Oh My Zsh is a popular framework for managing Zsh configurations. Install it with sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"