Exercise 4.1 Command-Line Navigation
In this exercise, you use commands to navigate the file system, to view items that aren’t visible from the Finder, and to access the manual (man) pages that tell you about commands.
View Your Home Folder
If necessary, authenticate as Local Administrator at the prompt.
Click Launchpad in the Dock.
In the Search field at the top of the screen, enter Terminal.
A new Terminal window opens.
The second line includes your user name and computer name followed by a prompt—for example:
ladmin@Mac-17 ~ %
In this example, ladmin is the name of the logged-in user, and Mac-17 is the name of the Mac you logged in to. A space separates the computer name from the path to your current working directory. The path is ~ (the tilde [~] is shorthand for your home folder). The path is followed by the prompt, which is %.
At the prompt, type ls and press Return.
Terminal displays output that looks something like this, followed by another prompt:
Desktop Downloads Movies Pictures Documents Library Music Public
Switch to the Finder. If you don’t find an open Finder window, choose File > New Finder Window or press Command-N.
Select ladmin’s home folder in the Finder sidebar and compare the contents of the home folder in the Finder and Terminal.
With the exception of the Library folder, what you see in Terminal is the same as in the Finder. (User Library folders are hidden in the Finder by default; see Reference 14.1, “Examine Hidden Items.”)
Switch back to Terminal and type ls -A (lowercase LS followed by space, a hyphen, and an uppercase A) at the prompt.
In general, the command-line environment is case-sensitive. For example, ls -a isn’t the same as ls -A.
The list includes some extra files that begin with a period. Files beginning with a period are hidden in directory listings unless you ask for them by entering ls -A. The Finder doesn’t show files beginning with a period (sometimes called dot-files).
Examine and Change Your Current Working Directory
Think of your current working directory as the place where you are in the file system. When you open a new Terminal window, your default working directory is your home folder. Use the cd command to change your current working directory.
At the prompt, type pwd.
The period (.) ends the sentence and isn’t part of the command, so don’t type it. This guide tells you if a trailing “.” is part of the command. Also, press Return at the end of each step, unless otherwise instructed.
This is where Local Administrator’s home folder exists in the file system. It’s the folder you’re “in” in this Terminal window.
At the prompt, type cd Library.
This changes your current working directory to the Library folder inside your home folder.
This command uses a relative path. A relative path means “Start in my current working directory.”
Your prompt changes to something like this:
ladmin@Mac-17 Library %
The path component of the prompt indicates the folder you are in, not the entire path.
The cd command changed your working directory without providing feedback. A command that completes and doesn’t need to provide feedback will exit silently. If you get an error message, you should investigate its cause before continuing.
At the prompt, type pwd. Terminal displays:
You changed to the Library folder that was inside your previous working directory.
Type ls to view what files and folders are in this Library folder.
At the prompt, type cd /Library. Note the / that precedes Library this time.
At the prompt, type pwd. Terminal displays output like this:
This is a different folder.
A path that starts with a leading / is an absolute path. It means “Start at the root folder and navigate from there.” A path that doesn’t start with a leading / is a relative path. It means “Start in your current working directory and navigate from there.” (For more information on file system structure, go to Reference 15.1, “macOS File Resources.”)
Enter ls to view the files and folders that are in this Library folder.
There is some overlap in the item names in this Library and the one in ladmin’s home folder, but the names aren’t entirely the same.
At the prompt, type cd and a space character. Don’t press the Return key.
Terminal enables you to drag and drop items from the Finder to Terminal and have the path to the items appear in the command line.
In this part of the exercise, you use the Finder to locate a folder you want to use as your working directory in Terminal.
Switch to the Finder.
When you don’t know exactly what you are looking for, it’s sometimes faster and easier to find a file or folder in the Finder.
Open a new Finder window if necessary.
Click Macintosh HD in the sidebar.
Open the Users folder.
Drag and drop the Shared folder to Terminal.
Terminal fills in the path (/Users/Shared). Macintosh HD doesn’t appear in the path that Terminal fills in.
The Finder shows you volume names to make locating a particular volume easier. Terminal doesn’t show volume names in the same way.
Switch to Terminal and press Return.
Type pwd at the prompt.
You are in the Shared folder.
Read About ls in the Man Pages
In Terminal, you can read the details about commands using the man command.
At the prompt, type man ls.
This opens the man page for the ls command.
Each man page is divided into various parts. The number in parentheses on the top line indicates in which section of the manual this command is documented. In this case, ls is documented in section 1, which is for general-use commands. Next, Terminal displays the name of the command and a terse summary of what the command does: “list directory contents.” The synopsis is supposed to be a formal representation of how to use the command. Anything contained in square brackets is optional. The synopsis isn’t always completely accurate. For example, a few options for ls are mutually exclusive of each other, but this synopsis does not indicate that. Options or switches (which change the behavior of a command) immediately follow the command, and arguments (which tell the command what to operate on) follow options or switches. The description, which explains the various uses of the command, follows the synopsis.
Type q to quit viewing the man page for ls.
At the prompt, enter man man.
Read about the man command.
You can also Control-click the command, or on a MacBook Pro with Touch Bar, tap the icon for a man page to open it.
When you are done, type q to exit.