|
|
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:
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.
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.
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.
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 /home/yana
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 /src/FreeBSD/SMPng/src/sys/i386/conf $ cd $ pwd /home/yana
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:
$ cd pictures $ ls -l Shaleema.jpeg daemon.gif
The ls command lists files in various formats. This one just shows the names.
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.jpegThis 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.
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.
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?
“Hold on”, you should be saying, “you just said that the owner can do anything with his own files”. Well, yes, but that includes protecting himself against accidental damage. The owner can set these permissions to whatever he wants, but if he ensures he can't write them, then he can't accidentally wipe out the contents.
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 ./-l
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”).
$ mkdir testdir $
Note that most of these commands aren't very talkative. They don't say anything unless something goes wrong. If you want to make sure they have worked, you need to use a command like ls.
$ cd testdir
$ mkdir pictures
$ ls myfile ls: myfile: No such file or directory so it doesn't exist $ touch myfile $ ls myfile myfile
touch is really intended to change the modification timestamp we saw above, but it has the side effect that if the file doesn't exist, it will create it.
$ ls myfile myfile $ rm myfile $ ls myfile ls: myfile: No such file or directory $
$ rmdir pictures
What happens to the files in the directory if you remove the directory? Nothing. If the directory contains files, then rmdir will fail. Remember that directory pictures? Let's take a look. First, we'll put a file in pictures:
$ mkdir pictures $ touch pictures/myfile $ rmdir pictures rmdir: pictures: Directory not empty
First you need to remove the files in the directory. But what if the directory contains subdirectories, and they contain files and subdirectories? That could be a lot of work. That's why there's a special option for rm, -r (“recursive”):
$ rm -r pictures $
Be very careful with this command. You can cause untold damage if you remove files you didn't intend to remove.
$ tar xvf /var/tmp/pictures.tar pictures/ pictures/Shaleema.jpeg pictures/daemon.gif pictures/oldstuff/To move a file out of the pictures directory into the current directory, you could do:
$ ls -l pictures 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 $ mv pictures/daemon.gif . $ ls -l pictures total 1 -rw-r--r-- 1 yana home 15042 Sep 3 12:10 Shaleema.jpeg drwxr-xr-x 2 yana home 512 Sep 3 12:22 oldstuff $ ls -l total 1 -rw-r--r-- 1 yana home 0 Sep 3 12:19 -l -rw-r--r-- 1 yana home 2404 Sep 3 12:10 daemon.gif drwxr-xr-x 3 yana home 512 Sep 3 12:49 pictures $Note the use of the symbol . for the current directory.
$ mv daemon.gif funnybloke.gif $ ls -l total 1 -rw-r--r-- 1 yana home 0 Sep 3 12:19 -l -rw-r--r-- 1 yana home 2404 Sep 3 12:10 funnybloke.gif drwxr-xr-x 3 yana home 512 Sep 3 12:49 pictures $
$ cp funnybloke.gif daemon.gif $ ls -l total 1 -rw-r--r-- 1 yana home 0 Sep 3 12:19 -l -rw-r--r-- 1 yana home 2404 Sep 3 12:53 daemon.gif -rw-r--r-- 1 yana home 2404 Sep 3 12:10 funnybloke.gif drwxr-xr-x 3 yana home 512 Sep 3 12:49 pictures $
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?
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:
Don't use " and ' characters; they're used to quote other file names. For example, if you're faced with a Microsoft-centric file name like Silly name with spaces in it, you could try listing it:
You can do the same with the ' character.
\ is another quoting character: it says “don't interpret the next character specially”. Microsoft uses \ instead of / to represent directories, causing untold confusion to people who try to access web pages like http://www.lemis.com/why\backslashes\are\not\slashes.html—users of Microsoft “Internet Explorer” can't even access this page. You can use backslashes instead of quotes if you want, for example for the previous silly file name:
If you have a file name with a \ in it, you need to quote that too:
Note that one of the \ disappears each time.
We've already seen ~: the shell uses it to represent your home directory. It can be confusing in file names: sometimes it will work, sometimes it won't. It's better just to avoid it.
Similarly, some programs use : to represent network components of file names. Like with ~, sometimes it will work, sometimes it won't.
Greg's home page | Greg's diary | Greg's photos | Copyright |