Are you afraid of the Mac command line? Learning a few simple commands can help you build your confidence and shed that fear. You might even discover that you enjoy working in a command-line environment!
Use Terminal to Execute Commands
Many of the shortcuts that you use in other apps will work here. You can open multiple tabs with Command+T or a new Terminal window with Command+N. Cut, copy, and paste all work as expected, and you can drag any file or folder into the Terminal window to immediately skip to that directory.
We’ll cover some of the most basic Mac terminal commands here. These will be familiar to you if you’ve ever used the Linux command line, too.
When we’re done, you might want to learn how to lock your Mac from the Terminal, or even how to shut it down with a simple command.
Use Flags to Modify Commands
Most commands can be appended with a flag in the form of a hyphen and a letter to access different functions. For example, the -R flag applies a command recursively so that it applies to a directory, all files and folders within that particular directory, all files and folders inside those folders, and so on.
The flag always appears after the command. For example: rm -i
Change Directory: cd
Example usage: cd /folder/
Use the cd command to change directories. For example: cd /Volumes/Elements/ to access an external drive called “Elements.”
You can use shortcuts to quickly skip to certain directories. For example, running cd ~ will take you to the Home directory for the current user. You can also use cd/ to get to the root of the drive, cd.. to move up one directory, or cd../.. to move up two directories.
List Files & Folders: ls
Example usage: ls /folder/
Also useful in navigating your drive, ls can be used to list the contents of the current directory simply by executing the command. Append it with a location on the drive to specifically target that directory.
You can add flags to the ls command to get different results. For example, use -C to get a multi-column output, -S to sort by size, -lt to sort by date modified, -la for detailed contents including hidden files, or -lh to create a list with readable file sizes.
Remember, you can also use the same location shortcuts that you’d use with the cd command (e.g. ls ~ ) to quickly jump around.
Copy: cp
Example usage: cp file.txt /destination/
Use cp to initiate the copy command, add a flag where required, and then enter the target file or folder, followed by a space, and then add the destination folder.
If you’re copying a single file, you can use the cp command without a flag as per the example shown above. If you want to copy a directory, you’ll need to use the -R flag to indicate that all files and folders in the directory are to be included. For example: cp -R /folder/ /destination/ .
You can even include multiple files in a single copy command. For example: cp file1.txt file2.txt file3.txt /destination/.
Move and Rename: mv
Example usage:mv file.txt /destination/
Move works almost identically to copy as shown above, except that there’s no need to add a recursive flag when moving directories. You can add an -i flag to the command to require confirmation before moving since the mv command will overwrite any files in the destination by default.
You can use mv to rename files too by “moving” a file to the same directory. For example: mv oldfilename.txt newfilename.txt.
Make a New Directory: mkdir
Example usage:mkdir
If you want to create a new directory, use the mkdir command, followed by the name of the directory that you want to create. You can create multiple directories by separating the names with spaces. For example: mkdir folder1 folder2 folder3.
If you want to create a folder with a space in the name, make sure that you put the folder name in quotations. For example, mkdir “my folder”.
Delete Files & Folders: rm
Example usage: rm
The rm command deletes files or folders immediately without asking you for confirmation first. You can add the -i flag to require user confirmation whenever using it, which should help prevent mishaps.
You can delete multiple files at once by appending more filenames to the end of the command. For example: rm file1.txt file2.txt file3.txt.
Display Disk Usage & Free Space: du & df
Example usage: du /destination/
Use the du command to calculate disk usage in the location specified thereafter. For a far more useful readout, run du -sh /destination/ instead to provide a human-readable total of disk usage for a specified location.
Similarly, you can use df -h to calculate disk space, or use the -H flag to display total disk space in “metric” storage units (e.g. 1000MB per GB as opposed to 1024MB per GB).
Find a File: find
Example usage: find /location/ -name
This command can help you locate files on your disk. Follow the find command with the location of the directory that you want to search in, the -name flag, and then the name of the file that you want to find.
You can always use a wildcard * to search partial filenames. For example, find /location/ -name ‘*.png’ would find all files with the .PNG extension in the specified location.
Open a File: open
Example usage: open
You can use the open command to open files or directories simply by specifying the path or path with filename thereafter. Open multiple directories or files by chaining them at the end of the command. For example, open file1.txt file2.txt file3.txt.
You can also open files in specific applications using the -a flag, followed by the name of the application (or the path to the .APP file if you know it). For example: open -a Preview file.pdf.
Edit a File: nano
Example usage: nano
nano is a basic open-source text editor included with macOS for editing files within the Terminal. You can edit text-based files, including system files, using the nano command, followed by the filename.
Once you’re in nano, pay attention to the commands at the bottom of the screen, which involve the control key. To save a file, hit Control+O (known as “Write Out”) or quit without saving using Control+X.
Run as Super User: sudo
Example usage: sudo
The sudo prefix is used to execute a command as a “super user,” also known as root or admin. Once you’ve entered a command prefixed by sudo, you’ll be required to enter your administrator password to execute it.
Some commands require root access in order to work. If you want to edit a system file, for example, you might need to use sudo nano
Show the Working Directory: pwd
Example usage: pwd
To display the current directory that you’re in (or “print working directory”), you can use the pwd command. This is especially useful for printing a path that you can later copy and paste.
Show Running Processes: top
Example usage: top
To see a list of currently running processes and how much CPU and memory they’re currently using, execute top. By default, the process will display all processes by CPU usage, with the process id or PID displayed alongside each entry.
You can hit “Q” to get back to the command line when you’re done.
Terminate a Process: kill
Example usage: kill
To kill a process, you’ll first need to run the top command to find its process ID (or PID). You can then use the kill command, followed by the number displayed alongside the process. For example: kill 1569.
Learn More about a Command: man
Example usage: man
Each command on this list has a manual associated with it that explains exactly how to use it and what the different flags do, along with some more exotic examples of the commands being used.
For example, the top command has a lot of flags and other modifiers, which you can read about using: man top. If you want to master the command line, use of the man command is vital.
Do More with Homebrew
These commands are useful to know in an emergency. For example, you can’t run Finder to copy files from your Mac’s drive in recovery mode, but you can manually copy files using Terminal if you know how to do it.
If you want to get more use out of Terminal, consider using Homebrew to download and install software directly from your Mac’s command line.