Book Cover

Introduction to Scientific Programming
Computational Problem Solving Using:
Maple and C
Mathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

Unix Files and Directories Tutorial

At this point in the course, you have created lots of files, primarily Maple worksheets. Some of them you have created yourself as homework assignments, and others you have copied and used as parts of lab assignments. You may have created other kinds of files as well, perhaps with the Emacs text editor.

In this tutorial we will study the Unix file system and discuss how to manipulate files and navigate directories. This will come in handy as you begin writing, compiling, and running C programs.

The Unix File System

We are now going to look at basic Unix commands for manipulating files and directories. In Unix, a file can be one of three types: a text file (such as a letter or a C program), an executable file (such as a compiled C program), or a directory (a file ``containing'' other files).

When you consider that there are thousands of users of the local workstation network, you will realize that the computers must keep track of tens or hundreds of thousands of files. Unix uses directories to organize these files, much like a filing cabinet uses drawers and folders to keep track of documents.

The Unix file system is organized around a single structure of directories, where each directory can contain more directories (often called subdirectories) and/or files. The entire file system, often spanning many machines and disks, can be visualized as a tree. Picture this tree as growing upside down, with the root at the top and the leaves toward the bottom. The leaves are all text and executable files, while the root, trunk, limbs, branches, and twigs are all directories.

The file system is called the directory tree, and the directory at the base of the tree is called the root directory. Every file and directory in the file system has a unique name, called its pathname. The pathname of the root directory is /.

As a Unix user, you are given control over one directory. This directory is called your home directory, and it was created when your account was established. This directory is your personal domain, over which you have complete control. You are free to create your own subtree of files and directories within your home directory. To determine the pathname of your home directory, enter the following command into a Unix shell window.

cd; pwd

Everyone has a different home directory, but two things are certain. The pathname of your home directory will start with a slash (everything is rooted in the root directory) and it will end with your user name. For example, suppose that a user jones has a home directory /home/cs/class/jones. From this, we can tell that the root directory / contains a subdirectory called home, which contains a subdirectory called cs, which contains a subdirectory called class, which contains a subdirectory called jones. Every directory has a pathname that shows the sequence of directories that lead from it back to the root.

Working Directory

At any given time when interacting with Unix, you are ``working in'' or ``connected to'' some directory. This is called your working directory. When a Unix Shell window running Unix is first created, you will be connected to your home directory. You will typically change your working directory (with the cd command, as discussed later) several times during a single session.

There is a command that prints the current working directory:

pwd

(You should try this out in a Unix Shell window, as you should all of the commands that we introduce.) Notice that part of the name of the current directory (the part following the last slash) appears as part of the command line prompt. For example, if your working directory is /home/cs/class/jones, your prompt might look like

7 cadesm35:jones%

Examining Directories

What files and directories are contained in the working directory? You can find out with the

ls

command, which lists the contents of the working directory. When you enter this command, you will see a list of all of the Maple worksheets and other files that you have copied or created in your home directory. Notice that only the names of the files are displayed, not their full pathnames. But if you know the name of the working directory, and you know the name of a file within it, you can easily figure out that file's full pathname. What would be the full pathname of a file named ``file'' in your home directory?

Click here for answer

Moving Around the Directory Tree

To this point we have never moved away from your home directory. Let's learn how to navigate the directory tree. Before we do this, let's add to your home directory so that we will have some files to experiment with. Type the following command into a Unix Shell window.

mkdir testdir

(This command will create a directory called testdir in your home directory. Use the ls command to verify that it really is there.)

The command cd takes a directory as an argument and makes that directory your working directory. There are two ways to specify the name of a directory or file. One way is to give the full pathname, and the other way is to give enough of the pathname to let Unix know how to get to the desired directory from the working directory. We'll look at these two methods in turn.

Absolute Pathnames

You can give the full pathname of the desired directory or file. For example, if jones wanted to go to her home directory, she could use the command

cd /home/cs/class/jones

Or, if she wanted to connect to the testdir directory within her home directory, she could issue the command

cd /home/cs/class/jones/testdir

Use cd now to connect to your testdir directory. Remember--if you've forgotten the pathname of your home directory, you can find it out with the pwd command.

Look at the prompt to verify that you have succeeded in connecting to the testdir directory. And use the ls command to see what is in the testdir directory. What do you find?

Click here for the answer

As you experiment with moving around the directory tree, be sure and get used to looking at the prompt to verify that things are working as you expect. If you get completely confused, you can use pwd to find out exactly where you are.

Typing a full pathname can be a real pain, especially when it is a long one. Fortunately, there are several convenient abbreviations. Unix will treat a tilde followed immediately by a user name as an abbreviation for the full pathname of that user's home directory. For example, if you wanted to connect to a user jones' home directory, you could do so with

cd ~jones

Use this form of abbreviation (with your user name, of course) right now to reconnect to your home directory.

This abbreviated form can be quite useful. Can you figure out how to use it to reconnect to your testdir directory?

Click here for the answer

If it is your home directory in which you're interested, and not someone else's, there's a second abbreviation. A tilde all by itself stands for your home directory. So, you can connect back to your home directory with

cd ~

and to your testdir subdirectory with

cd ~/testdir

Finally, here's the ultimate shortcut. If issue the

cd

command with no argument, you will connect to your home directory.

Relative Pathnames

Be sure that you are connected to your home directory. You should know how to do that without any help.

You can also specify a directory or file by describing to Unix how to get to the desired directory or file from the working directory. For example, suppose that you want to connect to your testdir directory from your home directory. You can do this by simply issuing the command

cd testdir

Unix knows that the pathname argument to cd is a relative pathname because it does not begin with a slash or a tilde, as all absolute pathnames do. When Unix encounters a relative pathname, it glues the relative pathname onto the end of the full pathname of the working directory to obtain an absolute pathname.

You should now be connected to your testdir subdirectory. You can connect back to your home directory by issuing the command

cd ..

When ``..'' appears in a pathname, it refers to the parent of the current directory. So the net result of issuing the cd command above is to move one step closer to the root of the tree. You should now be connected to your home directory.

Using Absolute and Relative Pathnames

You might be wondering when you should use absolute pathnames and when you should use relative pathnames. It is entirely a question of convenience. If you need to name a directory that is ``close to'' your working directory, then relative pathnames are quite convenient. This will usually be the case, since you'll do most of your work in or near your home directory.

On the other hand, if you need to name a directory that is ``far away from'' your working directory, then you should use an absolute pathname.

Creating Files and Directories

Connect to your home directory.

You can create a new (empty) directory using the mkdir command:

mkdir newdir

You can verify that the directory has actually been created by listing the contents of your home directory.

When you need to create a file, you will generally do it by using Emacs. Suppose that you'd like to create a file called newfile.txt in your newdir directory. You should select the ``Open File...'' option from the ``File'' menu.

Emacs will then prompt you for the name of a file to read. What do you make of the prompt that Emacs gives you?

Click here for the answer

You need to supply the rest of the pathname, in this case newdir/newfile.txt, and then type the Enter key. You can then use Emacs to create the text and, finally, save your edits with the ``Save Buffer'' option from the ``File'' menu.

Deleting Files and Directories

By now you know how to create and examine files and directories. It is almost as important to know how to get rid of unwanted files and directories.

To delete a file we use the rm command. (It helps to know that ``rm'' stands for ``remove''.) Connect to your newdir directory, which should contain a file newfile.txt. Verify this by listing the directory.

To delete newfile.txt, issue the command

rm newfile.txt

Depending upon how your defaults are set up, Unix may ask you to confirm that you really mean to delete the file. Just enter a ``y'' or a ``yes'' to confirm.

Now connect back to your home directory.

The command for deleting an empty directory is rmdir. For example, your testdir directory should be empty. You can delete it with

rmdir testdir

Copying Files

Often you will want to copy a file from one place to another. For example, an instructor in a class might place a file into a central location and ask everyone in the class to make a private copy. Or you might decide to make a backup copy of some file before modifying it.

To copy a file we use the cp command. For example, perhaps you have a file called solution1.mws or something similar in your home directory. You can copy it into a file called sol1-backup.mws by issuing the command

cp solution1.mws sol1-backup.mws

Either argument to cp can be an absolute or relative pathname. For example, to copy solution1.mws to a file called sol1-backup.mws in the newdir directory, issue the command

cp solution1.mws newdir/sol1-backup.mws

You should now use ls to verify that both copies were made.

File and Directory Summary

Here is a summary of the commands that we covered in this section.

SUMMARY OF UNIX FILE SYSTEM


Directory abbreviations

  .                  Current directory

  ..                 Parent of current directory

  ~<user>            Home directory of user <user>

  ~                  Your home directory


Exploring the file system

  pwd                Print name of working directory

  cd <pathname>      Connect to directory <pathname> (. by default)

  ls <pathname>      List contents of directory <pathname> (. by default)


Manipulating directories and files

  mkdir <pathname>          Create a directory <pathname>

  rm <pathname>             Delete file <pathname>

  cp <pathname> <pathname>  Copy one file into another

  rmdir <pathname>          Delete empty directory <pathname>