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 “music” app, but it will locate the “Music.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:
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.
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:
joan@MyMac ~ % 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.
joan@MyMac ~ % cd Public/Drop\ Box joan@MyMac Drop Box % pwd /Users/joan/Public/Drop Box
Another way to enter filenames and paths with spaces is to surround filenames and paths with quotation marks:
joan@MyMac ~ % cd "Public/Drop Box" joan@MyMac Drop Box % 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 flashes 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 displays your two choices, Pictures and Public. Now, enter u after the initial P, and then press the Tab key again, and the Mac automatically finishes Public/ for you. Finally, enter D and press the Tab key one last time, and the Mac finishes 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. If in doubt, you should delete the / at the end of a path.
Tab key completion reads only into folders you have permission to access. You may run into issues trying to use this feature for items that are readable only by the root user.
View Invisible Items
The CLI and the Finder hide many files and folders from view. The hidden items are often created and used by macOS. Leave them alone. 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, filenames that begin with a period won’t appear. 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:
joan@MyMac ~ % ls -la /Users total 0 drwxr-xr-x 8 root admin 256 Aug 25 13:29 . drwxr-xr-x 22 root admin 704 Aug 20 22:57 .. -rw-r--r-- 1 root wheel 0 Jul 14 15:30 .localized drwxrwxrwt 6 root wheel 192 Aug 21 10:59 Shared drwxr-xr-x+ 11 jill staff 533 Aug 25 13:23 jill drwxr-xr-x+ 11 joan staff 352 Aug 25 13:29 joan
Any item with a period at the beginning of its name is hidden by default in the CLI and the Finder.
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 display a list of bookmarks. Choose a bookmark to jump to that bookmark.