RedHat Linux Free Tutorial

Web based School

Red Hat Linux rhl08

Previous Page TOC Next Page



8


The Linux File System


In this chapter, you learn about:

  • Files: what they are, types of files, filenames

  • Directories: what they are, parent directories and subdirectories, directory names, your home directory

  • Absolute and relative file and directory names

  • Moving between directories using the cd command

  • Using the cat command to create a new file

  • Creating directories

  • Moving and copying files

  • Removing files and directories

  • File and directory ownership, using chown and chgrp to change ownership

  • File and directory permissions, using chmod to change permissions

  • Using the gunzip command to uncompress .gz files compressed by gzip

  • The tar command

  • The standard Linux directories and directory structure

To 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.

Common Types of Files


Files can contain various types of information. The following three types will become the most familiar to you:

  • User data: Information that you create and update. The very simplest user data is plain text or numbers. You learn to create these simple files later in this chapter. More complicated user data files might have to be interpreted by another program to make sense. For instance, a spreadsheet file looks like gibberish if you look at it directly. To work with a spreadsheet, you have to start up the spreadsheet program and read in the spreadsheet file.

  • System data: Information, often in plain text form, that is read and used by the Linux system—to keep track of which users are allowed on the system, for instance. As a system administrator, you are responsible for changing system data files. For instance, when you create a new user, you modify the file /etc/passwd, which contains the user information. Ordinary users of the system are usually not concerned with system data files, except for their private startup files.

  • Executable files: These files contain instructions that your computer can perform. This set of instructions is often called a program. When you tell the computer to perform them, you're telling it to execute the instructions given to it. To human eyes, executable files contain meaningless gibberish—obviously your computer doesn't think the way you do! Creating or modifying executable files takes special tools. You learn how to use these programming tools in Part V, "Linux for Programmers."


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 guide and elsewhere.

The Root Directory


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.

How Directories Are Named


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.

The Home Directory


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.



Your home directory does not provide privacy! Normally, any user can go into another's home directory and read (and copy!) the files stored there (although he can't delete or change the files). When Linux creates your home directory, it in effect provides you with an open office cubicle whose desk and filing cabinet drawers are unlocked.
You must lock up everything you want to keep private. (This topic is covered in the section "File Permissions and Ownership.") It is generally considered rude or nosy to poke around in someone else's home directory, just as it's rude or nosy to poke around in someone's office while they're away from their desk, but the world is full of nosy and rude people, so you must take precautions!
Note that anyone logged in as root can read and manipulate all the files on the system, including files that users have locked up. If you can't trust the system administrator (who usually has the root password), don't use the system!


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.

Navigating the Linux File System


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

/home/fido

darkstar:~$

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.

Absolute and Relative Filenames


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.

Going Places The cd Command


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:


darkstar:~$ cd ..

darkstar:/home$ pwd

/home

darkstar:/home$ cd ..

darkstar:/$ pwd

/

darkstar:/$ cd ..

darkstar:/$ 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

darkstar:/usr/bin$ pwd

/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.

There's No Place Like Home


Type cd without any directory specification:


darkstar:/usr/bin$ cd

darkstar:~$ pwd

/home/fido

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.



When you're changing to a distant directory, it's often a good idea to take several steps. If you mistype a very long directory specification, you will have to retype the entire specification. Sometimes it might not even be clear why cd gave you an error! Taking a number of shorter steps means less retyping in case of an error. Consider this example:
darkstar:~$ cd /usr/docs/faq/unix
bash: /usr/docs/faq/unix: No such file or directory
You're pretty sure that this path is correct. Let's change directories one step at a time:
darkstar:~$ cd /usr
darkstar:/usr$ cd docs
bash: docs: No such file or directory
Aha! There's a problem with docs. The directory is actually named doc:
darkstar:/usr$ ls
bin/ doc/ games/ info/ man/ sbin/ spool/
darkstar:/usr$ cd doc
darkstar:/usr/doc$ cd faq/unix
darkstar:/usr/doc/faq/unix$ pwd
/usr/doc/faq/unix



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!



Go through the following sections very carefully. You should be logged in as your "ordinary" username, not as root! Only when you're sure you understand these sections thoroughly should you use these commands while logged in as root.
There is no "unerase" command in Linux! Be sure you know what you're doing!


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.

cat That Useful Feline


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

hello

what

what

asdf

asdf

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.



When you say that a program exits, you mean that it has finished running and that you are back at the Linux command prompt. It might seem odd to talk about the program exiting when, from your point of view as a user, you have exited the program. This turn of phrase goes back to the early days of UNIX, when it was coined by the people who were programming the system. They looked at things from the program's point of view, not the user's!

So how do you use cat to create a file? Simple! You redirect the output from cat to the desired filename:


darkstar:~$ cat > newfile

Hello world

Here's some text

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

Hello world

Here's some text

darkstar:~$

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

Some more lines

^D

darkstar:~$ cat newfile

Hello world

Here's some text

Some more lines

darkstar:~$

To discover what cat actually stands for, let's first create another file.


darkstar:~$ cat > anotherfile

Different text

^D

darkstar:~$

Now, try this:


darkstar:~$ cat newfile anotherfile> thirdfile

darkstar:~$ cat thirdfile

Hello world

Here's some text

Some more lines

Different text

darkstar:~$

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. They are discussed in Chapter 16, "Text Editors."

Creating Directories


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

anotherfile newfile thirdfile

darkstar:~$ mkdir newdir

darkstar:~$ ls

anotherfile newdir/ newfile thirdfile


The mkdir command is already familiar to you if you have used MS-DOS systems. In MS-DOS, you can abbreviate mkdir as md. You might think that md would work in Linux, because, after all, most of the commands we've seen have extremely concise names. However, Linux doesn't recognize md; it insists on the full mkdir.
If you frequently switch between Linux and MS-DOS, you might want to use mkdir for both systems. However, be warned that you might start typing other Linux commands in MS-DOS—for example, typing ls instead of dir!



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>

cp <source> <destination>

As you can see, mv and cp are very simple commands. Here's an example:


darkstar:~$ ls

anotherfile newdir/ newfile thirdfile

darkstar:~$ mv anotherfile movedfile

darkstar:~$ ls

movedfile newdir/ newfile thirdfile

darkstar:~$ cp thirdfile xyz

darkstar:~$ ls

anotherfile newdir/ newfile thirdfile xyz

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


darkstar:~$ mv newfile newdir/newfile

However, it's much more common to type


darkstar:~$ mv newfile newdir

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

darkstar:~newdir$ copy ../newfile .

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:


darkstar:~$ cp newfile newdir/anothername

This would create a copy of newfile in the directory newdir and name the copied file anothername.



When moving or copying files between directories, you should always double-check that the file's destination directory exists and verify the directory's name. Otherwise, the results of your command can be unexpected, as the following two examples show.
If in the example just shown you mistyped newdir—for instance, as mv newfile mewdir—you would wind up with a file called mewdir in your current directory and no file newfile in the newdir subdirectory!
Another way you would get an unexpected result would be to type cp newfile newdir if you didn't realize that the directory newdir existed. In this case, you would be expecting to create an identical file called newdir in your current directory. What you would actually do is create a copy of newfile, called newfile, in the subdirectory newdir.


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!

Moving and Copying with Wildcards


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 *:


darkstar:~$ cp * /tmp

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 guide1, guide_idea, guide-chapter-1, and poem.guide. To copy just the first three files, you could type cp guide* /tmp. When you type guide*, you are asking Linux to match all files whose names start with guide. In this case, poem.guide does not start with guide, so there is no way guide* can match it. (Note that if your filename were guide.poem, guide* would match it.)



As you saw at the outset, mv and cp are very simple commands. It's specifying the files that's the complicated part! If things still seem confusing, don't worry. Even experts sometimes mess up "simple" moves and copies. Follow the examples and try any different ways you think of. There is a definite logic as to how the files to be moved and copied should be specified. It takes a while to become familiar with this logic, and you will have to practice a while before these things become intuitive.


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

darkstar:~$ cd /tmp

darkstar:/tmp$ ls

/newdir

The directory newdir is now a subdirectory of /tmp.



When you move a directory, all its files and subdirectories go with it.


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:


darkstar:~$ rm dead_duck

removes the file dead_duck from your current directory.


darkstar:~$ rm /tmp/dead_duck

removes the file dead_duck from the /tmp directory.


darkstar:~$ rm *

removes all files from your current directory. (Be careful when using wildcards!)


darkstar:~$ rm /tmp/*duck

removes all files ending in duck from the /tmp directory.



As soon as a file is removed, it is gone! Always think about what you're doing before you remove a file. You can use one of the following techniques to keep out of trouble when using wildcards.
  1. Run ls using the same file specification you use with the rm command. For instance:

        
         darkstar:~$ ls *duck
        
         dead_duck guiduck lame-duck
        
         :~$ rm *duck

    In this case, you thought you wanted to remove all files that matched *duck. To verify that this indeed was the case, you listed all the *duck files (wildcards work the same way with all commands). The listing looked okay, so you went ahead and removed the files.
  2. Use the i (interactive) option with rm:

      
       darkstar:~$ rm -i *duck
      
       rm: remove 'dead_duck'? y
      
       rm: remove 'guiduck'? n
      
       rm: remove 'lame-duck'? y
      
       darkstar:~$


When you use rm -i, the command goes through the list of files to be deleted one by one, prompting you for the OK to remove the file. If you type y or Y, rm removes the file. If you type any other character, rm does not remove it. The only disadvantage of using this interactive mode is that it can be very tedious when the list of files to be removed is long.


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


rmdir: <directory>: Directory not empty

This is as close to a safety feature as you will see in Linux!



This one might mystify you:
darkstar:/home$ ls
fido/ root/ zippy/
darkstar:/home$ ls zippy
core kazoo stuff
darkstar:/home$ rm zippy/*
darkstar:/home/zippy$ ls zippy
darkstar:/home$ rmdir zippy
rmdir: zippy: Directory not empty
darkstar:~$
The reason for the Directory not empty message is that files starting with . usually are special system files and are usually hidden from the user. To list files whose names start with ., you have to use ls -a. To delete these files, use rm .*:
darkstar:/home$ ls -a zippy
./ ../ .bashrc .profile
darkstar:/home$ rm zippy/.*
rm: cannot remove '.' or '..'
darkstar:/home$ ls -a zippy
./ ../
darkstar:/home$ rmdir zippy
darkstar:/home$ ls
fido/ root/
darkstar:~$
You will most often come across this situation in a system administrator role.


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.



You should use rm -r only when you really have to. To paraphrase an old saying, "It's only a shortcut until you make a mistake." For instance, if you're logged in as root, the following command removes all files from your hard disk, and then it's "Hello, installation procedure" time (do not type the following command!):
rm -rf /
Believe it or not, people do this all too often. Don't join the club!



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:


-rw-r—r— 1 fido users 163 Dec 7 14:31 myfile

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.

File and Directory Ownership


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

-rw-r—r— 1 fido users 114 Dec 7 14:31 myfile

darkstar:~$ chown root myfile

darkstar:~$ ls -l myfile

-rw-r—r— 1 root users 114 Dec 7 14:31 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.

File Permissions


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:


-rw-r—r— 1 fido users 163 Dec 7 14:31 myfile

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:

read permission 4
write permission 2
execute permission 1

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.



If you're familiar with the binary system, think of rwx as a three-digit binary number. If permission is allowed, the corresponding digit is 1. If permission is denied, the digit is 0. So r-x would be the binary number 101, which is 4+0+1, or 5. —x would be 001, which is 0+0+1, which is 1, and so on.

The following combinations are possible:

      0 or —-: No permissions at all

      4 or r—: read-only

      2 or -w-: write-only (rare)

      1 or —x: execute

      6 or rw-: read and write

      5 or r-x: read and execute

      3 or -wx: write and execute (rare)

      7 or rwx: read, write, and execute



Anyone who has permission to read a file can then copy that file. When a file is copied, the copy is owned by the person doing the copying. He or she can then change ownership and permissions, edit the file, and so on.



Removing write permission from a file doesn't prevent the file from being deleted! It does prevent it from being deleted accidentally, since Linux asks you whether you want to override the file permissions. You have to answer y, or the file will not be deleted.


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

-rw-r—r— 1 fido users 114 Dec 7 14:31 myfile

darkstar:~$ chmod 345 myfile

darkstar:~$ ls -l myfile

—wxr—r-x 1 fido users 114 Dec 7 14:31 myfile

darkstar:~$ chmod 701 myfile

darkstar:~$ ls -l myfile

-rwx——x 1 root users 114 Dec 7 14:31 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


darkstar:~$ chmod go+r myfile

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.



Any user who has write permission in a directory can delete files in that directory, whether or not that user owns or has write privileges to those files.
Most directories, therefore, have permissions set to drwxr-xr-x. This ensures that only the directory's owner can create or delete files in that directory.
It is especially dangerous to give write permission to all users for directories!



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. These and other important system administrator commands are further detailed in Chapter 37, "System Administration Basics."

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.

How to tar Without Feathering


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.



When tar archives to a floppy disk, all the data already on the disk is destroyed. You have to reformat it to use it with MS-DOS again.

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.



Unlike gzip, tar doesn't remove, delete, or rename files it puts into the archive. However, when tar extracts archived files, it overwrites existing files with files of the same name from the archive.


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!

/home


This directory holds users' home directories. In other UNIX systems, this can be the /usr or /u directory.

/bin


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.

/usr


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

docs Various documents, including useful Linux information
man The man pages accessed by typing man <command>
games The fun stuff!

/usr/bin


This directory holds user-oriented Linux programs.

/var/spool


This directory has several subdirectories. mail holds mail files, spool holds files to be printed, and uucp holds files copied between Linux machines.

/dev


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.

/usr/sbin


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.

/sbin


This directory holds system files that are usually run automatically by the Linux system.

/etc


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!).

Summary


You should now feel more comfortable working in Linux. Understanding and being able to navigate the Linux file system is very important, since Linux really does consist simply of some files organized in a fairly standard way.

You still might find yourself stumped by certain file or directory problems. Remember that the online man pages can assist you. Linux gives you a lot of flexibility in creating files, specifying absolute or relative names, and setting permissions. Don't be afraid to experiment (as an ordinary user, in your home directory). There are too many different ways to perform tasks to list or exhaustively describe here. Don't cling to rigid recipes written on a piece of paper. You learn by trying!

You should go on to Chapters 9, "Introduction to the GNU Project Utilities," through 12, "Using tcsh," especially if you want to create programs or macros from system command files or learn more about the built-in user interface features in Linux shells. Chapter 16 has some very useful information about editing text files.

Once you are familiar with shells and have had some practice manipulating files and directories, you can move on to the advanced topics in Part III, "Editing, Typesetting, and More," through Part VIII, "Advanced Programming Topics," of this guide.

Previous Page Page Top TOC Next Page