Files and Directories


In Unix, all data are stored in repositories called files. For example, the RESOLVE/C++ programs that you write in this course will be stored as Unix files. You could also use files to store any reports that you write or to save the e-mail that you receive.

Like in the "real" world, it is not a good idea to have lots of files "lying around" in a disorganized manner. Unix allows you to organize your files into directories. A "directory" is a location where files are kept in a list. For instance, you could create a directory to store all your files for the first lab and call it Lab1. You could create another directory called Lab2 to store your files for the second lab. If you are already familiar with either Apple Macintosh computers or Windows File Manager, just think of Unix directories as being the same as folders.

When you login to a Unix system, the system puts you in your home directory. Your home directory is the directory that is assigned to you to store all your files. Unix has commands that you can use to create and delete files and directories within your home directory. Unix also gives you commands to change from your home directory to other directories. The directory that you're in at any given point of time is called your current working directory.

The ls command

The Unix command ls, lists the contents of the current working directory. The contents of a directory are the files and sub-directories inside that directory.
 
  • In the xterm, enter the command: ls 

  • Since your working directory at this point is your home directory, the output of the ls command tells you the contents of your home directory.

  • In the xterm, enter the command: Ls 

  • You'll get an error message of the form: 

    Ls: Command not found.

    Important Note: Unix is case-sensitive. That means it treats ls and Ls as two distinct commands. In fact, there is no command Ls in Unix. Similarly, if you have a file named foo, you cannot access it using the name FOO.

    The mkdir command

    You can use the mkdir command to create new directories. In the home directory, let us create a directory called Testdir1.
     
  • Enter the command: mkdir Testdir1 

  • If you repeat the ls command, you'll see that Testdir1 appears among the contents listed. Let us use the cd command to change our working directory to Testdir1

    The cd command

    You can move around from one directory to another with the cd command.
     
  • Enter the command: cd Testdir1 

  • At this point, you're not in your home directory anymore but in the sub-directory Testdir1. What command do you think you should choose if you wanted to create a sub-directory Testdir2 within this directory?

  • Go ahead and enter the command to create a new sub-directory Testdir2 in your xterm. Use the ls command to verify that the directory was created. 

  • Now Testdir2 is a sub-directory of Testdir1, which is a sub-directory of your home directory. 

    Naming the current and parent directories

    To review, we have seen how we can change to a particular directory by using the cd command followed by the name of the directory. Unix uses the special name .. (pronounced "dot dot") to refer to the parent directory of a particular directory. Note that each directory can have only one parent directory. The special name . ("dot") is used to refer to the current working directory. You can use these special names with the Unix commands that work with directories.
     
  • Enter the command: cd .. 

  • This puts you in the parent directory of the current working directory. Now you should be back in your home directory. 

    Naming other directories

    Earlier we used the ls command to list the contents of the current working directory. You can use ls to look at the contents of a directory other than the one you're currently in, by specifying the directory. Let us list the contents of the sub-directory Testdir1.
     
  • Enter the command: ls Testdir1 

  • Actually, you could use ls to list the contents of the directory Testdir2 (in the sub-directory Testdir1), while you're still in your home directory. At this point, Testdir2 should be empty.

  • Enter the command: ls Testdir1/Testdir2 

  • In this case, you have specified the directory to be listed using what is called a relative pathname. A relative pathname specifies a route to get to the desired file or directory starting from the current working directory. In other words, a relative pathname also contains the name of each intermediate directory that you need to pass through to get to the desired destination. The names are separated by the "/" character. All components of a relative pathname except the last, must be directory names. The last component can be either a file name or a directory name.



     
    Previous Table of Contents Continue


    Last modified: Tue Jan 6 20:27:59 EST 1998