Browsing files from the commandline

By: Jeremy W. Sherman. Published: . Categories: Under the Hood. Tags: terminal.

Open a folder in Finder. Double-click on it, and you get a window. Look at that window’s title bar. In the middle of the title bar, it says maybe, “Documents”. Where’s that? Maybe you switch to column view and arrow left for a while, or maybe you hit Cmd-Up to go up a few folders, or command-click the file icon in the title bar and look at the drop-down. After clicking and tapping and staring for a bit, you finally figure it out: I’m in the Documents folder in my user directory on the hard drive named My Mac.

That just took way too long.

Go open Terminal.app. You’ll see a mostly empty tab with some text and a cursor, something like:

Documents$ _

That text is called a prompt, because it’s giving you a bit of information (where you’re at) and then asking you, “What next?” Pretend you are that terminal, that cursor. You are standing in the middle of the filesystem. Where are you? You could do like you did in Finder and poke around for a while and figure out the answer. But you don’t need to do that; this is the Terminal.

Instead, you can just ask directly, “Where am I?” Type in pwd and hit return to send the command, and you’ll see an answer come back with something like:

/Users/Me/Documents

Or, in words, the Documents folder in the Me folder in the Users folder on your main hard drive (which is not named, here). Or, in the usual words, so you can read left-to-right, “slash Users slash Me slash Documents”. You asked, it answered. “pwd” is actually short for “present working directory”. “Present working directory” is a more impersonal name for where you (via the terminal) are standing right now.

What’s in /Users/Me/Documents? Take a look around with ls, short for “list”. It will print out a list of files and folders (directories) in that directory. You can work with those files, or you can go into any folder you can see in there using cd (“change directory”).

Two folders are always present in each folder, and so ls omits them from its list by default. The folders have short names: “.” (read “dot”) means “the current directory (whatever it happens to be now)". “..” (read “dot-dot” or “double-dot”) means “the parent of the current directory”; if you’re in /Users/Me/Documents, then “.” refers to Documents, and “..” refers to /Users/Me.

Let’s move to “..". Type in cd .. (“change directory to ..") and you will see the prompt that comes back now reports that you are in the parent of your old directory. Use pwd to confirm this. If you keep going up, eventually you’ll hit the top, and cd .. will take you right back to that same place (or do nothing, depending on how you look at it). This place is called the root directory, and it’s named / (forward slash; slash or solidus to its friends). From the root directory, cd . and cd .. mean the same thing, and neither actually takes you anywhere different from where you started in /.

You can move back down the directory tree using cd name, where name is the name of a folder. From /, cd Users changes your current directory to /Users. From /Users, cd Me changes your current directory to /Users/Me. Move back up a level with cd .. and you’re back at /Users. Got it? Good.

Maybe you’re wondering about that “directory tree” phrase that I just slipped in. Why “tree”? It’s a tree because it has a root ("/") that then branches out. Each branch is a folder or file. Files can’t have branches; they are dead-end leaves. Folders can have branches, either more folders or files.

You might also hear terminology taken from genealogical trees instead: a folder can be a parent to several children, and it has a parent directory of its own. And if you hear talk of “moving up”, that means towards the root directory (cd ..); and “moving down” means away from the root directory, by cding into another directory (one other than . or ..).

So now you can ask, “Where am I?” with pwd. You can take a look around your present working directory using ls, which lists the files and folders in that directory. And you can change directories to go both up and down the directory tree using cd. And now browsing your files is one less thing you’ll need Finder for.