Lesson 2: Navigating the File System
In the first installment of this series, we looked at the basics of using the Terminal to access Mac OS X’s Unix core. What’s next? Well, when you visit a new place, the first thing you need to learn is how to get from one location to another. So, let’s look this time at how you use the Terminal to move around your Mac’s file system. (You might want to go back and read the previous article again to refresh your memory before continuing.)
As you are certainly already aware, your files are organized in a hierarchy of directories (the Unix word for folders). When moving around in the Finder, you open folders, which display other folders and files, and you drag, copy and paste files and folders among the windows that the Finder displays. In the Unix world, you navigate among directories using commands, displaying lists of files and directories, and you move, copy and paste files and directories from the command line.
In most cases, you’re probably better off using the Finder for your everyday activities. But navigating the file system with the Terminal can have a few advantages:
It’s fast. You can jump from one directory to another in a split-second, as long as you know its path. There’s no need to open window after window to get where you want.
It’s informative. While you can access most of the same information from the Finder – such as file permissions – the Terminal displays these details more quickly. For example, you can use the Finder’s Get Info window to learn the permissions for an entire folder of files, but you must do so individually for each file. With the Terminal, you can display a list showing permissions for all the files instantly.
It’s complete. You can access invisible files and directories that aren’t accessible via the Finder (without special utility software). In most cases, you’re better off leaving these files and directories alone, but if you need to see or work with invisible files and directories, the Terminal may be the best way to go.
Navigating the Mac OS X File System — Mac OS X, like most other operating systems, uses a file system to organize files and folders. The basic principle behind this system is that of hierarchy: at the top of the hierarchy is a single point (called the "root" of the file system, indicated by / in Unix systems), and below that point, a tree structure spreads out from directory to sub-directory.
In the Finder, you can easily see tree structure by looking at a window in column view. Open a new Finder window, choose View > As Columns, click on your startup disk, then Users, and then your user name. You should see at least eight folders in the fourth column. You’re looking at the contents of your Home folder. Each of its sub-folders can contain other files and folders, and so on.
The rest of this article will show you how to move around in your Mac’s file system using the Terminal. You will learn how to display the contents of a directory, how to change directories, and how to determine where you are.
Bear in mind that whenever you open the Terminal you are at a specific location in the file system. Unless you’ve modified how the Terminal works on your Mac (at which point you probably don’t need these articles), you will always begin in your Home directory, the one with your user name (such as mine, called kirk). No matter what you do in the Terminal, you are always someplace.
Finding Where You Are with pwd — When traveling, you sometimes need to look at a map to find where you are. The same goes for the Terminal. The pwd command (print working directory) does just that by telling the Terminal to display the full path to the current working directory.
[Walden:~] kirk% pwd /Users/kirk
I left the complete prompt visible in the above example to show you the difference between the directory information visible in the prompt (the ~) and what pwd displays. The only difference is that my user’s Home directory is replaced by the ~ shortcut. But look at the next example. When I’m outside my Home directory, there’s no difference between what is shown in the prompt and what pwd returns.
[Walden:/Library/Preferences] kirk% pwd /Library/Preferences
Listing Directory Contents with ls — The ls command (list directory contents) is one of the most useful and commonly used commands for navigating the file system. You’re used to seeing the contents of folders when opening new windows in the Finder; when you move to a new directory in the Terminal, you see nothing. The Terminal only displays the contents of a directory after you tell it to with the ls command.
When you are in your Home directory, and run the ls command, you should see something like this:
% ls Desktop Library Music Public Documents Movies Pictures Sites
The ls command returns a simple list of the current working directory’s contents, but nothing in this list tells you anything about what these items are. Luckily, the ls command offers a plethora of options on how to display the contents of a directory, and what type of information is shown.
Viewing the Contents of a Different Directory — The ls command displays the contents of the current working directory (the one the Terminal is in) if you do not add the name of a directory to the command as an argument. You can list the contents of any directory on your computer, as long as you know its path. To do this, you must specify the full path of the directory. For example, if you want to list the contents of the /Library/Fonts directory, enter the following:
% ls /Library/Fonts Arial Cochin.dfont HelveticaNeue.dfont Arial Black Comic Sans MS Hoefler Text Arial Narrow Courier New Optima.dfont [etc.]
You must use the complete path of the directory /Library/Fonts/, because you are specifying an absolute file path. However, you can also use ls to display the contents of directories using relative file paths. When in my Home directory, I can display the contents of the Pictures directory by simply entering the following:
[Walden:~] kirk% ls Pictures/ iPhoto Library white background.jpg
As you can see from the prompt, I am still in my Home directory (~). If you don’t specify an entire file path beginning with /, ls assumes you want to start from the current working directory.
Viewing the Entire Contents of a Directory — The basic ls command displays the contents of a directory in a simple manner. But what this command doesn’t tell you is that there are other items in the directory. In fact, every Mac OS X directory contains invisible items, either files or folders. To view the entire contents of a directory, use the ls command with the -a option. (Note that options go before the directory path, if you’re listing the contents of a specific directory as discussed just previously.)
% ls -a . .Trash Desktop .. .addressbook Documents .CFUserTextEncoding .addressbook.lu Library .DS_Store .lpoptions Movies [etc.]
Your Home directory may contain different files, but many of them are the same. As you can see, there are a series of files beginning with . (dot). This dot indicates that a file is invisible, and the ls -a command displays all files, including invisibles. While you won’t need to access most of these files (and messing with some of them could be harmful to your system), you can see them and know they are there. (Later in this article, when examining the cd command, we’ll see what . and .. do.)
Viewing Long Information — Another option to the ls command, -l (that’s a lowercase L), returns much more complete information on the contents of a directory, going much further than what you can find out from the Finder’s Get Info command. If I run ls -l in my Home directory, the Terminal returns the following information:
% ls -l total 0 drwxr-xr-x 3 kirk staff 102 Nov 9 11:22 Applications drwxr-xr-x 8 kirk staff 272 Nov 11 14:46 Desktop drwxr-xr-x 24 kirk staff 816 Nov 11 13:07 Documents drwxr-xr-x 44 kirk staff 1496 Nov 10 22:49 Library [etc.]
When you use ls with the -l option, the following information is displayed:
The first line shows the number of 512-byte blocks used by files in the directory. In this example, there are no files – only directories – so it reads total 0.
The first part of each line shows the file or directory’s permissions.
The next part is a number, showing the number of file system links the file or directory uses.
Then comes the user name. In the above example, I am in my Home directory, so all the files belong to me.
Next is the group the user belongs to. In this case, I belong to staff.
The next number is the size of files in bytes, or the number of directory entries. This number is useful for files; the directory number is less useful.
The date and time shown correspond to the last time the file or directory was modified. If the file has not been modified in the last year, the date and year are shown.
Finally, the name of the file or directory is shown.
If you use ls -l in other locations, you may see additional information:
Items belonging to other users, such as root.
Additional groups, such as wheel, unknown, and admin, in addition to staff.
Some items contain -> in their names. These are symbolic links, similar to Mac OS aliases. For example, var -> private/var is a symbolic link pointing from var to private/var.
While the ls -l command shows the size of files, there are some cases where this figure is incorrect. Adding the -s option adds a column, at the left of the list, showing the number of 512-byte blocks each file takes up on disk. Look at the following example:
% ls -ls 328 -rw-r–r– 1 kirk staff 30819 Nov 14 18:06 Perceval.jpg 64 -rw-r–r– 1 kirk staff 30819 Nov 14 18:07 PercevalCopy.jpg
These two files seem to have the same size (30819 bytes) but the first file takes up much more space on disk. This is because the first file, a picture, has an additional resource that the Terminal does not see. This thumbnail takes up much more space than the actual file, and the Finder clearly shows the difference in size. But the Terminal does not unless you use the -s option.
Changing Directories with cd — When navigating your Mac’s file system in the Finder, you have two choices: you can either have the Finder open a new window when you open a folder, or it can open the new folder in the same window. When working in this second way, you always have just one window open, and you can go backward and forward in your file system hierarchy, but always within the same window.
Moving around in the Terminal works in this second way: no matter where you move to, you are always "in" one directory. As we saw above, the Terminal prompt tells you which directory you are in, and the pwd command tells you where you are in the file system hierarchy. So, remember that, when navigating the file system in the Terminal, you are always in one location.
The cd command (change directory) moves you from one directory to another. In Unix lingo, it changes the working directory; this is the directory in which you are located, and the one in which you can act on enclosed files or directories without specifying a file path. Let’s start by looking at the following:
[Walden:~] kirk% ls Desktop Library Music Public Documents Movies Pictures Sites
The above shows the contents of my Home directory after executing the ls command. To move to the Library directory, type the following and press Return:
% cd Library [Walden:~/Library] kirk%
The prompt changes to reflect the current working directory. (If you need to move into a folder whose name contains more than one word, enclose the folder name in quotes.) Then, to see what it contains, type ls and press Return.
% ls Addresses FontFavorites.plist Preferences Application Support Fonts Printers Assistants Fonts (Disabled) Recent Servers Audio Frameworks Screen Savers [etc.]
You can move into one of these directories with cd:
% cd Addresses [Walden:~/Library/Addresses] kirk%
And, look at its contents, using ls:
% ls Address Book.addressbook Addresses.addressBook
At this time, you may have forgotten where you are in your file system. Use the pwd command to learn your location:
% pwd /Users/kirk/Library/Addresses
You can now use the cd command to move back up in the hierarchy by running cd with the complete file path of the folder you want to move to. Say you want to go back to the Users/ directory; use the following:
% cd /Users [Walden:/Users] kirk%
The prompt shows that you are in the Users directory. If you want to go back to the Library directory, where you were before, you could type the following:
% cd /Users/kirk/Library/ [Walden:~/Library] kirk%
If you attempt to move into a directory that does not exist, or if you misspell the name of a directory, you see this message:
% cd Proust Proust: No such file or directory.
Using the Finder to Save Time in the Terminal — While you may think that the Finder and the Terminal are mutually exclusive, the two can work together to save you time. Sometimes, you may need to type a lot to get into a deeply nested directory, but it’s a cinch to navigate there in the Finder. If you want to move into a directory using cd, type cd [space] in the Terminal. Then switch to the Finder, look for the folder you want to move to, and drag that folder into the Terminal window. The Terminal adds the path of the item you drag to the command you just typed.
[Walden:~] kirk% cd /Users/kirk/Library/Preferences
Press Return, and that directory becomes your current working directory.
You can do the same thing to act on a file. Just type the beginning of the command you want to use (don’t forget to type a space before dragging the file), then locate the file in the Finder, and drag it to the Terminal window.
Going Home in a Jiffy — It may seem as though using the cd command requires a lot of typing, and in its basic usage, that can be true. However, cd also offers many shortcuts to let you navigate quickly and easily through the file system. The most useful shortcut is to type just cd, then press Return, which we do while our current working directory is the Addresses directory, as you can see in the prompt below.
[Walden:~/Library/Addresses] kirk% cd [Walden:~] kirk%
As you can see from the second prompt, cd by itself takes you to your Home directory. (Remember, the tilde character (~) is a shortcut for the current user’s Home directory.) The reason for this shortcut is that Unix assumes most users will only be working in their Home directory or one of its sub-directories. For this reason, the tilde (~) shortcut for your Home directory also lets you move easily to sub-directories within your Home directory. If you want to move to your Documents directory, you can type the following no matter where you are:
% cd ~/Documents [Walden:~/Documents] kirk%
Using the ~ shortcut helps save you from typing the full file path of the Documents directory.
Upstairs Downstairs — Every directory in your file system contains at least two entries. When you create a new, empty directory, these two "files" are automatically added to them. They are . (dot) and .. (dot dot). Look at the following:
% mkdir New_Directory % cd New_Directory % ls -a . ..
In the first line above, I created a new directory, and, in the second line, I moved into that directory. I then ran the ls -a command, which displays the contents of that directory; the -a option shows invisible files. You can see that the only files in this new directory are . (dot) and .. (dot dot).
These two files are useful. The first one, . (dot), represents the current working directory, and can save you time in certain commands involving files within that directory. The second one, .. (dot dot), is a shortcut to the parent directory, or the next directory up in the file system. No matter where you are in the file system, you can always type the following:
[Walden:~/Documents/New_Directory] kirk% cd .. [Walden:~/Documents] kirk%
As you can see from the prompts in this example, typing cd .. takes you up to the parent directory of New_Directory, or Documents. Any time you move into a sub-directory, using the cd command, you can use cd .. to back up. But you can also move up the file system from the current directory until you reach the top. Look at the following:
[Walden:~/Documents] kirk% cd .. [Walden:~] kirk% cd .. [Walden:/Users] kirk% cd .. [Walden:/] kirk%
As you can see in the prompts, I was in my Documents folder, then moved up three times to reach the root level of the file system, or /.
You can now combine the cd .. command with cd – to move up and down in the file system, but, remember, the cd – command only moves back one step. There is no simple equivalent (in the cd command) to move down the file system to where you started.
[Walden:/Users] kirk% cd .. [Walden:/] kirk% cd – [Walden:/Users] kirk%
In the first line, I moved up from /Users to /; in the second line, I moved back to the previous working directory, /Users.
Opening a Directory in the Finder — Sure, the Terminal lets you move around quickly and easily, yet all but the most die-hard Unix fans will eventually yearn to see icons again. Wherever you are in the Terminal, no matter what directory you are in, you can run the following command at any time to open the current working directory in a new window in the Finder:
% open .
You will recall that . (dot) represents the current working directory. The open command lets you open many kinds of items just as if you double-click them. The open command even lets you open directories that are normally hidden from view by the Finder, thus giving you the best of both worlds.
Home Again — This article showed you how to move around your Mac’s file system, and how to see what its folders (or directories) contain. You learned about how the Mac OS X file system works, and saw some of the differences between what you see in the Finder and what Terminal shows you. You discovered some of the basic commands for navigating your computer, which are the command-line equivalents of double-clicking folders and reading their contents:
- pwd: tells you where you are.
- cd: takes you into another directory.
- ls: shows you what your directories contain
- open: opens a directory in a new window in the Finder.
- You also learned how to use the ~ (tilde) character as a shortcut for your Home directory.
In the next installment, you’ll learn how to move and copy files, how to create directories, and how to delete files and directories, rounding out the basics of command-line navigation.
[Kirk McElhearn is a freelance writer and translator living in a village in the French Alps. This article is an excerpt from his forthcoming book, Unix for Mac OS X: Learning the Command Line, to be published by Addison-Wesley in September 2003.]
PayBITS: Did this article help you dip your toes into Terminal?
Consider supporting Kirk with a few bucks via PayPal!
Read more about PayBITS: <http://www.tidbits.com/paybits/>