FILES AND DIRECTORIES UNDER LINUX

File structure and specifications

Like most computer systems, MathLAN workstations store information of all sorts -- data, text, programs -- in named files. Since each of our machines is a multi-user system, it keeps track of who owns each file and what other users are allowed to have access to it. The general rule is that the owner of a file is the person who created it, and only the owner of a file (and, theoretically, the system managers) have access to it; but the owner of a file can decide to share it with other users of the system if he likes.

A collection of related files is called a directory. (Under some other operating systems, the usual term is `folder'.) When you first activate your account, you already own a directory, which is called your home directory. Initially, any files that you create are placed in your home directory; but you are also allowed to create new directories and to place files into them.

MathLAN files make up a hierarchical file system. At the top of the hierarchy is a directory called / (which is pronounced ``root''). The / directory has one or two files in it, but it also contains other directories, with names like bin, home, usr, and so on. Each of these other directories has files of its own, and may also contain further directories; for example, usr contains directories called lib, man, share, and many others. This process continues indefinitely: lib contains a directory gtk, which contains a directory themes, which contains a directory engines. However, you do always reach bottom eventually; the engines directory, for instance, contains only files. The directories that are contained in another directory are called its subdirectories. Every directory except / is a subdirectory of some other directory.

Every file and every directory in the structure can be uniquely identified by specifying the series of directories that one would go through to get down to it from /. For example, a file called libpixmap.so in the engines directory mentioned above is more fully identified as

/usr/lib/gtk/themes/engines/libpixmap.so

Note that each of the names of the intermediate directories is followed by a slash. For this reason, one can't use a slash as part of the name of a file or directory; in fact, it's best to stick just with letters, numbers, hyphens, and the period, though Unix does accept some other characters.

Your home directory is a subdirectory of /home, and its name is the same as your user name. So, for example, the home directory of the MathLAN standard imaginary user George Spelvin, whose user name is spelvin, is /home/spelvin.

The working directory

Fortunately, in many cases, you don't have to remember and use the full specification of a file that you want to work with. If you refer to a file with a name that doesn't begin with a slash, the shell (the program that interprets your commands) will assume that you are talking about a file that is in your working directory. When you log in, your working directory is the same as your home directory, but this is just a conventional starting point. You can move around to work in any directory whose owner has chosen to give you access. The command that you give in order to change your working directory is cd (change directory). For instance, if you want to move your working directory to /net/matlab5, the command would be

bourbaki$ cd /net/matlab5

After this, a reference to a file called installguide.ps would be understood to refer, not to a file in your home directory, but to /net/matlab5/installguide.ps.

The command

bourbaki$ cd

with no directory specified, moves your working directory back to your home directory. The command

bourbaki$ pwd

prints out the full specification of your working directory.

Abbreviating file names

There are also some convenient ways of specifying files and directories without changing the working directory. One is that if your working directory has subdirectories of its own, you can work down through those subdirectories just as if you were starting from root, provided that your specification doesn't start with a slash. For instance, if your working directory is /net/matlab5, you can specify the file /net/matlab5/etc/license.dat as simply etc/license.dat: the license.dat file in the etc subdirectory of the working directory.

In addition, the symbol . always refers to the working directory, .. to its ``parent'' (that is, to the directory of which the working directory is a subdirectory), and ~ to your home directory. It's also possible to refer to the home directories of another user by writing that person's user name after a ~, so that, for example, ~spelvin is George Spelvin's home directory. (Note, however, that none of the files in another user's home directory will be accessible to you unless the other user has chosen to share them.)

File management commands

The Linux operating system provides many useful commands for managing files and groups of files. Most user files are created with the help of a text editor, such as Emacs, which is described in other documents. Once created, files can be manipulated with the following commands:

To get a list of all the files and subdirectories of a directory, you use the ls command. For example,

bourbaki$ ls /home

displays a list of the contents of the /home directory, and

bourbaki$ ls ~

lists the contents of your home directory. If, instead of specifying a directory, you just type

bourbaki$ ls

you get a list of the contents of the working directory.

A variant of the ls command, ls -l, can be used if you want a more complete description of each file in the directory. A typical line of output from ls -l looks like this:

-rw-------   1 spelvin  users      17395 Aug 25 22:44 file-system.tex

This is the entry for a file called file-system.tex, 17395 bytes in length, last modified at 10:44 p.m. on August 25 of the current year, owned by spelvin, a member of the group ``users'' (which is almost everyone on MathLAN), readable and writable by owner but by no one else (that's what rw------- means), and accessible in only one place in the file system (that's what the 1 means).

If a directory is owned by someone else, the ls command will not work on it unless the owner has granted access to all users.

To display a file on screen, you usually use the more command:

bourbaki$ more frogs.txt

displays a file in the working directory called frogs.txt (assuming that there is such a file). If the file to be displayed won't all fit on the screen at once, the more program pauses when the screen is full. The reader can press the space bar for the next screenful, or opt out by pressing Ctrl/C.

To copy the file to the screen without page breaks, the basic command is cat:

bourbaki$ cat frogs.txt

The cat program is used when one just wants to push the file onto the screen without reading it. For instance, when preparing the ``official run'' of a programming assignment, a student might first start the utility that makes a transcript of a terminal session, then use cat to list the text of his program -- not so that she can read it, but just to allow the transcription utility to record it.

To display just the first or the last few lines of a file, use the head and tail commands, which work like cat. The command

bourbaki$ head frogs.txt

outputs the first ten lines of frogs.txt to the screen, and

bourbaki$ tail -15 frogs.txt

the last fifteen. (A number can be used with either command; ten lines is the default size of the chunk that gets printed.)

To get a hard (paper) copy of a file, you use the a2ps command:

bourbaki$ lpr frogs.txt

This transmits a copy of frogs.txt to one of the laser printers in the MathLAN lab.

To remove a file from a directory that you own, you use the rm command:

bourbaki$ rm frogs.txt

This command asks the system to get rid of the frogs.txt file. Warning: This process is not reversible. Once a file has been removed, it's gone forever. For this reason, you may prefer to form the habit of using the -i option, which checks before it actually removes the file:

frogs.p: ? (y/n)

To confirm that you want the file removed, type y and press Enter. If you want to keep the file, type n rather than y.

Files are also removed from your directory in another way: Whenever you save a file that you are editing, using the XEmacs text editor, the previous version of that file (if there was one) is moved to a backup file, the name of which is the same as the name of the file being edited, with the addition of a tilde (~) at the end. The previous backup file, if there was one, is removed completely.

To rename a file, you use the mv command, providing both the file's current name and the new name you want it to have. For example,

bourbaki$ mv frogs.txt toads.txt

changes the name of the frogs.txt file to toads.txt. You can also use this command to move files from one directory to another, provided that you own both directories. For example, if you are in your home directory and have created a subdirectory called amphibians, the command

bourbaki$ mv frogs.txt amphibians/frogs.txt

moves the frogs.txt file from your home directory into the subdirectory.

You can also give a directory as the second name in a mv command, which will cause the file to be moved into the specified directory, retaining its name. Thus, in the most recent example, the command

bourbaki$ mv frogs.txt amphibians

would have exactly the same effect.

You could have a problem with the mv command if you try to change the name of a file to something that is already the name of a different file. It's not possible for two different files to have the same name, so clearly something has to give. In this situation, mv without options simply deletes the file that previously had the contested name in order to make room for the one you move in. mv -i will ask for permission to remove the previously existing file and to replace it with the one whose name you're trying to change:

remove toads.txt? (y/n)

If you type y and press Enter, the previously existing file is removed and the file being renamed takes its place. If you type n and press Enter, both files will remain under their original names.

To make an exact duplicate of a file under a new name, you use the cp command:

bourbaki$ cp frogs.txt toads.txt

This command creates a new file, toads.txt, with the same contents as frogs.txt. The new file can be edited independently, moved to a different directory, and so on. Very often the cp command is used to duplicate a file that is owned by someone else. For instance, if George Spelvin has given us access to his home directory and to the frogs.txt file contained in it, we can write

bourbaki$ cp ~spelvin/frogs.txt frogs.txt

to obtain a copy of the file. The original belongs to George, but the copy belongs to us, so we can edit, modify, or remove it without affecting the original.

Like mv, cp allows you to specify a directory as the target of the operation, and it creates the copy within that directory. So, in the preceding example, we could also write

bourbaki$ cp ~spelvin/frogs.txt .

to get the same effect.

Again, there is the danger of inadvertently copying a file onto a previously existing file, so you may prefer to use cp -i, which asks for confirmation before proceeding:

overwrite toads.txt? (y/n)

Again, y Enter means that the previously existing toads.txt will be removed, while n Enter cancels the cp operation. In most MathLAN accounts, copy has been defined to be a synonym for cp -i.

Wildcards

Most of the preceding commands can be applied to several similarly named files at once with the help of ``wildcard'' file specifications. These are similar to ordinary file specifications, except that part of the filename is replaced by an asterisk (*). The asterisk is treated as a wildcard character that matches any sequence of characters. So, for example, the command

bourbaki$ rm frogs*

removes every file in the working directory directory whose name begins with frogs -- frogs.txt, frogs.p, frogspawn, frogs itself, and so on. Similarly,

bourbaki$ cp ~spelvin/*.txt

copies every file in Spelvin's home directory that has a name ending in .txt into the user's own home directory.

In some cases, wildcard specifications are used to restrict the application of a command. For instance,

bourbaki$ ls *.txt

displays a list of the names of just those files in the working directory that end in .txt, rather than all the files in the directory.

Directory management commands

A user is free to set up whatever directories he likes as subdirectories of his home directory, and it will make your life a lot easier if you set up subdirectories once you have more than about twenty or thirty files to keep track of. You should consider setting up a subdirectory when you have a group of files, all related to the same project or goal, that could plausibly be kept separate from the main body of your account.

To create a new directory, you use the mkdir command, naming the directory you want to create. If your working directory is your home directory, you can create a subdirectory called amphibians as follows:

bourbaki$ mkdir amphibians

A directory created by mkdir starts out empty, so at this point the command

bourbaki$ ls amphibians

produces no output.

A directory that you make yourself shows up in its ``parent'' directory -- the one within which it is created -- as one of the entries in the ls listing for that directory.

To remove a directory that is no longer wanted, the best approach is first to remove all the files in the unwanted directory, then use the special command rmdir to remove the directory itself. For example,

bourbaki$ rmdir ~/amphibians

removes the amphibians subdirectory of the user's home directory, but only if it contains no files. You can remove all the files from a directory that you own by giving the command

bourbaki$ rm -i ~/amphibians/*

(specifying the directory you want to get rid of in place of ~/amphibians) and replying y to each of the prompts that rm issues.

Controlling access to files

Normally, when you create a file, the Linux system will not allow others to use it. However, you can use the chmod command to give others access to your file, or to retract access rights previously granted:

To allow everyone to read a file, you give the command

bourbaki$ chmod a+r frogs.txt

using the name of the file you want to share.

To deny everyone (except yourself) the right to read a file, you give the command

bourbaki$ chmod go-r frogs.txt

If the file in question is an executable program or shell script, you can also selectively control the right to run it:

To allow everyone to execute a program, you give the command

bourbaki$ chmod a+x frogs

using the name of the file containing the program you want to share.

To deny everyone (except yourself) the right to execute a file, you give the command

bourbaki$ chmod go-x frogs.txt

In order to secure access to the file that you have made available in this way, another user must also be able to access all the directories from the root downward to the file you have specified. The root directory and such common directories as /home and /usr/local are set up to permit such access, but you will also have to make your home directory and any subdirectories that must be traversed accessible. The command to do this is

bourbaki$ chmod a+rx ~

for your home directory, and

bourbaki$ chmod a+rx ~/amphibians

for a typical subdirectory.

(laurel leaf logo)

created August 8, 1995
last revised June 25, 2001

John David Stone (stone@cs.grinnell.edu)