Files

A file can be thought of as something which contains data. This "data" can be the source of a program you're writing, the text of a document like this one, or any other collection of characters (such as directory information or executable code). A file always has a name associated with it which can be up to 255 characters long. Files are usually created with text editors.

A directory is a file containing information about other files and directories. A directory which is found "within" another directory is called a "subdirectory" and can contain even more subdirectories. A Unix file system is like a tree with a root (the "/" directory) and many branches (subdirectories).

When an account is created for you, you are assigned a "home directory". This is where you will find yourself every time you log in. If you were to type "pwd" (print working directory) after you logged in, you would see something like this:

/users/admin/jsmith which is the name of your "working directory" and the full "pathname" to your files. In this example, directory names are separated by "/"s. The pathname describes where your files are in relation to the total file system. When you create files, they will be "under" your home directory and their pathnames will look like this: /users/admin/jsmith/mklogin.c /users/admin/jsmith/junk The "ls" (for list) command lists the names (not contents) of all the files in the current directory. (more on directories later)

The names are sorted into alphabetical order automatically, but other variations are possible. For example, the command "ls -t" causes the files to be listed in the order in which they were last changed, most recent first.

The "-l" option (that's the letter l, not the number 1) gives a long listing. "ls -l" will produce something like:

total 2 -rw-r--r-- 1 jsmith 205 Jan 30 11:24 myfile -rw-r--r-- 1 jsmith 76217 Jan 31 11:23 temp The date and time are of the last change to the file. The number before the date indicates the number of characters. The owner of the files, that is the person who created them, is jsmith. The -rw-r--r-- tells who has permission to read and write the file; in this case, the owner can read and write the files, but everyone can read them.

Options can be combined: "ls -lt" gives the same thing as "ls -l", but sorted into time order (i.e., the most recently modified files are listed first). You can also name the files you're interested in, and "ls" will list the information about only them.

The use of optional arguments that begin with a minus sign, like "-t" and "-lt", is a common convention for Unix programs. In general, if a program accepts such optional arguments, they precede any filename arguments. It is also vital that you separate the various arguments with spaces: "ls-l" is not the same as "ls -l".

The "ls" command doesn't normally list files whose names begin with a dot (or period), such as '.login' and '.cshrc'. If you want these files listed, include the "-a" option, as in "ls -a".

So far we have used filenames without ever saying what's a legal name, so it's time for a couple rules. First, filenames are limited to 255 characters, which is enough to be descriptive. Second, although you can use almost any character in a filename, common sense says you should stick to ones that are visible, and that you should probably avoid characters that might be used with other meanings. We have already seen, for example, that in the "ls" command, "ls -t" means to list in time order. So if you had a file whose name was "-t", you would have a tough time listing it by name. Besides the minus sign, there are other characters which have special meaning. To avoid pitfalls, you would do well to use only letters, numbers and the period until you're familiar with the situation. Note that there is a difference between upper- and lower-case letters in filenames. In this document, all filenames will be in lower-case.

There are special files, usually in your home directory, whose names start with a period (.). These files are not listed when you use the "ls" command unless you use the "-a" option. Filenames which begin with a comma (,) are removed automatically every night. Files beginning and ending with a sharp sign (#) are created by the GNU Emacs editor as temporary files and are deleted after three days.

You can find further information under:

Click here to go to the next section.

Or click here to go to the top of the chapter.