Terminal, sometimes called the command line interface or CLI, is a text-based program for interacting with your computer’s deepest levels. But while it gives users more power, it also ditches the graphical interface we all know and love. This can make learning Terminal tricky, and not a little bit daunting. Fortunately there’s nothing to be anxious about: great power is at your fingertips, and you need only learn the right macOS Terminal commands.
If Terminal is brand new for you, start here. Once you know how to launch Terminal and input commands, you can come back here to expand your knowledge.
Hacking the Mainframe: Getting Acquainted
Before we get started, let’s talk about the working directory. The working directory, as we covered previously, is the directory Terminal is currently using as “home base.” You change your working directory with
cd, and it’s the point from which all file path references are calculated.
Some commands are performed within the working directory, and other commands don’t care about it at all. If a command does care about the working directory (like
ls, for example), you’ll need to run that command from within the appropriate working directory. You can see your current working directory to the right of the colon at command prompt. Remember that the tilde ( ~ ) represents the current user’s home directory and the forward slash ( / ) alone represents your boot drive.
Also note that, when using the commands below, you should omit the dollar sign ($). It’s a universal symbol for “command prompt,” but it’s not part of the command itself.
macOS Terminal Commands: Modifiers
Modifiers adjust existing commands to make them do more things, or do things differently than they might normally.
sudo stands for “super user do,” and it doesn’t do anything on its own. Instead, it gives the command it’s partnered with superpowers. When you type
sudo before a command, the command will be run with administrator privileges, giving you the power to do things you won’t normally do. It also gives you the power to break things very badly, so don’t use sudo unless you know what you’re doing.
After you use
sudo, you’ll need to enter your administrator password. When you do, the input cursor won’t move, but the keystrokes will be captured. Just press Enter when done to execute the command.
Run the last command again, but this time with administrator privileges. And get really excited about it!
Called a “redirect,” the caret sends the output of your command to a file (AKA “file output”) instead of the Terminal window (AKA “standard output”). For example,
ls > filelist.txt will send the output of
ls to a text file called
filelist.txt located in your current working directory. If the file doesn’t exist yet, it will be created.
This symbol, called a pipe, is found above the Enter key on US keyboards or next to the left Shift key on ISO keyboards. It will send the output of one command, like
ls, to another command, like
Flags are options appended to commands and preceded by one or two dashes ( – ). These flags change the way the command operates, adjusting functionality or toggling different features. A few frequent flyer flags are featured further down:
-v: verbose mode, which causes the command to narrate its actions. Useful for debugging problematic programs or monitoring long-running commands.
-r: recursive mode, which causes the command to run itself repeatedly over the contents of a directory or other set of inputs. Useful for applying per-file commands like
chmod to whole folders.
-f: force a command to run, disabling confirmation dialogs. Use with caution!
macOS Terminal Commands: Viewing & Navigating Directories
You’ll spend a lot of time in Terminal moving between directories using
cd and examining their contents using
ls, or list, reports information about the working directory’s contents. We’ve covered
ls‘s basic operation already, so here’s a few more advanced flags.
$ ls -la
The -a flag lists all the contents of directory, including hidden files. The -l flag returns the results in “long format,” which includes crucial information like file size, modification date, permissions and so on.
$ ls -lS
The -S flag sorts long format output by file size. Like all the commands below, the -S flag needs to be run with the -l flag to work. To quickly run more than one flag at once, list them one after another, prepended by a single dash.
$ ls -lh
The -h flag shows file sizes in a “human readable” format, using units like kilobytes instead of block size.
$ ls -le
The -e flag shows the Access Control Lists, or ACLs, associated with a file. macOS’s HFS+ file system uses ACLs to attach advanced file system attributes to files and folders.
$ ls -lt
The -t flag sorts files by the time they were modified, with the most recently modified file displayed first.
We also touched on the basics of
cd, so here’s a few tricks you can try out.
Without any other arguments,
cd returns you to your home directory at /Users/[username]/
$ cd ..
The two periods at the end of the command mean “parent folder,” and they will bump you up one directory level.
$ cd Adobe/Files/Brushes
Changes the working directory to Brushes. Don’t forget that
cd for the specified path within the current working directory. If you want to navigate to a specific path using the root directory as your starting point, you’ll need to start your file path specification with a forward slash ( / ), like /Users/alexander/Documents/Adobe. The root directory starts from your computer’s boot disk (typically Macintosh HD), so the file paths will often be fairly lengthy.
macOS Terminal Commands: Copying, Moving and Deleting Files
$ ditto -V old/folder/location new/folder/location
Copies the contents of first directory into contents of second directory. The -V flag is for verbose, which will cause
ditto to report its progress as it copies files. Of course, you could also specify the current folder as the target destination by using the period ( . ) in place of new/folder/location.
$ cp filename.doc path/to/directory/newfilename.doc
Creates a copy of the file filename.doc in path/to/directory/newfilename.doc. If the filename already exists, the copy operation will not be completed.
$ mv badname.txt goodname.txt
Renames badname.txt to goodname.txt. Since renaming a file is essentially the same as “moving” the file’s bits to a new filename, we use the
mv, or move, command to accomplish this.
$ mv badname.txt path/to/directory/goodname.txt
Moves badname.txt to path/to/directory and renames it goodname.txt. Deletes the original badname.txt file when moving.
$ mv goodname.txt path/to/directory/goodname.txt
Moves goodname.txt to a new directory location without renaming the file.
$ rm -rf contents/
Removes the directory contents and all the files contained within it. The -r flag makes
rm, or remove, operate recursively, while the -f flag shuts off any confirmation dialogs. The more powerful version of this command,
sudo rm -rf, should be used with extreme caution. It can erase your boot drive without warning or complaint, so handle with care.
macOS Terminal Commands: Permissions
Permissions affect which users can view, edit and execute specific files. Files and folders have an owner, which is normally the user that created it, and modes, which control the users that can access the file, as well as what they can do with it.
These commands are the first commands that we’ll use
sudo with. Remember that
sudo elevates us to a superuser, giving us temporary administrator power. This is almost always necessary when dealing with file permissions, since not every file will be owned by your current user.
$ sudo chown -R af path/to/directory/
Change the owner of every file in the given directory to user af. The -R flag makes the command recursive. Of course, you could also run this in your working directory by using the period ( . ) in place of path/to/directory.
$ sudo chmod -R 775 path/to/directory/
chmod stands for “change mode,” and adjust permissions for files and folders. In this example, the -R flag is again used to apply permissions recursively.
File permissions can be represented a few ways, but the “numeric mode” used above (775) is the most common. To learn about the different ways to represent permissions, check out chmod’s man page.
Now that you have these commands under your belt, you should be able to get a little more done with Terminal! However, I doubt you’ll be giving up Finder to start using mv instead. In our next post, we’ll look at expanding your understanding to accomplish more useful tasks.
You might also like:
5 Fun Terminal Commands To Try Right Now
Getting Started with Terminal: An Introduction
Featured image by Thomas Schanz (Own work) [CC BY-SA 3.0], via Wikimedia Commons