Image of grog
Greg's UNIX shells and files tutorial
Greg's diary
Photo index
Greg's home page
Network link stats
Greg's other links
Copyright information
I originally wrote this text in September 2000 for my daughter Yana. This is a slightly cleaned up version.


After this lesson, you will be able to:

Running a shell

One of the most important programs you run on a UNIX system is the shell. It's what you get when you start a terminal window. The shell is also sometimes called a command interpreter, because you type in commands to it, and it performs them (or emits a rude remark telling you why it hasn't). The shell shows that it's ready for a new command by printing a prompt. Traditionally, the prompt was a single character, such as $ or %, because old terminals were so slow that it was a nuisance to wait for them to print a long prompt. Nowadays you might see a longer prompt, like === yana@echunga (/dev/ttyp8) ~ 9 ->. These prompts give you a lot of information; we'll look at it at the end of the lesson. In the meantime, we'll pretend that the prompt is $, because that's what most books do.

Files and directories

We've talked about files a bit, and we've created and edited them, but what are files really? Basically, they're just sections of disk space which the operating system maintains for you. It's easiest to think of them as documents, though that's not quite accurate.

File and directory names

We've seen that files have names. So do directories: you can't tell from the name whether it represents a file or a directory. UNIX allows names to be up to 255 characters long, and to contain any name except the magic / which represents directory content. Each character has its own meaning. In particular, upper and lower case letters are different, so the names index.html, INDEX.HTML and Index.Html represent three different files. Other operating systems are much more restricted and confused about these matters.

Although UNIX understands just about every character in a file name, there are a number of characters that you should avoid: spaces and the characters " ' ~ : \, and also non-printing characters. We'll look at why below.

UNIX stores files in directories, which is just the name we use for the things where we put the files. Directories have a nice feature: you can put other directories in there, called subdirectories. The subdirectories can have subdirectories in turn, and you can build up quite a structure of directories, called a tree.

Trees have roots, of course, so there's one directory which we call the root directory: its name is simply / -- that's right, just a single slash. The directories in / are called the top-level directories, and usually they're reserved for system use. One top-level directory is important, though: /home is the name of the directory where user files are put.

There's also a directory called /usr, where the system puts its own directories. The reason for this name is lost in the mists of time.

When you log in, the system sets your shell's current working directory to a special directory called your home directory. You can usually refer to this directory with the special name ~. By convention, every user gets a home directory, a subdirectory of /home. The name is usually the name of the user. If your name is, say, Yana, then your home directory will probably be called /home/yana.

Current working directory

OK, so you create a file. Where does it go? It goes into the current working directory. Any program you run has its own idea of a current working directory. You can find out which it is with the shell command pwd, which means print working directory:

$ pwd

This tells you that your current working directory is called /home/yana, so you're probably in your home directory. If not, you can change back to it with the cd (change directory) command without specifying the name. For example, if you're in some funny directory:

$ pwd
$ cd
$ pwd

Referring to files in subdirectories

So if we have a directory called pictures, how do we access the files in it? Let's assume that pictures contains the images Shaleema.jpeg and daemon.gif. There are a number of ways:

  1. You can change the current working directory to point to into the directory:
    $ cd pictures
    $ ls -l
    Shaleema.jpeg   daemon.gif

    The ls command lists files in various formats. This one just shows the names.

  2. You can refer to them by following the name of the subdirectory with a / and the name of the file. This is the reason that you can't use / as part of a file or directory name:

    $ ls -l pictures/Shaleema.jpeg
    -rw-r--r--  1 yana  home  15042 Sep  3 12:10 pictures/Shaleema.jpeg
    This example shows the use of the -l (“long”) option with ls: it gives you much more detail about the files. We'll look at the details below.

Parent directories

Every directory has a name, but it's not always easy to know the name of the parent directory, the directory one level higher up. By convention, you can use the name .. to refer to the parent directory without knowing its real name. You can refer to the parent directory of the parent directory with ../.., etc.

File attributes

We've seen above that the ls -l command gives a lot of information about the files. What does it all mean? Let's take another look. We're in the directory pictures now, but we've added a directory oldstuff:

total 1
-rw-r--r--  1 yana  home  15042 Sep  3 12:10 Shaleema.jpeg
-rw-r--r--  1 yana  home   2404 Sep  3 12:10 daemon.gif
drwxr-xr-x  2 yana  home    512 Sep  3 12:22 oldstuff

Obviously the end of the lines is the name of the file. What's the rest?

Referring to files in the current directory

Well, we know how to refer to files in the current directory: we just refer to them by name. Sometimes, though, that doesn't work. What if we have a file called -l? If we say ls -l, it won't do what we want:

$ ls -l
-rw-r--r--  1 yana  home    0 Sep  3 12:19 -l
drwxr-xr-x  2 yana  home  512 Sep  3 12:11 pictures

For a few such obscure reasons, there is an alternative: a single . represents the current directory. Since it's a directory, we need a / to follow it when referring to the files, so we might end up saying:

$ ls ./-l

Exercise: a few tools for manipulating files and directories

So, what would you like to do with files and directories? Here are a few basic commands. Try them out as you go along. To do that, you must first start an xterm (“terminal window”).

So what are those funny characters in the prompt?

So far we've been pretending that the prompt character is $, but in fact it's more like === yana@echunga (/dev/ttyp8) ~/public_html 13 -> . What does that mean?

Characters in file names

We've seen that UNIX can handle any character in a file name except for /. I've also said that you should avoid a number of others. Here's why:

Greg's home page Greg's diary Greg's photos Copyright

Valid XHTML 1.0!

$Id: files.php,v 1.4 2009/01/26 04:44:40 grog Exp $