The File System

The Linux file system can be thought of as a single abstract "tree." When you add files on a removable device—such as a floppy drive or CD-ROM—or a remote system, these subsidiary file systems are "grafted" onto the original file system at a mount point.

Mount points are usually empty directories.

A mount point—and, indeed, the entire abstract tree—has no relationship with physical reality. This behavior is different than in Microsoft Windows, where the files and directories on drive E are all on the logical or physical drive E.

Actually, the term file system has two meanings:

  • The entire Linux abstract file system

  • A subsection, or "branch," of the file system tree: in other words, a directory, possibly with subdirectories

Generally, it is apparent from the context which usage is meant.

There's no requirement that a Linux file system be organized in a particular way, but over time a kind of organizational convention has grown up. The root file system includes the root directory, the Linux kernel, /dev for device files, /etc for critical system files, and /bin for important utilities. Other standard directories and their contents are listed in Table 11.3.

It's also important to understand that every Linux file has a mode. The mode of a file controls who can read, write, and execute the contents of a file; it is also called the file's permissions. Generally only the owner of a file or the root superuser can change a file's permissions.

Mounting devices

File systems are extended by mounting devices, such as a CD-ROM, floppy drive, or removable hard drive.

To mount and dismount a file system:

  • The general syntax for mounting is mount special-device directory

    The special-device argument indicates the device driver file: for example, /dev/fd0 for the first floppy disk drive, or /dev/cdrom for a CD-ROM drive. The directory argument can refer to any directory, which you can create if you want, but by convention, it is often a subdirectory of /mnt.

    The general syntax for unmounting is umount name where name is either the special-device file or its mount point.

    Standard Linux Directories and Their Contents
    Path NameContents
    /The root directory
    /bin, /sbinImportant utilities necessary for minimum system operability
    /devDevice files for terminals, disks, modems, and so on
    /etcCritical system files
    /tmpTemporary files that disappear between reboots
    /usr/binExecutable files
    /usr/libSupport files for standard Linux programs
    /usr/manMan pages
    /usr/srcSource code
    /var/logLog files
    /var/spoolSpooling directories for printers, mail, and so on
    /var/tmpTemporary space (files that don't disappear between reboots)

To mount a CD-ROM:

1.
At the prompt, type

mount /dev/cdrom /mnt/cdrom

2.
Press Enter.

Figure 11.6. You can mount and unmount file systems using the Gnome control panel.


To unmount a CD-ROM:

1.
At the prompt, type

umount /dev/cdrom

or

umount /mnt/cdrom

2.
Press Enter.

Figure 11.7. You will be asked to confirm a mount or unmount.


To mount a floppy drive:

1.
At the prompt, type

mount /dev/fd0 /mnt/floppy

2.
Press Enter.

To unmount a floppy drive:

1.
At the prompt, type

umount /dev/fd0

or

umount /mnt/floppy

2.
Press Enter.

To mount a device using Gnome:

1.
Log on as root.

2.
On the Gnome desktop, open the Linux Configuration applet on the control panel.

3.
Scroll down until you see

Mount/Unmount File Systems in the list on the left (Figure 11.6).

4.
Select a device to mount it: for example, /dev/cdrom.

5.
Respond as appropriate when asked to confirm the mount (Figure 11.7).

Tip

You can use the control panel to unmount devices as well.


Figure 11.8. Directories are indicated with a "d" in the leftmost column when you list the contents of the current directory


Creating directories

Directories are the containers for other Linux directories and files. Most likely, every time you start a new project or want to group related files together, you will create a directory.

To create a new directory:

1.
Move to the location in the directory tree where you want to create the new directory.

2.
At the command prompt, type

mkdir mynewdir

3.
Press Enter.

4.
Type ls -l to list the current directories. You will see mynewdir listed with a "d" in the leftmost column, indicating that it is a directory (Figure 11.8).

Tip

If you try to create a directory using a name that already exists within the container directory, you will receive a message stating that a file of that name already exists, proving once again that directories are files in Linux.


Tip

The following are illegal characters in Linux file and directory names: <> {} [] () "" " * ? | / ^ ! # $ & ~


Tip

In addition to the illegal characters, avoid using spaces and dashes (-). Some Linux programs cannot handle them.


To delete a directory:

1.
Using the cd command, navigate to the parent of the directory you want to delete.

2.
At the prompt, use the rmdir command to remove the directory: for example, type rmdir mynewdir.

3.
Press Enter.

Creating files

If directories are the filing system, then files are the contents.

To list all files in a directory:

1.
At the prompt, type ls -al.

2.
Press Enter.

The files in the current directory will be displayed (Figure 11.9).

Figure 11.9. The command for listing files is l s..


Tip

The -l flag displays full information about the files; the -a flag displays all files, including hidden files. For more information on ls flags, see Appendix A.


To create a file:

1.
At the prompt, type touch followed by the new file name. For example, type

touch happy.file

2.
Press Enter.

Tip

There are many ways to create files. One approach is to use a text editor such as vi.


To copy a file (or directory):

  • To copy a file (or directory), use the cp command. For example, enter

    									cp myfileordir yourfileordir
    								

To move a file (or directory):

  • To move a file or directory, use the mv command. For example, enter

    									mv thefileordir /newloc/thefileordir
    								

To delete a file:

1.
At the prompt, type rm followed by the name of the file. For example, type

rm this.file.is.history

2.
Press Enter.

Tip

The rm command, depending on the flags you use, and whether you are logged on as root, can be extremely dangerous. For example, the -f flag removes write-protected files without prompting, and the -r flag deletes files and directories recursively. This means that if root issues the command rm -rf, the entire file system could be removed. Don't try this at home!


Figure 11.10. You can use the find command to display files whose name matches the supplied criteria.


To find a file:

  • The find command is an extremely flexible means of finding files. (For more information on the find command, see Appendix A.)

    For example, to find all the files in the /home/hdavis/ch11 directory that contain the word fig in the name, use the following syntax:

    									find /home/hdavis/ch11 -name _   '*fig*' -print
    								

    This displays all the files that match the condition (Figure 11.10).

To find text within a file (or files):

  • Use the grep command to search one or more files for specific text, or for text that matches a regular expression.

    Figure 11.11. The grep command is used to find text that matches expressions within a file.


    For example, the following grep command (See Figure 11.11) would search all the files in the current directory for the string elephant, printing three lines on either side of the occurrence of elephant and providing line numbers (because of the -n flag):

    									grep -n -3 elephant *
    								

To view the beginning of a file:

1.
Type head myfile.

2.
Press Enter.

The first 10 lines of the file will be displayed.

Tip

You can use the head command to view as many files as you want: for example, head -42 myfile displays the first 42 lines of myfile.


Tip

You can view the beginnings of multiple files by piping them to more: for example, head -5 my* | more


To view the end of a file:

1.
Type tail myfile.

2.
Press Enter.

The last 10 lines of the file will be displayed.

Tip

As with the head command, you can determine the number of lines that are displayed and display multiple files in a single tail command.


Tip

The tail command is useful for keeping track of the most recent changes to log files when you are tracking or debugging the operation of a program—such as a database engine—that writes to a log file as it progresses.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.194