Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Reference 4.2 CLI Navigation

The command line is case-sensitive and requires that you use full filenames with filename extensions. For example, the CLI won’t locate the “itunes” app, but it will locate the “iTunes.app” app.

A path represents a file or folder’s location in the file system. For instance, Disk Utility’s file system path is /Applications/Utilities/Disk Utility.app. In the CLI, you use the pathname to navigate through the file system and to identify the location of items.

There are two types of file-system pathnames:

arr.jpg Absolute pathnames are full descriptions of an item location, starting from the root (or beginning) of the system (startup) volume. An absolute path begins with a forward slash (/) to indicate the beginning of the file system. An example of the absolute path to the user Joan’s Drop Box folder is /Users/joan/Public/Drop Box, which means: Start from the startup volume; go to the Users folder, then to the joan subfolder, and then to the Public subfolder; and select the item named Drop Box.

arr.jpg Relative paths are partial descriptions of an item location. They’re based on where you’re currently working in the file system. When you first open Terminal, your session starts at your home folder. The relative path from your home folder to your Drop Box is Public/Drop Box. This means: From where you are now, go into the Public subfolder, and select the item named Drop Box.

Navigate with Commands

You use three commands to navigate the file system: pwd, ls, and cd. Short for “print working directory,” pwd reports the absolute path of your current working location:

MyMac:~ joan$ pwd
/Users/joan

Short for “list,” ls lists the folder contents of your current working location. Enter a pathname following the ls command to list the contents of the specified item. The ls command has additional options for listing file and folder information that are covered throughout this lesson.

Short for “change directory,” cd is the command you use to navigate. Enter a pathname following the cd command to change your current working location to the specified folder. Entering cd without specifying a path returns you to your home folder.

Use Special Characters

You can use special characters at the prompt or in pathnames to save time and to be able to use special characters in filenames and pathnames.

Enter a space between command items to separate the items. If you don’t want the space character to separate items, use the backslash (\) before a space character.

MyMac:~ joan$ cd Public/Drop\ Box
MyMac:Drop Box joan$ pwd
/Users/joan/Public/Drop Box

Another way to enter filenames and paths with spaces is to surround filenames and paths with quotation marks:

MyMac:~ joan$ cd "Public/Drop Box"
MyMac:Drop Box joan$ pwd
/Users/joan/Public/Drop Box

You can drag and drop items from the Finder to Terminal. When you do this, Terminal enters an item’s absolute path with the appropriate backslash characters before spaces in names. Use the Tab key completion feature that’s built into the command line to automatically complete filenames and pathnames.

Other special characters include !, $, &, *, ;, |, \, parentheses, quotes, and brackets. The Finder drag-and-drop and Tab key completion parse these characters. In the CLI you can enter a backslash before any special character to treat that special character as regular text rather than as a special character.

Use double periods (..) to indicate a parent folder. For example, if you are in your home folder at /Users/username, enter cd .. to navigate to the /Users folder.

Use the tilde (~) to indicate the current user home folder in a pathname. For example, say the current user’s Drop Box is in ~/Public/. Use the tilde to specify another user’s home folder. For example, ~jill/Public specifies Jill’s Public folder.

Use Tab Key Completion

Use Tab key completion to automatically complete filenames, pathnames, and command names. Tab key completion prevents you from making typos and verifies that the item you’re entering exists.

Here’s an example of Tab key completion. Start from your home folder by entering cd, then P, and then press the Tab key. The Terminal window will flash quickly and you may hear an audible alert, letting you know there is more than one choice for items that begin with “P” in your home folder. Press the Tab key again, and the Mac will display your two choices, Pictures and Public. Now, enter u after the initial P, and then press the Tab key again, and the Mac will automatically finish Public/ for you. Finally, enter D and press the Tab key one last time, and the computer will finish the path with Public/Drop\ Box/.

When completing a folder name, Tab key completion puts a forward slash (/) at the end. It assumes that you want to continue the path. Most commands ignore the trailing slash, but a few behave differently if it’s there. You should delete the / at the end of a path.

Tab key completion reads only into folders you have permission to access. You might run into issues trying to use this feature for items that are readable only by a root user.

View Invisible Items

The CLI and the Finder hide many files and folders from view. The hidden items are often macOS support items. In the Finder, these items are set with a hidden file flag. The CLI ignores the hidden file flag and shows most hidden items. If you enter the ls command, you won’t see filenames that begin with a period. To see hidden items in long format at the command line, add the -a option to the -l option when you enter the ls command:

MyMac:~ joan$ ls -la
total 16
drwxr-xr-x+ 14 ladmin staff  448 Oct 26 01:06 .
drwxr-xr-x  5 root  admin  160 Oct 26 00:54 ..
-r--------  1 ladmin staff   7 Oct 26 00:54 .CFUserTextEncoding
drwx------  5 ladmin staff  160 Oct 26 01:06 .Trash
-rw-------  1 ladmin staff  139 Oct 26 01:06 .bash_history
drwx------  6 ladmin staff  192 Oct 26 11:29 .bash_sessions
drwx------+ 4 ladmin staff  128 Oct 26 01:06 Desktop
drwx------+ 3 ladmin staff  96 Oct 26 00:54 Documents
drwx------+ 3 ladmin staff  96 Oct 26 00:54 Downloads
drwx------@ 54 ladmin staff 1728 Oct 26 09:17 Library
drwx------+ 3 ladmin staff  96 Oct 26 00:54 Movies
drwx------+ 3 ladmin staff  96 Oct 26 00:54 Music
drwx------+ 3 ladmin staff  96 Oct 26 00:54 Pictures
drwxr-xr-x+ 4 ladmin staff  128 Oct 26 00:54 Public

Any item with a period at the beginning of its name is hidden by default in the CLI and the Finder. These items are created and used by macOS. Leave them alone.

Navigate to Other Volumes

In the CLI, the system volume is also known as the root volume, and it’s identified by a lone forward slash. Other nonroot volumes appear as part of the main file system in the Volumes folder.

Use Marks and Bookmarks

Add marks and bookmarks as you work; then use them to quickly navigate through lengthy Terminal output.

Select a line in Terminal, then choose Edit > Marks > Mark to add a mark. By default, Mark > Automatically Mark Prompt Lines is selected, so each prompt line sets a mark. Then you can choose Edit > Select Between Marks, or choose Edit > Navigate > Jump to Previous Mark, or just press Command-Up Arrow.

Choose Edit > Marks > Mark as Bookmark to add a bookmark. Then choose Edit > Bookmarks to see a list of bookmarks. Choose a bookmark to jump to that bookmark.

  • + Share This
  • 🔖 Save To Your Account