Skip to content

Learning the Terminal on the Mac - Part 2 - The Shell and Paths

In the first article we learned how to launch Terminal and get started using the shell. Also we covered some basic commands for moving around, including cd, pwd and ls.

In this action-packed episode, we are going to take a closer look at directories and paths. Usually. I would try to cover material in a bit more breadth before going into depth on a particular topic. However, an understanding of paths is important foundational knowledge, and will be very useful for our next topic, basic file manipulation.

Before diving into paths and directories, I want to share a little more background information about the shell, and also some tips on how to get yourself out of trouble.

About the Shell

This “shell” thing we have been talking about is actually a kind of program. There are several different shells available on the Mac, but the most popular and the default is called bash. Bash stands for “Bourne-again Shell”, was created over 20 years ago and it is still being actively developed[1].

Due to it’s long history in the UNIX world, bash sometimes isn’t the most user-friendly piece of software. Sometimes you can make a typo that puts your shell into a state you don’t understand. Fortunately, all hope is not lost.

Getting out of Trouble

You’re probably familiar with some of the common Mac OS X keyboard shortcuts, such as ⌘S (Save), ⌘P (Print), and ⌘Q (Quit). Most keyboard shortcuts in the graphical part of Mac OS X use the ⌘ (Command key) modifier. These shortcuts are also used in Terminal.app for quitting, closing windows, etc.

However, remember that Terminal.app loads a shell, bash, inside of it. Bash uses a completely different set of keyboard shortcuts, which use the Control key. This key is usually labeled “ctrl” on your Mac keyboard, and uses ^ as its symbolic representation.

If you know only one control sequence in the shell, than it should be ^C. Without going into too much detail, this keystroke is similar to “cancel” or “escape”. If you end up in some mode or program that you don’t understand, you can often get back to the prompt with just ^C.

Perhaps it is best explained with a haiku:

Lost or stuck in bash?

Prompt gone, output out of hand?

^C saves you

Then again, maybe not. Time to move on to our Feature Presentation: Directories and Paths.

Directories

As a Mac user you should already be familiar with using folders in the Finder. Well, a directory is just the more shell-oriented term for a folder, and since we’re working in the shell, we’ll talk in terms of directories most often.

Special Directories

There are a few “special” directories that we should touch on before going further.

The top-level directory in your Mac’s hard drive is referred to as / or the root directory in shell terms. All other directories are descendants of the root directory. It’s easy to navigate to / and see what is there, using cd and ls.

lt2-ls-root.png

The next special directories we need to mention are . (dot) and .. (dot dot). The directory . is just an alias for your current directory. If you type cd . at the prompt, essentially nothing will happen. The purpose of the . directory might not be immediately apparent, but will be because especially useful when we discuss file manipulation.

The .. directory is similar to ., except that it refers to the directory “above” your current directory, also known as the parent directory. Typing cd .. from your home directory will move you up to the /Users directory, and typing it again will put you in / (the root).

These directories will actually be shown in the output of ls if the -a option is specified:

lt2-dot-and-dotdot.png

Astute readers may be wondering about the effects of cd .. if your current directory is /. Well, fear not. You will not accidently change into some forbidden directory, whose contents, once seen, may not be unseen. When in root, cd .. essentially does nothing.

The final magic directory is ~. This is simply a shortcut to your home directory. Every user has one and only one home directory. On Mac OS X, all users’ home directories are in /Users directory. Also you may notice that your prompt shows ~ when you are in your home directory, rather than the full name of the directory (for basic prompt description, see the last article).

lt2-tilde.png

There is one important stipulation when using ~. It does not work inside either single (') or double (") quotes. This is due to the way ~ is implemented in the shell. All the other special directories mentioned in this section have no problems with quotes, however. Look to the escaping and examples sections for how to deal with ~ in paths.

To review:

  • /

    • The top-level directory of your computer’s hard disk.
    • Usually called root or slash.
  • .

    • The current directory.
    • Usually called dot.
  • ..

    • The directory that contains the current, or parent directory.
    • Usually called dot dot.
  • ~

    • Your home directory.
    • Usually called tilde.
    • Won’t work inside quotes!

Paths

We’ve already worked with paths a little bit in the last article with the cd command. Now we will talk about them in a little more detail.

The path separator on Mac OS X is / (called a slash or sometimes forward slash).

Kinds of Paths

There are two kinds of paths, relative and absolute. Relative paths reference files from your current directory, while absolute paths specify the complete location of a file or directory.

This is I how would change to the Downloads directory of my “demo” account using an absolute path.

cd /Users/demo/Downloads

And this is how I would change to the Downloads directory of my “demo” if I was already in my home directory (~).

cd Downloads

Both these commands get me to the same directory, we just got there by a different path.

lt2-ls-downloads-abs-1.png
lt2-ls-downloads-rel2.png

Above, we worked with paths to directories. Well, paths to files work the same way. I could open a file called “Notes.txt” on my Desktop using the absolute path:

open /Users/demo/Desktop/Notes.txt

or a relative path (shown from my home directory):

open Desktop/Notes.txt

Escaping

The shell gives special meaning to certain characters in order to implement all its features. If you are familiar with web programming, this is similar to how URLs are percent-ecoded.

The most notable of these characters is probably the space. The shell uses spaces to separate commands from options and arguments. However, this is a problem if the argument you want already has a space in it. This occurs in many files and folders on Mac OS X, like the “About Stacks.pdf” file we used in the last article.

Special characters are escaped using the \ or backslash character. This can be confusing since this character is used as the path separator on Microsoft Windows. It’s probably best if you can just wipe Windows from your mind entirely (zing!).

To escape a space in a file or directory, with prefix it with a \, like so:

open Documents/About\ Stacks.pdf

Alternatively, you can quote the entire argument, using double quotes ("):

open "Documents/About Stacks.pdf"

Finally, we don’t need to escape the entire path, just the part that actually needs the escaping:

open Documents/"About Stacks.pdf"

Actually, single quotes (') can be used instead, but they have a slightly different meaning. We’ll discuss the meanings of the different types of quotes soon.

Also, note that if you use tab completion the shell will be smart enough to do most of the escaping for you. If you’re hungry for more, you can read more about escaping in the Escaping topic in the Advanced Bash-Scripting Guide.

Path Examples

Here are a few examples of how to put all this knowledge together:

cd .. Go to the directory one level above
cd ../.. Go to the directory two levels above
cd ../../ Go to the directory two levels above (same as previous, extra /’s are ignored)
cd ~ Go to your home directory
cd ~/Documents Go to your Documents directory
cd ~/.. Go to the directory one level above your home directory (Users)
cd ~/Library/Application\ Support Go to your Application Support folder using escaping
cd ~/Library/"Application Support" Go to your Application Support folder using quotes (don’t quote the ~)
open ~/Documents Open your Documents folder in Finder
open . Open the current directory in Finder (see, the . is useful)

Wrap Up

We now have enough knowledge regarding paths to provide a solid foundation going forward. Next up, we’ll be talking about copying, moving, and deleting files. If you have questions, comments, or suggestions for future articles, please note the comments section below. I believe it will accommodate your needs.

One Trackback/Pingback

  1. [...] first few articles laid out a good foundation of basic shell usage, but little Mac-specific information. In [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*