3. Managing Directories

We will do a top–down exploration of the file system. In this spirit, we will first learn how to manage directories; this is the UNIX name for folders. Directories are actually files: they contain links to the items “inside” of them. You will want to know how to create and manage directories, and how to navigate through them.

You have been in a directory all along without knowing it. Whenever you start a UNIX session, you begin in your home directory. Every user on a UNIX system owns a home directory. This is where you will keep all of your stuff. You will see that ownership of stuff is baked right into a UNIX system, and you will learn how to control what others see of your stuff.

To see your home directory, type pwd at the UNIX prompt. This command means, “Print working directory!” You will see something like this.

unix> pwd
/home/faculty/morrison

This directory is your home directory. Whenever you start a new session, you will begin here.

In this example,morrison is a directory inside of faculty, which is inside a directory home, which is inside the root directory, /. Your home directory will likely be slightly different. It is very common for UNIX systems to keep all user directories inside of a directory named home. Often, several different types of users are organized into sub-directories of home. Enter the pwd command on your machine and compare the result to what was shown here. Become familiar with your home directory’s appearance so you can follow what goes on in the rest of this chapter.

If you are using Linux on your PC, your home directory will likely look like this.

/home/morrison

On a Mac it looks like this

/Users/morrison

This directory structure is exactly the same as your hierarchy of folders and files you have dealt with on a modern computer. You already know that folders can contain files and other folders. This is also true in a UNIX environment.

To make a new directory in Mac or Windoze, you right click in the open folder and choose a menu for making a new folder. In UNIX, the mkdir command makes a one or more new directories. The mkidr command requires at least one argument, the name(s) of the director(ies) you are creating. Let us make a directory by typing

unix> mkdir Projects 

makes a directory called Projects; this directory is now empty. We can always get rid of an empty directory or directories by typing the rmdir command like so.

unix> rmdir garbageDirectory(ies) 

In this case, garbageDirector(ies) stands for the directory or directories you wish removed.

The rmdir command will not remove a directory unless it is empty. There is a way to snip off directories with their contents, but we will avoid it for now because it is very dangerous. For now, you can delete the contents of a directory, then remove the directory. Be warned as you proceed: When you remove files or directories in Linux, they are gone for good! There is no “undelete.”

If you got rid of the Projects directory, re-create it with mkdir. To get into our new directory Projects, enter this command.

unix> cd Projects 

and type ls. You will see no files. This is because the directory Projects is empty, and ls by default only shows you the files in the directory you are currently occupying. The command cd means, “Change directory!” Having done this now type

unix> pwd

You will see a directory path now ending in Projects.

There is a command called touch which will create an empty file(s) with a name(s) you specify. Create files named moo and baa with touch as follows.

unix> touch moo baa 

Then enter ls at the command line. This command means “list stuff.” You will see just the files you created.

As we said before, The command ls displays only files in the directory you are currently occupying. This directory is called your current working directory, or cwd for short. Every terminal session has a working directory. When you first log in, your working directory is always your home directory. Now that you have changed your directory, you are here.

unix> /home/yourUserName/Projects 

This directory is the Projects directory you just created.

If you type cd without arguments, you will go straight back to your home directory. This should make you will feel like Dorothy going back to Kansas. Now if we use pwd again we see our home directory printed out.

image of ruby sliipers

You can also see your home directory anywhere you are by typing

unix> echo $HOME

The fearsome–looking object $HOME is just a symbol that points to your home directory. There are various items like this present in your system. They are called environment variables. Other examples of environment variables include $PWD, which is just your current working directory and $OLDPWD which is your previous working directory.

Programming Exercises

  1. Navigate to a directory. Then enter this.

    unix> pushd

    Then navigate to another directory and repeat this a few times. Now alternately type

    unix> popd
        unix> pwd

    What does this do? Think of Hansel and Gretel!

  2. Crawl around in your directory structure. Each time you enter a new directory type

        unix> echo  $PWD
        unix> echo  $OLDPWD
  3. Use cd to change into some directory. Then type cd - and then pwd. Repeat this. What does - mean?

3.1 Processes and Directories

We know that when we log in, we are starting a program called a shell. The shell is a process, or running program. Every process has a cwd (current working directory). When you type pwd into your shell, you are asking the OS to tell you your shell’s current working directory. If you log in to a UNIX server in several terminal windows, each runs in a separate shell, so each has can have its own working directory.

Observe that, much of the time, your shell is idle. When you finish typing a command and hit the enter key, that command launches a program, that program runs, and any output is directed to your terminal window.

The command cd is a computer program. What it does is it changes the cwd of the shell that calls it. Now you know what it means to be “in” a directory: it means the cwd of your shell is that directory.

Programming Exercises

  1. Enter

    unix> cd $HOME/Projects

    and see what happens.

  2. Make these directories inside of Projects labors, feats and chores

  3. Type cd labors at the command line then pwd.

  4. Type cd .. at the command line then pwd. What happened?

  5. Type cd .. at the command line again, then pwd. What happened?

  6. What do you think .. is?

  7. Type cd . at the command line then pwd. What happened?

  8. Type ls . at the command line then pwd. What happened?

  9. What do you think . is?