Quick Links

Key Takeaways

  • The Terminal utility on a Mac lets you use powerful text-based commands for various tasks such as finding files and customizing settings.
  • Understanding the syntax of Terminal commands and the path of files will help you navigate and interact with your Mac more effectively.
  • Some useful Terminal commands include "find" for searching files, "du" for checking disk usage, "mv" for moving files, "ls" for listing files, "mkdir" for creating folders, and "rm" for deleting files.

While the Mac’s desktop GUI is easy to use and sufficient for most of your everyday needs, the Terminal utility lets you get under the hood and use some powerful text-based commands to accomplish all sorts of tasks.

There are faster ways to find out where that pesky 5GB file is hiding or the path of every file related to that app you thought you deleted. For these jobs and others, the Mac Terminal is your new best friend.

What Is the Terminal?

The Terminal is a utility that allows you to interact with your Mac through the command line. Linux operating systems include similar tools, since both Linux and macOS are Unix-like OSes. While the macOS Terminal formerly used Bash commands, ever since macOS Catalina it has been based on zsh (Z shell). The basic commands we’re using here should work with either, however.

There are various ways to open the Terminal on your Mac. Once you start using it, you can also customize the zsh prompt in the Mac Terminal to your preferences.

There are many more things you can do in the Terminal, too. By installing the Homebrew package manager, you can use it to install new programming languages, software repositories, and more.

General Mac Command Line Tips

First, let's look at a few basic Terminal facts you should know.

General Syntax

A zsh (or Bash) command typically follows this pattern:

        [Command] [Options] [Input or Path to File or Directory]
    

For example, in the command:

        ls -la Downloads
    

…to list the contents of the Downloads folder:

  • ls is the command
  • -la is a compound of two individual options: -l (long listing format) and -a (all files and directories)
  • Downloads is the path to the directory (folder)

The Path

Understanding paths will help you understand how macOS actually sees your files. Essentially, the path of a file is the Russian dolls' nest of folders in which it's contained, followed by the name of the file itself.

For example, on a Mac, the full path of a file called My Secrets that lives on user John Doe's Desktop is:

        /Users/jdoe/Desktop/"My Secrets"
    

If you are already in that user’s home folder, however—check with the pwd command—you can just use a relative path:

        Desktop/"My Secrets"
    

White Space

You must escape white space for the Terminal to process it properly. When zsh sees a space, it interprets it as the end of a command. So if you have a folder with spaces in its name, like Path Test, and you try to list its contents with:

        ls Documents/Path Test
    

…you'll get the error "No such file or directory". What's going on here? Well, zsh thinks that you called ls on Documents/Path. When it couldn't find a file or directory with that name, it stopped.

If you want zsh to recognize the full name of your folder, you can either wrap the name in quotes:

        ls Documents/"Path Test"
    

Or you can use a backslash before the space:

        ls Documents/Path\ Test
    

The contents of the Path Test directory will then be listed.

Mac Terminal ls command for PathTest directory
Phil King/MakeUseOf

Sudo

Many of the commands below require administrator-level access. If you're not currently signed into the administrator account, but you know the administrator's password, you can place sudo (which stands for "superuser do") in front of the command to temporarily give it administrator-level privileges.

Terminal Commands to Improve Your Workflow

Now that you know the basics, let's take a look at some of the handiest Terminal commands. Note that you can pull up full information on these commands, including all their options and examples, by typing:

        man <command name>
    

…into the Terminal.

find

Replaces: Spotlight

Why it's better: It's faster and searches system folders that Spotlight excludes, or has trouble indexing. Spotlight tends to skip macOS system files unless you tell it not to, and even then can have trouble indexing them. Conversely, the Terminal find command can search for anything, in any place, and will output the full path of what you're looking for.

The syntax of find consists of four parts. In order, they are:

  1. find
  2. the path of the directory (folder) you want to search (Documents below)
  3. options (the example below has -d (directory depth level) and -name (match the name)
  4. the string to search (the below example has Google Chrome)

You should know that find uses regex (also called regular expressions). Here we’re using the asterisk (*), which acts as a wildcard character. Putting it at the end of the search string means that find will output results that have characters before and after the search term.

It all comes together to look like this:

        find Documents -d 1 -name "p*"
    

This finds all files or directories with names starting with a lower-case "p" in the Documents directory (without looking in any of its subdirectories, due to the -d 1 depth level 1 option).

Mac Terminal find command
Phil King/MakeUseOf

du

Replaces: Cmd + I to show info.

Why it's better: It can show you multiple folders at once, and typically takes less time to load. du stands for "disk usage," and can quickly tell you the size of a file or folder, or even a list of files within a folder. The best options for du are:

  • -d (depth): When followed by a number, tells du to limit its search to a -d level of depth in the directory where it runs. For example, du -d 1 Documents will only show you the total size of the files and first level of subfolders in your Documents folder.
  • -h (human readable): Shows you the size of your files in K, M, or G, which stands for kilo, mega, or gigabytes.
Mac Terminal window running disk usage command
Phil King/MakeUseOf

mv

Replaces: Point-and-click moving of folders and files.

Why it's better: It's faster and requires no navigation. You can quickly move a file or folder into another folder using mv. It works by simply changing the name of the path.

The syntax is:

        mv <old file path> <new file path>
    

For example:

        mv /Users/jdoe/Documents/file1.rtf /Users/jdoe/Desktop/file1.rtf
    

…will move file1.rtf from the jdoe user’s Documents folder to the Desktop.

ls

Replaces: Cmd + I to show info.

Why it's better: It's faster, can show info on multiple files at once, and is highly customizable. ls is an incredibly powerful command for showing you exactly what's in your folders. It also reveals who's allowed to see them, if you have any hidden files or folders, and much more.

The best options for ls are:

  • -l (long): Shows the permissions for each file in the folder, the most recent modification time, the file owner, and filename.
  • -a (all): Shows you all the files in a folder, including the hidden files (great for showing the user library in macOS, which is hidden by default).

Here's what an example output looks like:

Mac-Terminal-ls-list
Phil King/MakeUseOf

mkdir

Replaces: Finder > File > New Folder

Why it's better: It's faster, and you can set the name right in the command instead of double-clicking the new folder. Create new folders in an instant with this command.

For example, the command:

        mkdir /Users/jdoe/Desktop/cool_stuff
    

…creates a new cool_stuff folder in Desktop.

rm

Replaces: Moving files to the Trash and emptying it.

Why it's better: It's faster and good for deleting pesky files that the Trash won't get rid of. This command will delete, immediately and without prejudice, any file you put in its path. Obviously, use it with extreme caution. Unlike clicking Empty Trash, rm will not ask if you're sure. It assumes you know what you're doing.

One thing to note about rm is that by default, it will only delete files, not folders. To delete folders, you must use the -R option, which stands for "recursive".

For example, the command:

        rm -R /Users/jdoe/Desktop/cool_stuff
    

…will delete the cool_stuff folder in Desktop.

Now you know some essential Terminal commands, you can start integrating them into your daily Mac workflow. Once you get comfortable using zsh, you can go beyond simply replacing your everyday tasks and start exploring powers that only the command line can offer.