Even MAC has a command line, and it's Berkley Unix Based. Ya, that's right, MAC os is basically wrapped around Unix, just like linux. So how can we take advantage of the MAC command line?
Let's say, for example, that we want 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 command line or 'Terminal' is your new best friend.
So what is a Terminal? 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. The command line interface (CLI), or the language that you type into Terminal to interact with your Mac, is called bash. Everything we discuss below is a bash command.
Before you start using Terminal, you can customize it to your own personal preference. If you prefer, it’s even possible to download a third-party Terminal alternative for a customized look and feel.
First, let’s look at some basic Terminal facts you should know.
A bash command typically follows this pattern:
[Command] [Options] [Input or Path to File or Directory]
For example, in:
ls -la /Applications
ls
is the command, -la
is a compound of two individual options (-l
and -a
), and /Applications
is the path to list.
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 path of a file called My Secrets that lives on user John Doe’s Desktop is /Users/jdoe/Desktop/"My Secrets"
.
You must escape white space for the Terminal to process it properly. When bash 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 /Applications/Path Test
, you’ll get this:
What’s going on here? Well, bash thinks that you called ls on /Applications/Path. When it couldn’t find that file, it stopped.
If you want bash to recognize the full name of your folder, you can either wrap the name in quotes or use a backslash, like so:
ls /Applications/"Path Test"
orls /Applications/Path\ Test
Many of the commands below require administrator-level access. If you’re not currently signed into administrator account, but you know the administrator’s password, you can place sudo
(which stands for “single user do”) in front of the command to temporarily give it administrator-level privileges.
Now that you know the basics, let’s take a look at some extremely handy 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.
Spotlight tends to skip macOS system files unless you tell it not to, and even then can have trouble indexing them. Conversely, the bash 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:
You should know that find uses regex (also called regular expressions). A full explanation of this topic is outside the scope of this article (or anything short of a textbook). However, the below example introduces a vital concept in regex, which is the asterisk (*), or wildcard character.
Putting it at the beginning and end of the search string means that find will output results that have characters before and after the search term. In this case, Google Chrome will bring up Google Chrome.app.
It all comes together to look like this:
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:
du -d 1 /Applications
, it will only show you the total size of the folders and files in your Applications folder, not the sizes of subfolders within those folders.Take a look at du in action:
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 /Users/jdoe/Desktop/file1
will move file1from jdoe’s Documents to his Desktop.
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:
Here’s what the output looks like:
Create new folders in an instant with this command.
Example: mkdir /Users/jdoe/Desktop/cool_stuff
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.
Example: rm -R /Users/jdoe/Desktop/cool_stuff
Now you know some essential Terminal commands and can start integrating them into your daily Mac workflow. Once you get comfortable using bash, you can go beyond simply replacing your everyday tasks and start exploring powers that only the command line can offer.