From Coder Merlin
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Directory Hierarchy[edit]

Linux File Hierarchy

All files are integrated into a single, hierarchical directory structure that appears as an inverted tree. A directory provides a logical grouping of related files (and potentially other directories). The top of this tree is called the root and is referred to by a slash (/). In the diagram, we see that the root contains five subdirectories (i.e., a directory below the parent directory): bin, etc, home, lib, and tmp. The home directory contains two subdirectories, jack-williams and john-williams. Note that this differs from a Windows file structure because Unix integrates all files into a single directory structure rather than listing different physical storage devices. Thus, the file structure is logical rather than physical. A path describes an ordered transition through one or more directories on the way to a destination (either a directory or a file). As such, note that although both jack-williams and john-williams each have a subdirectory named "projects," these are different directories. The path to the former is "/home/jack-williams/projects" and the path to the latter is "/home/john-williams/projects". Paths that begin at the root (symbolized by the initial slash) are called absolute paths. All other paths are relative paths.

Observe, Ponder, and Journal: : Section 2
  1. It appears that both Jack Williams and John Williams can have a directory of the same name. Do you think the ability to have two different users create a directory of the same name is important? Why?

Display the Working Directory (Where am I?)[edit]

The shell uses a concept known as your working directory, which can be thought of as your current directory, i.e., where you are now in the file system. If you forget where you are, you can always use the command pwd to print the working directory. Try it now. Type "pwd" followed by <RETURN>. Note that you'll need to press <RETURN> after any command before the shell executes the command.

john-williams@codermerlin:~$  pwd


The command prints something similar to "/home/john-williams" and then presents you with a new prompt and cursor to indicate that it's ready and awaiting your input. This directory is your home directory and it is indicated by a special symbol: the tilde (~).

Observe, Ponder, and Journal: : Section 3
  1. Why do you think the home directory has a special symbol to represent it?

Create a New Directory[edit]

To keep our Merlin Missions organized, let's place them all under a directory named "Merlin." To make a new directory, we'll use the command mkdir to which we'll provide a single argument, the name of the directory that we want to create. Try it now. Type "mkdir Merlin" followed by <RETURN>.

john-williams@codermerlin:~$  mkdir Merlin

Display the Contents of a Directory[edit]

How do we find out what’s contained in our current directory? We can use the ls (that's a lowercase 'L' followed by a lowercase 'S') command to list the directory contents. Try it now. Type "ls" followed by return.

john-williams@codermerlin:~$  ls

The command prints the contents of the current directory (in this case, our home directory), which will include the single directory, "Merlin," that we created earlier.

Change to a New Directory[edit]

When we want to change our current directory we use the cd command, followed by the name of the directory to which we want to change. Try it now. Type "cd Merlin" followed by return.

john-williams@codermerlin:~$  cd Merlin


Observe, Ponder, and Journal: : Section 4
  1. Did the prompt change? If so, how? Why is this useful?

Note that our prompt has now changed. Before the command was executed, the prompt contained a single tilde indicating that our home directory was our current directory. After executing the command, our prompt now includes a tilde, followed by a slash, followed by "Merlin." This indicates that we are in the directory named "Merlin" below our home directory. The slash is a delimiter separating the levels of our hierarchy.

Let's try the pwd command again.

john-williams@codermerlin:~/Merlin$  pwd

We can now see that from the root, we're three levels down:
/ (root)

The cd command has a useful shortcut. If we type the command without an argument, it takes us back to our home directory. It's the equivalent of "cd ~". Let’s try it now:

john-williams@codermerlin:~/Merlin$  cd


Now, move back to the Merlin directory by issuing the appropriate command.

Change to a New Directory Temporarily[edit]

When we want to change our current directory temporarily (meaning we want to return later to where we are now), we use the pushd command followed by the name of the directory to which we want to change. Try it now. Type pushd Merlin followed by RETURN.

john-williams@codermerlin:~$  pushd Merlin


We can now perform any actions we'd like in this directory and when we're done, we can easily return from whence we came without needing to type in the original directory name. The popd command looks up where we came (when we used pushd) and returns us to there. Try it now. Type popd followed by RETURN.

john-williams@codermerlin:~/Merlin$  popd