You might immediately become anxious seeing a reference to the “command line” – or as it’s known on Mac, “terminal.” It’s true that it can feel daunting to use at first. But the good news is that it follows straightforward rules.
Once you get the hang of the commands, using the terminal can become second nature. And if you aren’t sure where to start, you can use the cheat sheet below! It contains commands as well as common vocabulary.
Mac Terminal Commands & Cheat Sheet
Tip before you dive in: although you might see commands with quotation marks below, you don’t need to use the quotation marks in the terminal.
How to Open Terminal on Mac:
Accessing the terminal app on your Mac is easy. To open Terminal on your Mac, you can click the magnifying glass search icon in the top bar, and type “Terminal.”
Then, simply double-click the Terminal app result.
This is called a “shell prompt.” It shows up when the Mac terminal is ready to accept a command. For example, when you first open the terminal, you’ll see a placeholder line like this:
yourname@macbook-pro ~ $
When you see the shell prompt at the end of a line, you’ll know that it’s ready to accept commands.
When talking about the command line/Mac terminal, an “argument” is the subject of a command.
When using the command line, folders are called “directories.”
Whatever folder (or directory) you’re currently looking at in the terminal is called the “working directory.”
Files and directories on your computer are organized into a “filesystem.” This organizes the computer’s files and directories into a tree-like structure.
The first directory in the filesystem is the “root directory.” It’s the parent of all other directories and files in the filesystem.
For example, let’s say you have a folder called “Desktop” as your root directory. Within that folder, you might have folders for Art, Music, Programming, and Pictures. Each of those folders would also have their own folders within them. So you might have a path like:
Often abbreviated as stdin, this refers to any information (for example text) inputted into the terminal through the keyboard or input device.
For example, standard input could be whatever command you enter.
Often abbreviated as stdout, this refers to information outputted after a process is run.
For example, if you did an “ls” command, the standard output would be the list of files/folders it showed as a result.
Often abbreviated as stderr, this refers to an an error message outputted by a failed process.
“pwd” stands for “print working directory.” It shows the name of the directory you’re currently in, aka the “working directory.”
“ls” and “pwd” are helpful for showing where you are in the filesystem.
For example if you did:
it might output:
i.e.: it shows you exactly where you are. If all it said was “Games,” you might not be sure where on your computer the “Games” folder was nested.
If you’re wondering how to navigate to a folder in Terminal, look no further.
cd stands for “change directory.” This command switches you into the directory you specify (i.e.: it changes the “working directory”).
However, the directory (folder) you’re switching to has to be within the current working directory. That may sound confusing, but think of it like this:
Let’s say that your computer has a parent folder called “MyComputer.” Within that folder, there’s a “Desktop” folder. On the desktop, there’s a folder called “Miscellaneous.” If you’re in the Mac terminal and your current working directory is MyComputer, doing “cd misc” it will output an error such as:
cd: no such file or directory: miscellaneous
But if your working directory is Desktop in the above example, when you do “cd miscellaneous,” it will switch you to the folder titled “miscellaneous” that’s on the desktop.
In this scenario, the folder you’re switching to is commonly referred to as the “argument” for the “cd” command.
If you want to switch to a folder (directory) that isn’t within your current working directory, you still have options.
To navigate into a child folder, you can do:
(Replacing “parentfoldername” with the name of the parent folder, etc.)
To move up one directory (to the parent folder) use:
for example if you were in the “Miscellaneous” directory mentioned above, doing “cd ..” would navigate from MyComputer/desktop/miscellaneous to MyComputer/desktop.
mkdir stands for “make directory.” It takes a directory name as an argument and creates a new directory in the current working directory.
In other words, it lets you choose the name of a new folder, and creates it within the folder you’re currently in.
For example if you’re in the “Work Docs” folder and you do:
it would add a new directory called “clients” to the “Work Docs” directory.
Wondering how to create a new file in terminal? The “touch” command is your new best friend.
This command creates a new file inside the working directory you’re in. It takes the filename as an argument, and creates an empty file with that name in the current working directory.
For example if you’re in the “Music Stuff” folder and you do:
That would create a new blank text file called “JayZ” in the “Music Stuff” directory.
When you type “ls” and press enter, the command line checks what folder you’re in, and then lists the files/folders inside of it.
For example, let’s say that you were in a folder that you use for saving Screenshots If you type “ls” and press the enter key, you might see a list like this:
This type of command is referred to as an “option” command. It modifies the behavior of the “ls” command so that it also lists the files/directories that start with a dot. If you’re just using “ls” on it’s own, it hides files starting with a period.
would list all the files/folders in the working directory you’re in, including ones that start with a dot.
This matters because some computer files are hidden from regular view. When that’s the case, those files/folders start with a dot.
This command can be used with “ls,” and it lists all the contents of a directory in long format. This means that all files and folders are listed as a table with different columns. Examples of columns that will be listed in the results:
- Access rights, which are actions that are permitted on a file or directory
- Number of hard links, which counts the number of child directories and files (and includes the parent directory link (..) and current directory link (.)
- Username of the file’s owner
- Name of the group that owns the file
- Size of the file in bytes
- Date/time that the file was last modified
- Name of the file or directory
To use this command, for example you could do:
which would output for example:
drwxr-xr-x 3 YourUsername staff 96 Jun 15 15:25 Art
-rw——-@ 1 YourUsername staff 64862 Feb 3 14:15 Form 1099G.pdf
-rw-r–r–@ 1 YourUsername staff 92672 Jun 8 13:28 Github Notes.doc
-rw——-@ 1 YourUsername staff 64862 Feb 3 14:11 Testing Instructions.pdf
-rw——-@ 1 YourUsername staff 6179 Apr 27 22:56 Client Notes.rtf
drwxr-xr-x 8 YourUsername staff 256 May 2 14:45 Test Scripts
-rw-r–r–@ 1 YourUsername staff 143254 Mar 8 12:05 iphone-receipt.png
You can compare that output to the list of column titles above to see what each represents.
This command can be used with “ls,” and it outputs a list of files/folders in the working directory in order of the time they were last modified.
So for example you could do:
and it would list all of the files/folders in the current working directory, starting with the one that was most recently modified (and ending with the one that was modified the longest ago).
Using Multiple Options at the Same Time:
You can use multiple options at the same time, like:
(i.e. that would use “-a” and “-l” at the same time, or “-a” and “-l” and “-t” at the same time)
This command copies files or directories. First you name the file you want copied, then the directory you want it copied to. For example, if you’re in the “Music Lyrics” directory and you do:
cp harddaysnight.txt allbeatleslyrics.txt
it would copy the Hard Days Night lyrics into the All Beatles Lyrics file.
Or you could do:
cp MusicLyrics/rockthecasbah.txt punk/
and it would move the Rock the Casbah text file into the “punk” directory.
You can also copy multiple at a time like:
cp MusicLyrics/rockthecasbah.txt MusicLyrics/magnificentseven.txt punk/
Wildcards are special characters for selecting groups. For example:
* selects all files in the working directory
So you could do:
cp * MusicLyrics
to copy all files into MusicLyrics.
If you wanted to only select and copy files with specific criteria, you can do that too. Let’s say that you only want to copy text files that start with the letter “g.” In that case, you would do:
cp g*.txt musiclyrics/
and that would copy all text files that start with the letter “g” into the MusicLyics folder.
Similar to cp, this command moves files. The main difference is that with “cp,” you’re copying the file – so the original version still remains in the previous location.
To move a file into a directory, use mv with the source file as the first argument and the destination directory as the second argument.
For example to move “guitarchords” text file into a folder called “Music,” you would do:
mv guitarchords.txt music/
You can also move multiple files at a time like:
mv guitarchords.txt basschords.txt music/
You can also use mv to change a filename. For example:
mv guitarchords.txt musiclearning.txt
That would change “guitarchords.txt” to be called “musiclearning.txt.”
If you want to know how to delete a folder (or file) in terminal, all you need to know are two letters: rm. This command deletes files and directories. For example, to delete a text file called “Miscellaneous” in the working directory, you would do:
If you want to delete a directory and all of it’s child directories, you would use the -r command too. To delete a Directory called “PhoneNumbers” you would do:
rm -r phonenumbers
Using this command makes the terminal output whatever you tell it to. For example:
echo “Testing 123”
With redirection, you can direct the input and output of a command to/from other files and programs, and chain commands together.
Redirection reroutes standard input, standard output, and standard error to or from different locations. For example, if you want to save a list of errors that you receive in a common location, this can be helpful. See below for ways to use redirections.
Using the “>” command redirects the standard output to a file. For example:
echo “Testing 123” > test.txt
would add “Testing 123” to the test text file.
This command outputs the contents of a file to the terminal. Using the above example:
would make the terminal output “Testing 123” (or whatever the contents of the “test.txt” file were).
cat and >:
You can use a combination of “cat” and “>” to add the contents of one file to another. For example:
$ cat chicago.txt > illinois.txt
would take the chicago text file contents and move them into the illinois text file, replacing everything that was already there.
cat and >>:
If you want to add the contents of one file to another (i.e.: add to the end of the new file rather than replace the old contents), you can use cat and >>. For example:
$ cat dachshunds.txt >> dogbreeds.txt
This would add the contents of “dachshunds.txt” to the end of the “dogbreeds.txt” file, without replacing the earlier content in the file.
Takes the standard input from the file on the right and inputs it into the program on the left. For example:
$ cat water.txt < beverages.txt
This would replace the contents of “water.txt” with the contents of “beveragestxt.”
The | symbol is called a “pipe.” It takes the standard output of the command on the left, and ‘pipes’ it as standard input to the command on the right (i.e.: command to command redirection). For example, using “wc” (which means word count):
cat oceans.txt | wc
Here the output of cat oceans.txt would be the standard input of wc (word count). Then the “wc” command outputs the number of lines, words, and characters in oceans.tx.
You can chain them together like this:
cat oceans.txt | wc | cat > totaloceans.txt
Here the output of cat oceans.txt is “piped” to the wc command, and then the output of the wc command is “piped” to cat, the output of which is redirected to rivers.txt.
In simpler terms, this would get the word count of of the “oceans.txt” file, and then add that word count to the “totaloceans.txt” file.
This command takes the standard input and orders it alphabetically. For example:
This would output the words in the oceans text file in alphabetical order.
Let’s say you wanted to sort all the items in a file, and create a new file with the list in alphabetical order. To do that, you could do:
$ cat oceans.txt | sort > sorted-oceans.txt
This command filters out adjacent duplicate lines in a file. For example if dogs.txt contents were:
and you did:
it would make the file contents:
If you want to make sure it gets rid of all duplicates (not just ones that were listed next to each other), you can do:
sort dogs.txt | uniq
Then it alphabetizes the contents and removes duplicates (which would always be adjacent in this case, since it would be in alphabetical order from being sorted).
If you wanted to put the list into a new file:
sort dogs.txt | uniq > individual-dogs.txt
grep stands for “global regular expression print.” It searches files for lines that match a pattern and returns the result. One important aspect to know is that it’s case sensitive.
For example if you did:
grep Corgi dogs.txt
It would search for lines in dogs.txt that have the text “Corgi” (but it wouldn’t return anything that had “corgi” instead of “Corgi”).
If you wanted it to not be case sensitive, you could use -i:
grep -i Corgi dogs.txt
If you wanted to search a whole directory (and all files in it), you could use -R:
grep -R Corgi /home/yourusername/Pets
This would search all files in the Pets directory for lines that contain “Corgi.”
If you only wanted to check filenames (and not content within the files), you could use -Rl:
grep -Rl Corgi /home/yourusername/Pets
and it would only return results where “Corgi” was in the filename.
sed stands for “stream editor.” It accepts standard input and modifies it based on an “expression,” before displaying it as output data. In other words, It’s basically like “find and replace.” For example if you did:
sed ’s/blue/green/‘ colors.txt
It would would search colors.txt and replace the first (and only the first) instance of the word “blue” with “green.”
The “s” stands for substitution. It’s always used when using “sed” for substituting.
If you wanted to replace all occurrences of the word, and not just the first, you would use g like this:
sed ’s/blue/green/g’ colors.txt
That would replace all uses of the word “blue” in the file. “g” stands for global.
The command line environment consists of the settings/preferences in that session, and can be configured/customized.
nano is a command line text editor. It works like TextEdit, except that it’s accessible from the command line (and only accepts keyboard input). To open a new text file in the nano text editor, you would do:
The menu of keyboard commands at the bottom of the resulting window lets you save changes to your new file and exit nano. The “^” stands for the Control key. Options in the nano editor include:
- Control + O saves a file. (O stands for output)
- Control + X exits the nano program (X stands for exit)
- Control + G opens a help menu
- clear (which you type and press enter) clears the terminal window, moving the command prompt to the top of the screen
This is the name of the file for storing environment settings. It’s commonly referred to as the “bash profile.” When you start a new command line session, it will load the contents of the bash profile before executing commands.
If you want to change environment settings, the command nano ~/.bash_profile opens the bash profile in nano.
If you save an echo in it, it will display that text whenever a terminal session begins.
When you do:
it activates the changes in the bash profile for the current session. That is, is saves the changes so they take effect immediately, instead of only saving it for future sessions.
You can store shortcuts for commands using “alias.”
For example if you wanted to save a new name for the “pwd” command (for printing the directory file path) you could do:
That would make it so that typing “aj” in the command line could be used to print working directory.
Then if you used source ~/.bash_profile, the new alias/shortcut would be available right away even in the current session.
In another example, you could do:
That would make it so that typing “hy” in the command line would output your history for that session. (Again, you’ll need to do source ~/.bash_profile in order to make this save right away and not wait until the next session.)
You could even use this for multiple commands, for example:
alias ll=“ls – la”
Would make it so that typing “ll” would automatically output all contents/directories in long format, including hidden files.
Environment variables hold info about the environment. They can be used across commands and programs.
The above would set the environment variable “USER” to “Solange Knowles.” (Usually, “USER” is set to the name of the computer’s owner.)
and then you could do:
export USER=“Solange Knowles”
which would make the variable available to all child sessions initiated from the session you’re in.
This would return the value of the variable (“Solange Knowles”). $ is always used when returning a variable’s value.
If you store the following in ~/.bash_profile:
export PS1=“>> “
(Note: PS1 is a variable the defines the makeup and style of the command prompt)
that would change the command prompt from “$” to “>>”
Then you’d just have to do the source command (to make it take effect immediately). From then on, the command line wold display the new command prompt.
The HOME variable is an environment variable that displays the path of the home directory. For example, if you did:
it would display the path to your home directory, i.e.:
PATH is an environment variable that stores a list of directories that contain scripts, separated by colons.
would result in something like this:
The bin directory stores many commands. For example, the following scripts:
are executed when you do “pwd” or “ls” respectively.
The “env” command stands for environment, and returns a list of the environment variables for the current user. For example:
env | grep PATH
would display the value of a single environment variable.
The standard output of “env” is ‘piped’ to the grep command. Then, the grep command searches for the value of the variable PATH, and outputs it to the terminal.
How to Clear the Terminal:
Let’s say that you want to clear the terminal, so that the previous commands from your session disappear from view. Luckily, this one’s very easy – all you have to do is:
After typing clear and pressing the enter key, it will clear your earlier commands from the terminal.
Using the Terminal on Mac
The command line/terminal might not have the most straightforward user experience. Although the cheat sheet doesn’t cover every possible command, it’s a great place to start.
And don’t worry about memorizing all of the items above. If you need to stop and double-check which command to use, that’s perfectly okay. The more you practice using terminal on your Mac, you’ll find that you start to remember common commands without trying.