The Mac Terminal Commands Cheat Sheet
The Mac Terminal Commands Cheat Sheet
The Mac Terminal Commands Cheat Sheet
COMMAND ACTION
Shortcuts
Tab Auto-complete file and folder names
Go to the beginning of the line you're currently typing
Ctrl + A
on
Ctrl + E Go to the end of the line you're currently typing on
Ctrl + U Clear the line before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + W Delete the word before the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor
Ctrl + L Clear the screen
Ctrl + C Kill whatever you're running
Ctrl + D Exit the current shell
Option + → Move cursor one word forward
Option + ← Move cursor one word backward
Ctrl + F Move cursor one character forward
Ctrl + B Move cursor one character backward
Ctrl + Y Paste whatever was cut by the last command
Puts whatever you're running into a suspended
Ctrl + Z
background process
Ctrl + _ Undo the last command
Option + Shift + Cmd + C Copy plain text
Shift + Cmd + V Paste the selection
exit End a shell session
Basics
/ (Forward Slash) Top level directory
. (Single Period) Current directory
.. (Double Period) Parent directory
~ (Tilde) Home directory
Run command with the security privileges of the super
sudo [command]
user
nano [file] Opens the Terminal editor
open [file] Opens a file
[command] -h Get help about a command
man [command] Show the help manual of the command
Change Directory
cd Home directory
cd [folder] Change directory, e.g. cd Documents
cd ~ Home directory
COMMAND ACTION
cd/ Root of the drive
cd - Previous directory or folder you last browsed
pwd Show your working directory
cd.. Move up to the parent directory
cd../.. Move up two levels
List Directory Contents
Display the name of files and subdirectories in the
ls
directory
ls -C Force multi-column output of the listing
List all entries including those with .(period) and ..
ls -a
(double period)
ls -1 Output the list of files in one entry per line format
Display a / (slash) immediately after each path that is a
ls -F directory, * (asterisk) after executable programs or
scripts, and @ after a symbolic link
ls -S Sort files or entries by size
List in a long format. Includes file mode, owner and
ls -l group name, date and time file was modified,
pathname, and more
ls -l / List of the file system from root with symbolic links
ls -lt List the files sorted by time modified (most recent first)
Long listing with human readable file sizes in KB, MB,
ls -lh
or GB
ls -lo List the file names with size, owner, and flags
ls -la List detailed directory contents, including hidden files
File Size and Disk Space
du List usage for each subdirectory and its contents
du -sh [folder] Human readable output of all files in a directory
du -s Display an entry for each specified file
List files and folders, totaling the size including the
du -sk* | sort -nr subfolders. Replace sk* with sm* to list directories in
MB
df -h Calculate your system's free disk space
Calculate free disk space in powers of 1,000 (as
df -H
opposed to 1,024)
File and Directory Management
mkdir <dir> Create new folder named <dir>
mkdir -p <dir>/<dir> Create nested folders
mkdir <dir1> <dir2> <dir3> Create several folders at once
mkdir "<dir>" Create a folder with a space in the filename
rmdir <dir> Delete a folder (only works on empty folders)
rm -R <dir> Delete a folder and its contents
COMMAND ACTION
touch <file> Create a new file without any extension
cp <file> <dir> Copy a file to the folder
cp <file> <newfile> Copy a file to the current folder
cp <file>~/<dir>/<newfile> Copy a file to the folder and rename the copied file
Copy a folder to a new folder with spaces in the
cp -R <dir> <"new dir">
filename
Prompts you before copying a file with a warning
cp -i <file><dir>
overwrite message
cp <file1> <file2> <file3>/Users/<dir> Copy multiple files to a folder
Copy the contents of a folder to new folder. In here "-
ditto -V [folder path][new folder]
V" print a line of status for every file copied
Delete a file (This deletes the file permanently; use with
rm <file>
caution.)
rm -i <file> Delete a file only when you give confirmation
rm -f <file> Force removal without confirmation
rm <file1> <file2> <file3> Delete multiple files without any confirmation
mv <file> <newfilename> Move/rename
Move a file to the folder, possibly by overwriting an
mv <file> <dir>
existing file
mv -i <file> <dir> Optional -i flag to warn you before overwriting the file
Move all PNG files from current folder to a different
mv *.png ~/<dir>
folder
Command History
Ctrl + R Search through previously used commands
Shows the previous commands you've typed. Add a
history n
number to limit to the last n items
![value] Execute the last command typed that starts with a value
!! Execute the last command typed
Permissions
ls -ld Display the default permission for a home directory
Display the read, write, and access permission of a
ls -ld/<dir>
particular folder
chmod 755 <file> Change the permission of a file to 755
Change the permission of a folder (and its contents) to
chmod -R 600 <dir>
600
Change the ownership of a file to user and group. Add -
chown <user>:<group> <file>
R to include folder contents
Processes
Output currently running processes. Here, a shows
ps -ax processes from all users and x shows processes that are
not connected with the Terminal
ps -aux Shows all the processes with %cpu, %mem, page in,
COMMAND ACTION
PID, and command
Display live information about currently running
top
processes
Display processes sorted by CPU usage, updating every
top -ocpu -s 5
5 seconds
top -o rsize Sort top by memory usage
Quit process with ID <PID>. You'll see PID as a
kill PID
column in the Activity Monitor
ps -ax | grep <appname> Find a process by name or PID
Network
ping <host> Ping host and display status
whois <domain> Output whois info for a domain
curl -O <url/to/file> Download file via HTTP, HTTPS, or FTP
Establish SSH connection to <host> with user
ssh <username>@<host>
<username>
scp <file><user>@<host>:/remote/path Copy <file> to a remote <host>
View a list of all devices on your local network. It will
arp -a
show you the IP and MAC address of all the devices
ifconfig en0 View your device IP and MAC address
Identify the path and the hops traversed by the packets
traceroute [hostname]
from your device to the destination address
Homebrew
brew doctor Check brew for potential problems
brew help List of useful homebrew formula and cask commands
brew install <formula>|<cask> Install a formula or cask
brew uninstall <formula>|cask> Uninstall a formula or cask
brew list --formula List only installed formulas
brew list --cask List only installed cask
brew deps <formula>|<cask> List all the dependencies of a formula or cask
brew search text|/regex/ Search formula or cask through regex
brew upgrade <formula>|<cask> Upgrade the formula or cask
brew outdated <formula>|<cask> Search for outdated formula or cask
brew outdated --formula Search for outdated formula
brew outdated --cask Search for outdated cask
brew pin [installed_formula] Pin a formula from getting upgraded
brew unpin [installed_formula] Unpin to upgrade a package
Remove stale lock files and outdated packages for all
brew cleanup
formula and casks.
Environment Variable or Path
Display a list of currently set environment variables.
printenv
Also tells you which shell you're using
$echo Tells the terminal to print something and show it to you
COMMAND ACTION
Check the value of the PATH variable which storea a
echo $PATH
list of directories with executable files
echo $PATH >path.txt Export the path directory to a text file
Execute a program via terminal only in your current
export PATH=$PATH:absolute/path
session. If you use a program regularly, add the path to
to/program/
shell configuration file.
Search
Find all files named <file> inside <dir>. Use wildcards
find <dir> -name <"file">
(*) to search for parts of filenames
Output all occurrences of <text> inside <file> (add -i
grep "<text>" <file>
for case insensitivity)
grep -rl "<text>" <dir> Search for all files containing <text> inside <dir>
Output
cat <file> Output the content of <file>
Output the contents of <file> using the less command
less <file>
that supports pagination and more
head <file> Output the first 10 lines of <file>
<cmd> > > <file> Appends the output of <cmd> to <file>
<cmd> > <file> Direct the output of <cmd> into <file>
<cmd1> | <cmd2> Direct the output of <cmd1> to <cmd2>