Creating and using directories

After working on your unix system for a while you will accumulate many files. Just like an unorganized desk, this creates a mess in which it is hard to work. The solution is to create directories in which to store related items. A directory is like a file folder which contains related documents (your files). As an example, suppose that when you list your files you see this:
  % ls
  fred1 fred2 fred3 ch1 ch2 ch3 foo.c bar.c
  %
This is really not badly organized: the files fred1, etc. are letters to fred, the files ch1, etc. chapters of a book, and foo.c, bar.c are C programs. Nonetheless, we decide that it is time to get organized, with one directory per project.

The mdkir command.

We create a new directory using the mkdir command ( m ake d irectory).

  % mkdir letters
  % ls
  fred1 fred2 fred3 ch1 ch2 ch3 foo.c bar.c letters

Notice that the directory letters shows up in the listing. If you are not sure what is a file and what is a directory, try this:

  % ls -F
  fred1 fred2 fred3 ch1 ch2 ch3 foo.c bar.c letters/

Notice that letters is displayed somewhat differently.


The mv command

Now we move the letters into the directory letters using the mv command ( m o v e).

  % mv fred1 fred2 fred3 letters
  % ls
  ch1 ch2 ch3 foo.c bar.c letters
If we want to check that letters really contains the files it should, we do this:
  % ls letters
  fred1 fred2 fred3
There is, by the way, a useful shortcut:
  % mv fred* letters

Here the character * matches any sequence of characters, including the null string. Thus files named fred, fred101, and freddy would all be moved into letters.


Paths

You can deal directly with files in a directory like this:
  % cat letters/fred1

This command displays the contents of the file fred1 , which is in the directory letters. Here are some other ways of doing the same thing:

  % cat letters/fred1
  % more letters/fred1
  % emacs letters/fred1
We could even do this:
  % cat l*f*1

Changing directories with cd

Sometimes it is better to work inside the directory letters. To do it we use the cd command ( ch hange d irectory).

  % cd letters
  % ls
  fred1 fred2 fred3

The letters are there, as they chould be. To go back to our home directory we do this:

  % cd
We check that our home directory contains what it should.
  % ls
  ch1 ch2 ch3 foo.c bar.c letters

Now we make directories for the other files and move them into the right places:

  % mkdir book; mv ch* book
  % mkdir cprogs; mv *.c cprogs
  % ls -F
  book/ cprogs/ letters/
  % ls book
  ch1 ch2 ch3
  %

Where are we?

Sometimes in moving from one directory to another we lose track of where we are. To find out what the current directory is, use the pwd command ( p rint w orking d irectory).

  % pwd
  jeremy
  % cd book
  % pwd
  jeremy/book
  %

Removing directories

To remove a directory we first remove all the file in it, then remove the directory with rmdir ( r emove d irectory).

  % pwd
  jeremy
  % cd letters
  % pwd
  jeremy/letters
  % rm *
  % cd ..
  % rmdir letters

The command rm * removes all files in the current directory. The command cd .. changes the current directory to the parent of the current one. In this case, it changes us from jeremy/letters to jeremy . Remember that jeremy/letters is a path , as is jeremy/letters/fred1. The latter is the path which starts with Jeremy's home directory and ends with the file fred1.


Back to tutorial | On to next lesson