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 more about commands.
View Your Home Folder
If necessary, log in as Local Administrator.
Click Launchpad in the Dock.
In the Search field at the top of the screen, enter Terminal.
Click Terminal.
A new Terminal window opens.
The second line you see includes your computer and user name followed by a prompt—for example:
Mac-17:~ ladmin$
In this example, Mac-17 is the name of the Mac you logged in to. The colon separates the computer name from the path to your current working directory. The path is ~ (the tilde [~] is shorthand for your home folder). After the space, you see the name of the logged-in user. The $ is the prompt.
At the prompt, type ls and press Return.
You will see 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 see a Finder window that’s open, go to 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.
You will see some extra files in the list 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 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.
You will see:
/Users/ladmin
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:
Mac-17:Library ladmin$
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:
/Users/ladmin/Library
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. You will see output like this:
/Library
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.”
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.
Open Terminal.
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 you see the name of the command and a very 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. Generally speaking, 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 describes the various uses of the command, follows the synopsis.
Press the Q key to quit viewing the man page for ls.
At the prompt, type man less.
The less option enables you to view one man page at a time.
Use the Space bar to read through the man pages one page at a time.
After you’re comfortable with the less option, read the ls man page.
At the prompt, enter man man.
Read about the man command.
You can also Control-click, or on a MacBook Pro with Touch Bar, tap the icon for a man page to open it.