MA10126

Essential Unix

 

Introduction | Logging in | Directories | Files | Text Editors | Miscellany | File access | Links

 

3. Directories

 

Just as in Windows, you will keep your documents as files, which are located within a hierarchical structure of folders. In Unix, these folders are called directories. Actually, Unix treats a directory as a special sort of file, containing links to other files which are its contents. In this page, we show how to create directories and subdirectories within your filespace, and to move from one directory to another.

 

When you start a Unix session, you will be in the topmost directory of your personal workspace. This is identical to your H: drive on the network in Windows, so any files and folders you have created in H: using Windows will also appear in Unix. But this is really just one subdirectory of the directory of maths undergraduates, which is a subdirectory of the directory of all maths users, and so on, back to the root directory. You can find the absolute path of your current working directory at any time by issuing the command

pwd

which stands for “print working directory”. Do it now (and press Return); you should see a line such as

/u/ma/p/abc123

 

Here, the first “/” indicates the root directory, and the subsequent subdirectories are separated by “/” characters. The subdirectory abc123 is the workspace of the user with that username. The line above is the absolute pathname of this directory, but it can also be abbreviated to ~abc123

 

To create a new subdirectory within your current working directory, use the command:

mkdir dirname

where dirname is the name of the new directory. To change into that directory (i.e. for dirname  to become the current working directory) use the command:

cd dirname

 

Directories can be referred to by their absolute pathname (starting from the root directory /) or their pathname relative to the current working directory. You can use either absolute or relative pathnames with the cd command.

Within a directory, the name “.” refers to that directory, and the name “..” refers to the parent directory (one level up). Thus, the command

cd ..

will take you to the up one level from the current directory. The command

cd

without any argument, takes you back to your home directory. Thus, if your username is abc123, the three commands

cd /u/ma/p/abc123

cd ~abc123

cd

will each have the same effect.

 

To list the files and directories within a subdirectory called dirname, use the command

ls dirname

Typing the command

ls

without any argument, will list the files and subdirectories in the current directory. If you do this now, it may be that nothing is printed out, because you don’t yet have any files.

 

To create a file, we can use a powerful feature of Unix which allows output from a command to be redirected into a file rather than onto the screen. For example, the command

who

outputs to the screen a list of all the users currently logged in (Try it now!). But typing

who > userfile

will send the output from the who command to a new file called userfile, in the current directory.

 

To move a file named file1 from the current directory into a subdirectory named subdir, and give it the name file2, use the command

mv file1 subdir/file2

If you don’t want to change its name, just type

mv file1 subdir

 

The mv command can also be used to move directories (along with all the files they contain), and to rename files. For example, to rename file peepel as people (keeping it in the same directory), type

mv peepel people

 

Practise these commands by performing the following exercise:

 

Starting in your home directory, create a subdirectory called calendar.

Change into the calendar directory, and create a subdirectory called week0.

Create a file called today, which contains today's date, in the calendar directory. You can do this by using the command date, which outputs the current date and time, and the redirection symbol >.

List the files in the current directory, to see that the file today exists there.

Move this file into the week0 subdirectory.

List the files in the current directory again – has today gone?

List the files in the week0 subdirectory – is today there?

Change into the week0 subdirectory, and create a file users0 containing the list of users currently logged in.

Check where you are by printing the current working directory.

Change back to your home directory. Can you list, from here, the files in the directory week0 (a subdirectory of calendar)?

Change up one level to the parent directory of your home directory. List all the files and directories there. What are all these directories, do you think?

 

 

To see the solution to this exercise, click here.

 

An important point to note is that Unix is case-sensitive: it recognises capital letters as different from lowercase ones. So if you create a directory called Bath and then issue the command

cd bath

you will get an error message because the directory bath does not exist. To avoid this, it is good practice to use lowercase letters throughout in the names of files and directories.

 

Play around with the mkdir, cd, mv and ls commands until you are happy you understand how they work, then go on to the next page which tells you more about manipulating files. Click on “Files” in the linkbar below…

 

 

Introduction | Logging in | Directories | Files | Text Editors | Miscellany | File access | Links