Directories in UNIX

Note

If you’re one of those people who’s only used a GUI, or you save all of your files on your Desktop, this sub-section is for you. There are plenty of web sites that discuss directories; this is just a brief overview.

The “folders” that you see when you look at your GUI are actually directories in your operating system. That tells you what a directory is: a container for other files, including other directories. The separator for directory names is “/”, so a/b/c is directory c within directory b within directory a.1

Everything in UNIX is within a directory. Yes, even your Desktop; typically, that is a directory whose name is ~/Desktop. That leads us to common abbreviations and commands for directories when you’re using the command line:

  • ~<account> means the home directory of the user <account>2. Just plain ~ means your own home directory. So ~/Desktop means a directory named Desktop within your home directory.

  • cd is the command to “change directory.” It’s the usual way to go from one directory to another. If there were a directory named Root3 in your home directory, you could visit that directory with:

      > cd ~/Root
    
  • .. is a reference to your parent directory, the one “above” the one you’re currently in. If you wanted to return to your home directory from ~/Root, you could type:

      > cd ..
    

    If you use the cd command without any arguments, it will return you to your home directory:4

      > cd
    
  • To look at the contents of your current directory, use the ls command:

      > ls
    

    You can also list the contents of any other directory (for which you have permission to view):

      > ls ~seligman/root-class
    
  • If you forget which directory you’re in, use the pwd (“print working directory”) command:

      > pwd
    
xkcd porn_folder

Figure 2: https://xkcd.com/981/ by Randall Munroe. Moral: Be careful how you organize your directories!


1

Now you know why it’s hard to put a / in a folder name: The operating system can’t tell the difference between a / that’s within a folder name versus a / that is a directory separator.

2

It’s always something like “~seligman” (tilde-seligman), never “–seligman” (dash-seligman). Depending on the exact font used to print or display this tutorial, sometimes tildes look like dashes. On most keyboards, tilde is typed with SHIFT-` where ` (backtick) is near the upper-left-hand corner of the keyboard.

3

UNIX is normally a case-sensitive operating system. ~/Root, ~/ROOT, and ~/root are three different directories. Exception: In Mac OS Darwin, file names are case-insensitive; all three of those directories would be the same.

4

Knowing this will become useful in the future, as you become more sophisticated in your use of UNIX. Eventually you’ll learn about shell variables. Sooner or later, you’ll make a typo in a variable name; e.g.,

cd $ROTSYS

Instead of going to $ROOTSYS, your intended destination, you’ll find yourself in your home directory. That’s because $ROTSYS doesn’t have a value, so UNIX interpreted this as the cd command without any arguments.