Working with the Linux File System


The Linux file system is the structure in which all the information on your computer is stored. Files are organized within a hierarchy of directories. Each directory can contain files, as well as other directories.

If you were to map out the files and directories in Linux, it would look like an upside-down tree. At the top is the root directory, which is represented by a single slash ( / ). Below that is a set of common directories in the Linux system, such as bin, dev, home, lib , and tmp , to name a few. Each of those directories, as well as directories added to the root, can contain subdirectories.

Figure 4-1 illustrates how the Linux file system is organized as a hierarchy. To illustrate how directories are connected, Figure 4-1 shows a /home directory that contains subdirectories for three users: chris, mary , and tom . Within the chris directory are subdirectories: briefs, memos , and personal . To refer to a file called inventory in the chris/memos directory, you could type the full path of /home/chris/memos/inventory . If your current directory were /home/chris/memos , you could refer to the file as simply inventory .

image from book
Figure 4-1: The Linux file system is organized as a hierarchy of directories.

Some of the Linux directories that may interest you include the following:

  • /bin - Contains common Linux user commands, such as ls, sort , date , and chmod .

  • /boot - Has the bootable Linux kernel and boot loader configuration files (GRUB).

  • /dev - Contains files representing access points to devices on your systems. These include terminal devices ( tty* ), floppy disks ( fd* ), hard disks ( hd* or sc* ), RAM ( ram* ), and CD-ROM ( cd* ). (Applications normally access these devices directly through the device files, but end users rarely access them directly.)

  • /etc - Contains administrative configuration files.

  • /home - Contains directories assigned to each user with a login account.

  • /media - Provides a location for mounting devices, such as remote file systems and removable media (with directory names of cdrom , floppy , and so on). In Fedora and RHEL, many removable media are mounted automatically in this directory when the media is inserted (CD or DVD) or connected (USB pen drives or cameras ).

  • /proc - Provides a mechanism for the kernel to send information to processes.

  • /root - Represents the root user's home directory.

  • /sbin - Contains administrative commands and daemon processes.

  • /sys - A /proc -like file system, added with the Linux 2.6 kernel and intended to contain files for getting hardware status and reflecting the system's device tree as it is seen by the kernel. It pulls many of its functions from /proc .

  • /tmp - Contains temporary files used by applications.

  • /usr - Contains user documentation, games , graphical files (X11), libraries (lib), and a variety of other user and administrative commands and files.

  • /var - Contains directories of data used by various applications. In particular, this is where you would place files that you share as an FTP server ( /var/ftp ) or a Web server ( /var/www ). It also contains all system log files ( /var/log ). In time, FTP, HTTP, and similar services will move to the /srv directory to adhere to the Linux Standards Base ( www.freestandards.org/spec ).

The file systems in the DOS or Microsoft Windows operating systems differ from the Linux file structure. See the sidebar on the Linux file system versus Windows-based file systems.

Creating Files and Directories

As a Fedora or RHEL user, most of the files you save and work with will probably be in your home directory. Here are commands you use to create and use files and directories:

  • cd - Change to another current working directory

  • pwd - Print the name of the current working directory

  • mkdir - Create a directory

  • chmod - Change the permission on a file or directory

  • ls - List the contents of a directory

The following procedure steps you through creating directories within your home directory, moving among your directories, and setting appropriate file permissions:

  1. First, go to your home directory. To do this, simply type cd . (For other ways of referring to your home directory, see the "Identifying Directories" sidebar.)

  2. To make sure that you got to your home directory, type pwd . When I do this, I get the following response (yours will reflect your home directory):

     $  pwd  /home/chris 
  3. Create a new directory called test in your home directory, as follows :

     $  mkdir test  
  4. Check the permissions of the directory by typing:

     $  ls -ld test  drwxr-xr-x 2 chris sales 1024 Jan 24 12:17 test 

    Notice that this listing says that test is a directory ( d ), the owner is chris , the group is sales , and the file was most recently modified on Jan 24 at 12:17 p.m. Suppose that you want to prevent everyone else who uses this computer from using or viewing the files in this directory. The permissions for the directory are rwxr-xr-x . I explain what these permissions mean later in this section.

Note 

When you add a new user in Fedora or RHEL, by default, the user is assigned to a group of the same name. For example, in the preceding text, the user chris would be assigned to the group chris . This approach to assigning groups is referred to as the user private group scheme. For more information on user private groups, refer to Chapter 11.

  1. For now, type the following:

     $  chmod 700 test  

    This step changes the permissions of the directory to give you complete access and everyone else no access at all. (The new permissions should read like rwx------ .)

  2. Next , make the test directory your current directory as follows:

     $  cd test  

Using Metacharacters and Operators

To make more efficient use of your shell, the bash shell lets you use certain special characters , referred to as metacharacters and operators. Metacharacters can help you match one or more files without typing each filename completely. Operators let you direct information from one command or file to another command or file.

Using File-Matching Metacharacters

To save you some keystrokes and to be able to refer easily to a group of files, the bash shell lets you use metacharacters. Anytime you need to refer to a file or directory, such as to list it, open it, or remove it, you can use metacharacters to match the files you want. Here are some useful metacharacters for matching filenames:

  • * - This matches any number of characters.

  • ? - This matches any one character.

  • [...] - This matches any one of the characters between the brackets, which can include a dash-separated range of letters or numbers .

To try out some of these file-matching metacharacters, go to an empty directory (such as the test directory described in the previous section) and create some files. Here's an example of how to create some empty files:

 $  touch apple banana grape grapefruit watermelon  

The next few commands show you how to use shell metacharacters to match filenames so they can be used as arguments to the ls command. Using the metacharacters shown below, you can match the filenames you just created with the touch command. Type the following commands and see if you get the same responses:

 $  ls a*  apple $  ls g*  grape grapefruit $  ls g*t  grapefruit $  ls *e*  apple grape grapefruit watermelon $  ls *n*  banana watermelon 

The first example matches any file that begins with an a ( apple ). The next example matches any files that begin with g ( grape, grapefruit ). Next, files beginning with g and ending in t are matched ( grapefruit ). Next, any file that contains an e in the name is matched ( apple, grape, grapefruit, watermelon ). Finally, any file that contains an n is matched ( banana, watermelon ).

Here are a few examples of pattern matching with the question mark ( ? ):

 $  ls ????e  apple grape $  ls g???e*  grape grapefruit 

The first example matches any five-character file that ends in e ( apple, grape ). The second matches any file that begins with g and has e as its fifth character ( grape, grapefruit ).

Here are a few examples of using brackets to do pattern matching:

 $  ls [abw]*  apple banana watermelon $  ls [agw]*[ne]  apple grape watermelon 

In the first example, any file beginning with a, b , or w is matched. In the second, any file that begins with a, g , or w and also ends with either n or e is matched. You can also include ranges within brackets. For example:

 $ ls [a-g]*  apple banana grape grapefruit  

Here, any filenames beginning with a letter from a through g is matched.

Using File-Redirection Metacharacters

Commands receive data from standard input and send it to standard output. Standard input is normally user input from the keyboard, and standard output is normally displayed on the screen. Using pipes (described earlier), you can direct standard output from one command to the standard input of another. With files, you can use less than ( < ) and greater than ( > ) signs to direct data to and from files. Here are the file redirection characters:

  • < - Direct the contents of a file as input to the command.

  • > - Direct the output of a command to a file, overwriting any existing file.

  • >> - Direct the output of a command to a file, adding the output to the end of the existing file.

Here are some examples of command lines where information is directed to and from files:

 $  mail root < ~/.bashrc  $  man chmod  col -b > /tmp/chmod  $  echo "Finished project on $(date)" >> ~/projects  

In the first example, the contents of the .bashrc file in the home directory are sent in a mail message to the computer's root user. The second command line formats the chmod man page (using the man command), removes extra back spaces ( col -b ) and sends the output to the file /tmp/chmod (erasing the previous /tmp/chmod file, if it exists). The final command results in the following text being added to the user's project file:

 Finished project on Wed Oct 25 13:46:49 PST 2006 

You could also pipe the output of the previous command to another command. For example, the following command line would send the line just shown in a mail message to the user boss@example.com:

 $  echo "Finished project on $(date)"mail -s 'Done' boss@example.com  

Understanding File Permissions

After you've worked with Linux for a while, you are almost sure to get a Permission denied message. Permissions associated with files and directories in Linux were designed to keep users from accessing other users' private files and to protect important system files.

The nine bits assigned to each file for permissions define the access that you and others have to your file. Permission bits appear as rwxrwxrwx . The first three bits apply to the owner's permission, the next three apply to the group assigned to the file, and the last three apply to all others. The r stands for read, the w stands for write, and the x stands for execute permissions.

If a dash appears instead of the letter, it means that permission is turned off for that associated read, write, or execute.

You can see the permission for any file or directory by typing the ls -ld command. The named file or directory appears as those shown in the following example:

 $  ls -ld ch3 test  -rw-rw-r-- 1 chris sales 4983 Jan 18 22:13 ch3 drwxr-xr-x 2 chris sales 1024 Jan 24 13:47 test 

The first line shows a file ( ch3 ) that has read and write permission for the owner and the group. All other users have read permission, which means they can view the file but cannot change its contents (although a user may be allowed to remove the file, since the ability to remove a file is based on directory permissions). The second line shows a directory (indicated by the letter d before the permission bits). The owner has read, write, and execute permission, while the group and other users have only read and execute permissions. As a result, only the owner can add, change, or delete files in that directory. Any other user, however, can only read the contents, change to that directory, and list the contents of the directory. (Note that by using the -d option, the test directory entry is listed without listing its contents.)

If you own a file, you can change the permission on it as you please . You can do this with the chmod command. For each of the three sets of permission on a file (read, write, and execute), r is assigned to the number 4, w to 2, and x to 1. So to make permissions wide open for yourself as owner, you would set the first number to 7 (4 plus 2 plus 1). The same would be true for group and other permission. Any combination of permissions can result from 0 (no permission) through 7 (full permission).

Here are some examples of how to change permission on a file and what the resulting permission would be:

 chmod 777 files rwxrwxrwx chmod 755 files rwxr-xr-x chmod 644 files rw-r--r- chmod 000 files --------- 

You can also turn file permissions on and off using plus (+) and minus (-) signs, respectively. This can be done for the owner user (u), owner group (g), others (o), and all users (a). For example, each time starting with a file that has all permissions open ( rwxrwxrwx ), here are some chmod examples with resulting permissions after using a minus sign:

 chmod a-w files r-xr-xr-x chmod o-x files rwsrwsrw- chmod go-rwx files rwx------ 

Likewise, here are some examples, starting with all permissions closed ( --------- ) where the plus sign is used with chmod to turn permissions on:

 chmod u+rw files rw------- chmod a+x files --x--x--x chmod ug+rx files r-xr-x--- 

When you try to create a file, by default it is given the permission rw-r--r-- . A directory is given the permission rwxr-xr-x . These default values are determined by the value of umask . Type umask to see what your umask value is. For example:

 $  umask  022 

The umask value represents the permissions that are not given on a new file. It masks the permissions value of 666 for a file and 777 for a directory. The umask value of 022 results in permission for a directory of 755 ( rwxr-xr-x ). That same umask results in a file permission of 644 ( rw-r--r-- ). (Execute permissions are off by default for regular files.)

Tip 

Here's a great tip for changing the permission for lots of files at once. Using the -R options of chmod , you could change the permission for all of the files and directories within a directory structure at once. For example, if you wanted to open permissions completely to all files and directories in the /tmp/test directory, you could type the following:

 $  chmod -R 777 /tmp/test  

This command line runs chmod recursively ( -R ) for the /tmp/test directory, as well as any files or directories that exist below that point in the file system (for example, /tmp/test/hat,/tmp/test/hat/caps , and so on). All would be set to 777 (full read/write/execute permissions).

Caution 

The -R option of chmod works best if you are opening permissions completely or adding execute permission (as well as the appropriate read/write permission). The reason is that if you turn off execute permission recursively, you close off your ability to change to any directory in that structure. For example, chmod -R 644 /tmp/test turns off execute permission for the /tmp/test directory, then fails to change any files or directories below that point.

Moving, Copying, and Deleting Files

Commands for moving, copying, and deleting files are fairly straightforward. To change the location of a file, use the mv command. To copy a file from one location to another, use the cp command. To remove a file, use the rm command. Here are some examples:

 $  mv abc def  $  mv abc ~  $  cp abc def  $  cp abc ~  $  rm abc   $ rm *  

Of the two move ( mv ) commands, the first moves the file abc to the file def in the same directory ( essentially renaming it), whereas the second moves the file abc to your home directory ( ~ ). The first copy command ( cp ) copies abc to the file def , whereas the second copies abc to your home directory ( ~ ). The first remove command ( rm ) deletes the abc file; the second removes all the files in the current directory (except those that start with a dot).

Note 

For the root user, the mv, cp , and rm commands are aliased to each be run with the -i option. This causes a prompt to appear asking you to confirm each move, copy, and removal, one file at a time. This is done to prevent the root user from messing up a large group of files by mistake. To temporarily get around an alias, type the full path to the command (for example, /bin/rm -rf /tmp/junk/* ).




Fedora 6 and Red Hat Enterprise Linux Bible
Fedora 6 and Red Hat Enterprise Linux Bible
ISBN: 047008278X
EAN: 2147483647
Year: 2007
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net