Alex Kaloostian

Apple Certified Master Trainer | Systems Integrator | Video Editor | Motion Graphics Artist

Command Line Basics 5 – paths


Where are you? In your cubicle, in an office, in your living room, a public library, on a blimp? Okay, probably not on a  blimp. But you are somewhere right now, as you read this. You can look around and figure it out. And if you opened a window on your Mac, you would know where you were as well, because it would say so at the top of the window, like this:

But where are you when you open the Terminal? Turns out, it’s right there in front of you as well, you just may not have noticed, or known how to read it. Open a new Terminal window and don’t type anything, just look at the text to the left of your cursor. This is called the prompt:

And this is actually a lot of useful information in a little place. The lion-Apps: is the computer you’re on. That might be obvious, I mean, its sitting right there on your desk in front of you, right? But when you start logging into machines remotely, that can be a very helpful reminder. That little squiggle, ~, is called a tilde, and it tells you what folder you’re in. More on that in a second. fmcadmin is the user you’re logged in as. And finally, the $ tells us we are using the bash shell. There are different shells out there, but you really don’t need to worry about that at this point. All Macs use the bash shell by default.

Okay, wait, tilde? Whats that? Well, in order to explain that, we need to take a step back and talk about paths. A path is like a set of directions to get somewhere.

Imagine you were the security guard in a big museum. You’re sitting there at your desk inside the Baroque impressionist gallery, and a kid comes up to you and asks for directions to the mummies. How would you tell him to get there? You could be really specific, and say “Go out of this room to the main impressionist gallery, turn left into the sculpture garden, go through there to the other end, you’ll see the post modern gallery, go through there back to the main lobby. Then go up the stairs and turn left and into the Egyptian gallery.” But why tell the kid how to get all the way back to the main lobby? He probably came in through there. So start from that common reference point: “Go to the main lobby, up the stairs, turn left into the Egyptian gallery.” That’s much easier.

The cd command is the Change Directory command, it’s used to go to a particular folder (just like the old DOS days, huh?) You simply type cd folder to go into a folder, and cd .. to go back out.

Here’s a typical Mac, fresh out of the box without a lot of files on it yet:

Now, pretend you were inside your Music folder, and I wanted you to go into your Applications folder. Going back to the security guard example, I could be really specific, and tell you to go out of your Music folder to your main home folder, then out of your home folder into the Users folder, the  out of the Users folder and into your hard drive, and finally into the Applications folder.  It would look like this:

Lion-Apps:Music fmcadmin$ cd ..
Lion-Apps:~ fmcadmin$ cd ..
Lion-Apps:Users fmcadmin$ cd ..
Lion-Apps:/ fmcadmin$ cd Applications
Lion-Apps:Applications fmcadmin$

But that’s reeeaaaaly tedious. We could shorten it, by taking all those steps and mushing them together on one line, with slashes. Like this:

Lion-Apps:Music fmcadmin$ cd ../../../Applications
Lion-Apps:Applications fmcadmin$

But even I lost track of all those dots and slashes! It would be much easier to start from a common frame of reference. Remember the main lobby? Well the “main lobby” on our Mac is the root of the hard drive. On PCs, its well known as the c drive, or c: . On a Mac, its simply . (No dot, just a slash. That dot was a period. Terminal commands and good grammar sometimes butt heads).

Lion-Apps:Music fmcadmin$ cd /Applications
Lion-Apps:Applications fmcadmin$

See that? Much easier. That slash refers to the root of the hard drive, and that slash if very important: If I tried typing

cd Applications

It probably wouldn’t work, because it would be looking for a folder named Applications, in the current location. And there is no folder named Applications in your Music folder, or in your System, or in your home folder. There’s (usually) only an Applications folder at the root of your hard drive, so that command would only work if you were at the root of your hard drive. On the other hand,

cd /Applications

Will always work, because you are basically saying “I don’t care where you are, get thee to the root of the home folder, and look there.” It’s the same for everyone, every time. This is known as an absolute path, while the previous is a relative path, because it is relevant to where you currently are. And if you’ve ever tried to support people over the phone before, you know its nearly impossible to guess where they are.

Here are some more commands, try to guess where they will take you, then try them out.

cd /
cd /Library
cd /System/Library
cd /Users/Shared
cd /Users/yourname

That last one presents a little issue. Did you type your name, or did you type “yourname”? It was kind of vague. You may not know what a particular user’s name is, and if you’re writing documentation, you have no idea who will be reading it. So how can you make sure they enter the right name, or even worse, how can you make sure a script runs on any computer as any user? Finally we get back to that tilde.

Lets say your user name is bruce. You could go to your Music folder with

cd /Users/bruce/Music

But you can make is even shorter, and type

cd ~/Music

That ~ is a shortcut meaning the currently logged in user. If I was to write a script with the first example, bruce, it would only work if there was currently a user named bruce on the computer. But if I used the ~, it would always work. And its shorter. Win-win.

Lets review. When going to a folder, listing a folder, or in any other way referring to a folder or file, / is shorthand for the root of the drive, and ~ is shorthand for the currently logged in user. This is very important, because there can be multiple folders in multiple places with the same name. For example, think of your Library. There are at least three libraries on any Mac: at the root of the hard drive, in the System folder, and in the user’s folder. Think of how you could list the contents of each?

ls /Library
ls /System/Library
ls ~/Library

The first is the root Library, the second is the System Library, and the third is the user Library. One more question. If I type this:

ls Library

What will happen?

Trick question! It depends on where you are. If you’re in your home folder, it will list the contents of the Library in your home folder. If you’re in the System folder, it will list your system library. If you’re in, say, your Applications folder? It won’t work at all, because there IS no folder in there named Library! So remember, that slash at the beginning, or a lack of one, makes all the difference. No slash, it will look where you currently are. Slash, it will look at the root of your hard drive every time.

And remember, the prompt is always there to remind you where you are:

Lion-Apps:Library fmcadmin$

But sometimes even the prompt is vague. This is telling me I’m on the Mac named Lion-Apps, in the Library folder, logged in as fmcadmin. But which Library folder am I in? Egad! Not to worry, there’s a command for that, too:

Lion-Apps:Library fmcadmin$ pwd

That’s better. I’m inside fmcadmin’s library. pwd stands for Print Working Directory. And that’s enough for today. Happy navigating!

Author: alexkaloostian

I'm a video editor, motion graphics designer and Mac IT consultant in the Boston area.

2 thoughts on “Command Line Basics 5 – paths

  1. Two little “cheats” that are always helpful to me:
    1) Tab completion. Tired of writing cd Applications? Instead type cd Ap and then hit the tab key. This works for both files and folders, and the shell will autocomplete the name of the file/folder it finds.
    2) Have the folder open in the Finder but do not want to type in the path? Type cd and then drag the folder into the Terminal window and hit return. Drag and drop, bringing the ancient shell into this century!

  2. Pingback: Command Line Basics 7 – review time | Alex Kaloostian

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s