Commands covered in this section: ls cd pwd cat more

The Filesystem

UNIX employs a directory hierarchy, as illustrated in the picture below. The top of the hierarchy is known as the root directory and is specified as /. In the example shown the root directory contains three directories, which are sub-directories of the root. The home directory is the parent of the two sub-directories foo and bar.

directory structure

An absolute filename is the full path of a file or directory, from the root of the hierarchy, given using the / symbol (called a slash - specifically a forward slash) as the directory separator. So the absolute filename of the bar directory is /home/bar. A relative filename is one without an initial / which refers to a directory in relation to the current directory.

What's Inside?

The most frequently used command in UNIX is ls. It has many options, but on its own it simply shows the contents of the current directory, which may be directories or files. Try it now: simply type ls at your command prompt. If you specify an argument to ls it will take this to be a directory and will attempt to list the contents of that directory. Try entering ls / which will list the files and directories in the root directory. There will be some of the ones shown in the diagram, and many others.

The command pwd tells you which directory you are in. If you haven't changed directory since logging in, you should get something like /home/fido, with your usercode in place of fido. This directory is your home directory, and is the part of the filesystem reserved for you. In here you can create the files and directories required for whatever tasks you are engaged in.

Moving Around

You are always in a particular directory, referred to simply as the current directory. To change the current directory, the command cd is used. It can take as an argument a directory, and will then move to this directory. So cd / will move you to the root directory - try using pwd and ls to verify this. Typing cd on its own will return you straight to your home directory, from wherever you are.

There are two important conventions employed when referring to UNIX directories. The first is a single dot: ".". This means "the current directory". The second is two dots: "..", which means "the previous directory up in the hierarchy", or "the parent directory of the current one". Use cd and pwd to experiment with these. Start in your home directory, and work up the hierarchy by using cd .. repeatedly. What happens if you attempt to go above the root directory? Try and it see. You won't get an error message, you just won't go anywhere.

What is it?

The -l option to ls will give you a long listing of the contents of the directory. It includes such things as the size of files, the date and time they are last modified, and whether the item is a file or a directory - the first character of the output shows this: d for a directory and - for a regular file.

Experiment with the ls and cd commands until you find a regular file that is not too large. The contents of this file can then be examined in two ways. Firstly, the cat command will simply output the entire file to the screen. It is called simply by typing cat followed by the name of the file you wish to view. If the contents are more than a screenful, they go off the top of your window, so you miss some. If this is the case, the second command is best used. This is known as more, and is called in the same way as cat, but pauses after each screenful has been displayed. Simply press the spacebar to move on to the next page. Alternatively, you can press q to quit the command, and stop the rest of the file being displayed.

< Previous: Logging In & Out ^ Next: Making A Difference >

Matt Chapman
Last modified: Sun Aug 10 15:15:18 BST 1997