Red Hat Linux: Linux File SystemThe Linux File SystemTo understand how Linux works, and to use the system beyond a superficial
level, you must be familiar with the Linux notion of files and the file system
into which they are organized. Files An Overview The most basic concept of a file, and one you may already be familiar with
from other computer systems, defines a file as a distinct chunk of information
that is found on your hard drive. Distinct means that there can be many
different files, each with its own particular contents. To keep files from
getting confused with each other, every file must have a unique identity. In
Linux, you identify each file by its name and location. In each location or
directory, there can be only one file by a particular name. So, for instance, if
you create a file called novel, and you get a second great idea, you will either
have to call it something different, such as novel2, or put it in a different
place, to keep from overwriting the contents already in your original novel. Files can contain various types of information. The following three types
will become the most familiar to you:
Filenames Linux allows filenames to be up to 256 characters long. These characters can
be lower- and uppercase letters, numbers, and other characters, usually the dash
(-), the underscore (_), and the dot (.). They can't include reserved metacharacters such as the asterisk, question
mark, backslash, and space, because these all have meaning to the shell. We met
some metacharacters when we discussed wildcards in the previous chapter. Other
metacharacters will be introduced in the Linux shell chapters. Directories An Overview Linux, like many other computer systems, organizes files in directories. You
can think of directories as file folders and their contents as the files.
However, there is one absolutely crucial difference between the Linux file
system and an office filing system. In the office, file folders usually don't
contain other file folders. In Linux, file folders can contain other file
folders. In fact, there is no Linux "filing cabinet"—just a huge file folder
that holds some files and other folders. These folders contain files and
possibly other folders in turn, and so on. Parent Directories and Subdirectories Imagine a scenario in which you have a directory, A, that contains another
directory, B. Directory B is then a subdirectory of directory A, and directory A
is the parent directory of directory B. You will see these terms often, both in
this book and elsewhere. In Linux, the directory that holds all the other directories is called the
root directory. This is the ultimate parent directory; every other directory is
some level of subdirectory. From the root directory, the whole structure of directory upon directory
springs and grows like some electronic elm. This is called a tree structure
because, from the single root directory, directories and subdirectories branch
off like tree limbs. Directories are named just like files, and they can contain upper- and
lowercase letters, numbers, and characters such as -, ., and _. The slash (/) character is used to show files or directories within other
directories. For instance, usr/bin means that bin is found in the usr directory.
Note that you can't tell, from this example, whether bin is a file or a
directory, although you know that usr must be a directory because it holds
another item—namely, bin. When you see usr/bin/grep, you know that both usr and
bin must be directories, but again, you can't be sure about grep. The ls program
shows directories with a following /—for example, fido/. This notation implies
that you could have, for instance, fido/file; therefore, fido must be a
directory. The root directory is shown simply by the symbol / rather than mentioned by
name. It's very easy to tell when / is used to separate directories and when
it's used to signify the root directory. If / has no name before it, it stands
for the root directory. For example, /usr means that the usr subdirectory is
found in the root directory, and /usr/bin means that bin is found in the usr
directory and that usr is a subdirectory of the root directory. Remember, by
definition the root directory can't be a subdirectory. Linux provides each user with his or her own directory, called the home
directory. Within this home directory, users can store their own files and
create subdirectories. Users generally have complete control over what's found
in their home directories. Because there are usually no Linux system files or
files belonging to other users in your home directory, you can create, name,
move, and delete files and directories as you see fit.
The location of a user's home directory is specified by Linux and can't be
changed by the user. This is both to keep things tidy and to preserve system
security. Fortunately, navigating the Linux file system is simple. There are only two
commands to be learned, and one of them has absolutely no options or parameters!
The pwd Command Where Am I? Type pwd at the Linux command prompt. You see darkstar:~$ pwd This tells you that you're currently in the directory /home/fido. (If you are
logged in under a different user name, you will see that name in place of fido.)
This is your home directory. When you log in, Linux always places you in your
home directory. The letters "pwd" stand for "print working directory." Again, a command's
name or function has been cut down to a few easy-to-type characters. (You will
often see the term current directory used in place of working directory.) You might be wondering what "working directory" or "being in a directory"
really means. It simply means that Linux commands, by default, perform their
actions in your working directory. For instance, when you run ls, you are shown
only the files in your working directory. If you want to create or remove files,
they will be created or removed in your working directory. If you specify only the name of a file, Linux looks for that file in your
working directory. For example, more myfile lets you read the contents of the
file myfile. But myfile must be in your current working directory, or the more
command won't find it. Sometimes you want to specify a file that isn't in your current directory.
You would then specify the name of the directory the file is in, as well as the
name of the file itself. If, for instance, your current directory has a subdirectory called novel,
which contains a file called chapter_1, you could type more novel/chapter_1,
which tells more that it should look in the subdirectory novel for the file
chapter_1. This is called a relative filename. You are specifying the location
of chapter_1 relative to where you are now, in the subdirectory novel, which is
found in your current directory. If you changed your working directory, the
relative filename would no longer work. Two special directory specifications are "." and "..".The specification "."
always stands for the directory you are currently in, and ".." stands for the
parent directory of your current directory. (You see how "." and ".." are used
later in this chapter.) Any filename that includes "." or ".." is, by
definition, a relative filename. A filename that is valid from any location is called an absolute filename.
Absolute filenames always begin with /, signifying root. So if you specify a
filename as /home/fido/novel/chapter_1, there is no doubt as to where the file
is located. Every file on your system has a unique absolute filename. Someone else on the system might also have a directory called novel in his or
her home directory. Perhaps it even contains a file called chapter_1. In this
case, you can't distinguish the two files by using the relative filename
novel/chapter_1. However, the absolute filenames will be different—for instance,
/home/fido/novel/chapter_1 as opposed to /home/mary/novel/chapter_1. The novel
subdirectory in /home/fido is not the same directory as the novel directory in
/home/mary! The two are in quite separate locations, and only coincidentally do
they share the same name. The cd (change directory) command lets you change your working directory. You
can think of it as moving to another directory. The syntax of the cd command is cd <directory specification> There must be a space between cd and the directory specification. The directory specification can be an absolute or relative one. For instance,
type cd .. followed by pwd: There is no parent directory for the root directory, so typing cd .. when in
the root directory simply leaves you in the root directory. Note that the Linux command prompt shows you which directory you are
currently in, so you don't have to type pwd all the time. (I will continue to
use pwd for clarity.) You can also use absolute directory names. darkstar:/$ cd /usr/bin When you type an absolute directory name, you go to that directory, no matter
where you started from. When you type cd .., where you end up depends on where
you started. To see the effect of changing your working directory, type ls. The list of
files is so long that the first part scrolls off your screen. The ls command
shows you the contents of your current directory (as always), but now your
current directory is /usr/bin, which contains many more files than your home
directory. Type cd without any directory specification: darkstar:/usr/bin$ cd Typing cd by itself always returns you to your home directory. When exploring
the file system, you sometimes wind up deep in a blind alley of subdirectories.
Type cd to quickly return home, or type cd / to return to the root directory.
The ~ in your prompt is another special character. It stands for your home
directory. There's no reason to type cd ~ when cd works just as well, and is
much easier to type! However, try this: When you type cd ~<user>, you move to that user's home directory. This is a
very useful trick, especially on large systems with many users and more
complicated directory structures than the simple /home/<user> on your Linux
system.
Creating and Deleting Files Linux has many ways to create and delete files. In fact, some of the ways are
so easy to perform that you have to be careful not to accidentally overwrite or
erase files!
Return to your home directory by typing cd. Make sure you're in your
/home/<user> directory by running pwd. In the previous chapter, you created a file by typing ls -l /bin > test.
Remember, the > symbol means "redirect all output to the following filename."
Note that the file test didn't exist before you typed this command. When you
redirect to a file, Linux automatically creates the file if it doesn't already
exist. What if you want to type text into a file, rather than some command's output?
The quick and dirty way is to use the command cat. The cat command is one of the simplest, yet most useful, commands in Linux.
It certainly does more than any living feline! The cat command basically takes all its input and outputs it. By default, cat
takes its input from the keyboard and outputs it to the screen. Type cat at the
command line: darkstar:~$ cat The cursor moves down to the next line, but nothing else seems to happen. Now
cat is waiting for some input: hello Everything you type is repeated on-screen as soon as you press Enter! How do you get out of this? At the start of a line, type ^D (Ctrl-D). (In
other words, hold down the Ctrl key and press D.) If you're not at the beginning
of a line, you have to type ^D twice. ^D is the Linux "end of file" character.
When a program such as cat encounters a ^D, it assumes that it has finished with
the current file, and it goes on to the next one. In this case, if you type ^D
by itself on an empty line, there is no next file to go on to, and cat exits.
So how do you use cat to create a file? Simple! You redirect the output from
cat to the desired filename: darkstar:~$ cat > newfile You can type as much as you want. When you are finished, press ^D by itself
on a line; you will be back at the Linux prompt. Now you want to look at the contents of newfile. You could use the more or
less commands, but instead, let's use cat. Yes, you can use cat to look at files
simply by providing it with a filename: darkstar:~$ cat newfile Neat! You can also add to the end of the file by using >>. Whenever you use
>>, whether with cat or any other command, the output is always appended to the
specified file. (Note that the ^D character does not appear on-screen. I show it
in the examples for clarity.) darkstar:~$ cat >> newfile To discover what cat actually stands for, let's first create another file.
darkstar:~$ cat > anotherfile Now, try this: darkstar:~$ cat newfile anotherfile> thirdfile cat stands for concatenate; cat takes all the specified inputs and
regurgitates them in a single lump. This by itself would not be very
interesting, but combine it with the forms of input and output redirection
available in Linux and you have a powerful and useful tool. Sometimes you want to change just one line of a file, or perhaps you are
creating a large and complicated file. For this you should use one of the
editing programs available in Linux. To create a new directory, use the mkdir command. The syntax is mkdir <name>,
where <name> is replaced by whatever you want the directory to be called. This
creates a subdirectory with the specified name in your current directory: darkstar:~$ ls
Moving and Copying Files You often need to move or copy files. The mv command moves files, and the cp
command copies files. The syntax for the two commands is similar: mv <source> <destination> As you can see, mv and cp are very simple commands. Here's an example: darkstar:~$ ls You can use cat (or more or less) at any time to verify that anotherfile
became movedfile, and that the contents of file xyz are identical to the
contents of thirdfile. It can get more confusing if you're moving or copying files from one
directory to another. This is because a file's real name includes its absolute
path—for instance, /home/fido/newfile. However, Linux lets you leave off parts
of the file's name, because it's more convenient to refer to newfile rather than
/home/fido/newfile. For instance, suppose you want to move newfile into the newdir subdirectory.
If you want the file to keep the same name, you type However, it's much more common to type Here, because you have typed a directory name for the destination, Linux
assumes that you want the file to be placed in the specified directory. You could also use cd to change to the directory you want to move the file
to: darkstar:~$ cd newdir This example is a bit less intuitive than the first two! You specify that the
source is ../newfile, which means "the file newfile in the current directory's
parent directory." The destination you simply specify as ".", which is short for
"the current directory." In other words, you're telling mv to "go up one level,
grab newfile, and move it to right here." Because this is less intuitive, you
might find yourself automatically pushing a file from your current directory to
another directory rather than pulling a file from another directory into your
current directory. You can also change the name of the file while moving or copying it to
another directory. The following is just one possible way: This would create a copy of newfile in the directory newdir and name the
copied file anothername.
The mv command is much more efficient than the cp command. When you use mv,
the file's contents are not moved at all; rather, Linux makes a note that the
file is to be found elsewhere within the file system's structure of directories.
When you use cp, you are actually making a second physical copy of your file
and placing it on your disk. This can be slower (although for small files, you
won't notice any difference), and it causes a bit more wear and tear on your
computer. Don't make copies of files when all you really want to do is move
them! If you have 20 files in a directory, and you want to copy them to another
directory, it would be very tedious to use the cp command on each one.
Fortunately, you can use the wildcards * and ? to copy more than one file at a
time. If you want to move or copy all files in a directory, use the wildcard *: This command copies every file in your current directory to the directory /tmp.
You can use *, along with other characters, to match only certain files. For
instance, suppose you have a directory that contains the files book1, book_idea,
book-chapter-1, and poem.book. To copy just the first three files, you could
type cp book* /tmp. When you type book*, you are asking Linux to match all files
whose names start with book. In this case, poem.book does not start with book,
so there is no way book* can match it. (Note that if your filename were
book.poem, book* would match it.)
Moving Directories To move a directory, use the mv command. The syntax is mv <directory>
<destination>. In the following example, you would move the newdir subdirectory
found in your current directory to the /tmp directory: darkstar:~$ mv newdir /tmp The directory newdir is now a subdirectory of /tmp.
Removing Files and Directories Now that you know how to create files and directories, it's time to learn how
to undo your handiwork. To remove (or delete) a file, use the rm command (rm is a very terse spelling
of remove). The syntax is rm <filename>. For instance: removes the file dead_duck from your current directory. removes the file dead_duck from the /tmp directory. removes all files from your current directory. (Be careful when using
wildcards!) removes all files ending in duck from the /tmp directory.
darkstar:~$ rm -i *duck
Removing Directories The command normally used to remove (delete) directories is rmdir. The syntax
is rmdir <directory>. Before you can remove a directory, it must be empty (the directory can't hold
any files or subdirectories). Otherwise, you see This is as close to a safety feature as you will see in Linux!
Sometimes you want to remove a directory with many layers of subdirectories.
Emptying and then deleting all the subdirectories one by one would be very
tedious. Linux offers a way to remove a directory and all the files and
subdirectories it contains in one easy step. This is the r (recursive) option of
the rm command. The syntax is rm -r <directory>. The directory and all its
contents are removed.
File Permissions and Ownership All Linux files and directories have ownership and permissions. You can
change permissions, and sometimes ownership, to provide greater or lesser access
to your files and directories. File permissions also determine whether a file
can be executed as a command. If you type ls -l or dir, you see entries that look like this: The -rw-r—r— represents the permissions for the file myfile. The file's
ownership includes fido as the owner and users as the group. When you create a file, you are that file's owner. Being the file's owner
gives you the privilege of changing the file's permissions or ownership. Of
course, once you change the ownership to another user, you can't change the
ownership or permissions anymore! File owners are set up by the system during installation. Linux system files
are owned by IDs such as root, uucp, and bin. Do not change the ownership of
these files. Use the chown (change ownership) command to change ownership of a file. The
syntax is chown <owner> <filename>. In the following example, you change the
ownership of the file myfile to root: darkstar:~$ ls -l myfile To make any further changes to the file myfile, or to chown it back to fido,
you must use su or log in as root. Files (and users) also belong to groups. Groups are a convenient way of
providing access to files for more than one user but not to every user on the
system. For instance, users working on a special project could all belong to the
group project. Files used by the whole group would also belong to the group
project, giving those users special access. Groups normally are used in larger
installations. You may never need to worry about groups. The chgrp command is used to change the group the file belongs to. It works
just like chown. Linux lets you specify read, write, and execute permissions for each of the
following: the owner, the group, and "others" (everyone else). read permission enables you to look at the file. In the case of a directory,
it lets you list the directory's contents using ls. write permission enables you to modify (or delete!) the file. In the case of
a directory, you must have write permission in order to create, move, or delete
files in that directory. execute permission enables you to execute the file by typing its name. With
directories, execute permission enables you to cd into them. For a concrete example, let's look at myfile again: The first character of the permissions is -, which indicates that it's an
ordinary file. If this were a directory, the first character would be d. There
are also some other, more exotic classes. These are beyond the scope of this
chapter. The next nine characters are broken into three groups of three, giving
permissions for owner, group, and other. Each triplet gives read, write, and
execute permissions, always in that order. Permission to read is signified by an
r in the first position, permission to write is shown by a w in the second
position, and permission to execute is shown by an x in the third position. If
the particular permission is absent, its space is filled by -. In the case of myfile, the owner has rw-, which means read and write
permissions. This file can't be executed by typing myfile at the Linux prompt.
The group permissions are r—, which means that members of the group "users"
(by default, all ordinary users on the system) can read the file but not change
it or execute it. Likewise, the permissions for all others are r—: read-only. File permissions are often given as a three-digit number—for instance, 751.
It's important to understand how the numbering system works, because these
numbers are used to change a file's permissions. Also, error messages that
involve permissions use these numbers. The first digit codes permissions for the owner, the second digit codes
permissions for the group, and the third digit codes permissions for other
(everyone else). The individual digits are encoded by summing up all the "allowed" permissions
for that particular user as follows:
Therefore, a file permission of 751 means that the owner has read, write, and
execute permission (4+2+1=7), the group has read and execute permission (4+1=5),
and others have execute permission (1). If you play with the numbers, you quickly see that the permission digits can
range between 0 and 7, and that for each digit in that range there's only one
possible combination of read, write, and execute permissions.
The following combinations are possible: 0 or —-: No permissions at all
Changing File Permissions To change file permissions, use the chmod (change [file] mode) command. The
syntax is chmod <specification> file. There are two ways to write the permission specification. One is by using the
numeric coding system for permissions: darkstar:~$ ls -l myfile This method has the advantage of specifying the permissions in an absolute,
rather than relative, fashion. Also, it's easier to tell someone "Change
permissions on the file to seven-five-five" than to say "Change permissions on
the file to read-write-execute, read-execute, read-execute." You can also use letter codes to change the existing permissions. To specify
which of the permissions to change, type u (user), g (group), o (other), or a
(all). This is followed by a + to add permissions or a - to remove them. This in
turn is followed by the permissions to be added or removed. For example, to add
execute permissions for the group and others, you would type Other ways of using the symbolic file permissions are described in the chmod
man page. Changing Directory Permissions You change directory permissions with chmod, exactly the same way as with
files. Remember that if a directory doesn't have execute permissions, you can't
cd to it.
Miscellaneous File Commands There are many Linux commands to manipulate files, directories, and the
entire file system. Many of these commands are used only by system
administrators. You will touch on a few that are also used by ordinary users.
Fear of Compression The Zipless File Most Linux files are stored on the installation CD-ROM in compressed form.
This allows more information to be stored. When you installed Linux, the installation program uncompressed many of the
files it transferred to your hard drive. However, if you look, you will be able
to find compressed files! Any file ending in .gz—for example, squashed.gz—is a compressed file. To
uncompress this particular type of file, type gunzip <file>. For this example,
you would type gunzip squashed.gz. The gunzip program creates an uncompressed
file and removes the .gz extension. Therefore, you would wind up with a normal
file called squashed. To compress a file, use the gzip command. Typing gzip squashed would compress
squashed and rename it squashed.gz. Another type of compressed file you might see ends with the extension .zip.
Use unzip to uncompress these files. To create files of this type, use zip. In almost any location with several Linux or UNIX systems, sooner or later
you will hear someone say, "Put that in a tar file and send it over." They are referring to the output created by the tar program. Although tar
stands for tape archive, it can copy files to floppy disk or to any filename you
specify in the Linux file system. The tar command is used because it can archive
files and directories into a single file and then recreate the files and even
the directory structures later. It's also the easiest way to place Linux files
on a floppy disk. To create a tar file, you typically type tar cvf <destination>
<files/directories>, where files/directories specifies the files and directories
to be archived, and destination is where you want the tar file to be created. If
you want the destination to be a floppy disk, you usually type /dev/fd0 as the
destination. This specifies your primary floppy drive (A: in MS-DOS). You can
use a floppy disk that's been formatted under MS-DOS.
To extract a tar file, you typically type tar xvf <tar file>. For instance,
to pull files from a floppy disk, you would type tar xvf /dev/fd0.
Important Directories in the Linux File System Most of the directories that hold Linux system files are "standard." Other
UNIX systems will have identical directories with similar contents. This section
summarizes some of the more important directories on your Linux system. This is the root directory. It holds the actual Linux program, as well as
subdirectories. Do not clutter this directory with your files! This directory holds users' home directories. In other UNIX systems, this can
be the /usr or /u directory. This directory holds many of the basic Linux programs. bin stands for
binaries, files that are executable and that hold text only computers could
understand. This directory holds many other user-oriented directories. Some of the most
important are described in the following sections. Other directories found in /usr
include
/usr/bin This directory holds user-oriented Linux programs. This directory has several subdirectories. mail holds mail files, spool holds
files to be printed, and uucp holds files copied between Linux machines. Linux treats everything as a file! The /dev directory holds devices. These
are special files that serve as gateways to physical computer components. For
instance, if you copy to /dev/fd0, you're actually sending data to the system's
floppy disk. Your terminal is one of the /dev/tty files. Partitions on the hard
drive are of the form /dev/hd0. Even the system's memory is a device! A famous device is /dev/null. This is sometimes called the bit bucket. All
information sent to /dev/null vanishes—it's thrown into the trash. This directory holds system administration files. If you do an ls -l, you see
that you must be the owner, root, to run these commands. This directory holds system files that are usually run automatically by the
Linux system. This directory and its subdirectories hold many of the Linux configuration files. These files are usually text, and they can be edited to change the system's configuration (if you know what you're doing!). |