Home » , , » Navigating the Linux File System

Navigating the Linux File System

Written By Sajib Barua on Sunday, August 19, 2012 | 12:18 AM

To use files and directories well, you need to understand the concept of a hierarchical file system. Even if you use the GUI file managers (addressed in the next chapter) to access files and folders (folders are also called directories), you can benefit from a lay of the land of the file system.
Understanding the Linux File System
Like any other operating system, Linux organizes information in files and directories. A directory is a special file that can contain other files and directories. Because a directory can contain other directories, this method of organizing files gives rise to a hierarchical structure. This hierarchical organization of files is the file system.
The Linux file system gives you a unified view of all storage on your PC. The file system has a single root directory, indicated by a forward slash (/). Within the root directory is a hierarchy of files and directories. Parts of the file system can reside in different physical media, such as a hard drive, a floppy disk, and a CD-ROM. Figure 4-1 illustrates the concept of the Linux file system (which is the same in any Linux system) and how it spans multiple physical devices.
If you’re familiar with MS-DOS or Windows, you may find something missing in the Linux file system: You don’t find drive letters in Linux. All disk drives and CD-ROM drives are part of a single file system.
The Linux file system provides a unified view of storage that may span multiple storage devices
Figure 4-1: The Linux file system provides a unified view of storage that may span multiple storage devices.
In Linux, you can have long filenames (up to 256 characters), and filenames are case-sensitive. Often these filenames have multiple extensions, such as sample.tar.Z. UNIX filenames can take many forms, such as the following: index.html, Makefile, binutils-2.15.92.0.2-5.i386.rpm, vsftpd_2.0.3-1_i386.deb, .bash_profile, and httpd_src.tar.gz.
To locate a file, you need more than just the filename. You also need information about the directory hierarchy. The extended filename, showing the full hierarchy of directories leading to the file, is the pathname. As the name implies, it’s the path to the file through the maze of the file system. Figure 4-2 shows a typical pathname for a file in Linux.
The path for the file shows the sequence of directories leading up to the file
Figure 4-2: The path for the file shows the sequence of directories leading up to the file.
As Figure 4-2 shows, the pathname has the following parts:
  • The root directory, indicated by a forward slash (/) character.
  • The directory hierarchy, with each directory name separated from the previous one by a forward slash (/) character. A / appears after the last directory name.
  • The filename, with a name and one or more optional extensions. (A period appears before each extension.)
The Linux file system has a well-defined set of top-level directories, and some of these directories have specific purposes. Finding your way around the file system is easier if you know the purpose of these directories. You also become adept at guessing where to look for specific types of files when you face a new situation. Table 4-1 briefly describes the top-level directories in the Linux file system.
Table 4-1 Top-Level Directories in the Linux File System
Directory Contains
/
Base of the file system. All files and directories are contained logically in the root, or /, directory, regardless of
their physical locations.
/bin
Executable programs that are part of the Linux operating system. Many Linux commands, such as cat, cp, ls, more, and tar, are located in /bin.
/boot
Linux kernel and other files that the LILO and GRUB boot managers need. (The kernel and other files can be anywhere, but placing them in the /boot directory is customary.)
/dev Special files that represent devices attached to the system.
/etc
Most system configuration files and the initialization scripts (in the /etc/rc.d subdirectory).
/home
Home directories of all users. User edulaney’s home directory, for example, is /home/edulaney.
/lib
Library files for all programs stored in /sbin and /bin directories (including the loadable driver modules) needed to start Linux.
/lost+found Lost files. Every disk partition has a lost+found directory.
/media
The /media/floppy directory for mounting floppy disks and the /media/cdrom or /media/cdrom0 directory for mounting the CD/DVD-ROM drive. If you have a CD/DVD recorder, you find a /media/cdrecorder directory instead of /media/cdrom and may also find /media/DVD. Used for mounting file systems on removable media, such as CD/DVD-ROM drives, flash drives, external drives, floppy disks, and Zip drives.
/mnt Temporarily mounted file systems
/opt
Storage for large application software packages. For example, some distributions install the OpenOffice.org Office suite in the /opt directory.
/proc
Various information about the processes running in the Linux system.
/root Home directory for the root user.
/sbin
Executable files representing commands typically used for system administration tasks and used by the root user. Commands such as halt and shutdown reside in the /sbin directory.
/srv
Data for services (such as Web and FTP) offered by this system.
/sys Information about the devices, as seen by the Linux kernel.
/tmp
Temporary directory that any user can use as a scratch directory, meaning that the contents of this directory are considered unimportant and usually are deleted every time the system boots.
/usr
Subdirectories for many important programs, such as the X Window System (in the /usr/X11R6 directory) and the online manual. (Table 4-2 shows some of the standard subdirectories in /usr.)
/var
Various system files (such as logs), as well as directories for holding other information, such as files for the Web server and anonymous FTP server.
The /usr and /var directories also contain a number of standard subdirectories. Table 4-2 lists the important subdirectories in /usr. Table 4-3 shows a similar breakdown for the /var directory.
Table 4-2
Important /usr Subdirectories
Subdirectory Description
/usr/bin
Executable files for many more Linux commands, including utility programs that are commonly available in Linux but aren’t part of the core Linux operating system.
/usr/games Some old Linux games.
/usr/include
Header files (filenames ending in .h) for the C and C++ programming languages and the X11 header files in the /usr/include/X11 directory and the Linux kernel header files in the /usr/include/linux directory.
/usr/lib
Libraries for C and C++ programming languages and many other libraries, such as database libraries and graphical toolkit libraries.
/usr/local
Local files. The /usr/local/bin directory, for example, is supposed to be the location for any executable program developed on your system.
/usr/sbin
Many administrative commands, such as commands for e-mail and networking.
/usr/share
Shared data, such as default configuration files and images for many applications. For example, /usr/share/gnome contains various shared files for the GNOME desktop, and /usr/share/doc has the documentation files for many Linux applications (such as the bash shell, the Sawfish window manager, and The GIMP image processing program).
/usr/share/man
Online manual (which you can read by using the man command).
/usr/src
Source code for the Linux kernel (the core operating system).

Table 4-3
Important /var Subdirectories
Subdirectory Contains
/var/cache
Storage area for cached data for applications.
/var/lib
Information relating to the current state of applications.
/var/lock
Locked files to ensure that a resource is used by one application only.
/var/log
Log files organized into subdirectories. The syslogd server stores its log files in /var/log, with the exact content of the files depending on the syslogd configuration file /etc /syslog.conf. For example, /var/log/messages is the main system log file; /var/log/secure contains log messages from secure services (such as sshd and xinetd); and /var/log/maillog contains the log of mail messages.
/var/mail User mailbox files.
/var/opt Variable data for packages stored in /opt directory.
/var/run Data describing the system since it was booted.
/var/spool Data that’s waiting for some kind of processing.
/var/tmp Temporary files preserved between system reboots.
/var/yp Network Information Service (NIS) database files.
 
Navigating the File System with Linux Commands
 

Although GUI file managers such as Nautilus (in GNOME) or Konqueror (in KDE) are easy to use, you can use them only if you have a working GUI desktop. Sometimes, you may not have a graphical environment to run a graphical file manager. For example, you may be logged in through a text terminal, or X may not be working on your system. In those situations, you have to rely on Linux commands to work with files and directories. You can always use Linux commands, even in the graphical environment — all you have to do is open a terminal window and type the Linux commands.

In this section, I briefly describe some Linux commands for moving around the Linux file system.

Commands for directory navigation

In Linux, when you log in as root, your home directory is /root. For other users, the home directory is usually in the /home directory. My home directory (when I log in as edulaney) is /home/edulaney. This information is stored in the /etc/passwd file. By default, only you have permission to save files in your home directory, and only you can create subdirectories in your home directory to further organize your files.

Linux supports the concept of a current directory, which is the directory on which all file and directory commands operate. After you log in, for example, your current directory is the home directory. To see the current directory, type the pwd command.

To change the current directory, use the cd command. To change the current directory to /usr/lib, type the following:

cd /usr/lib

Then, to change the directory to the cups subdirectory in /usr/lib, type this command:

cd cups

Now, if you use the pwd command, that command shows /usr/lib/cups as the current directory.

These two examples show that you can refer to a directory’s name in two ways:

  • Absolute pathname: An example is /usr/lib, which is an exact directory in the directory tree. Think of the absolute pathname as the complete mailing address for a package that the postal service will deliver to your next-door neighbor.
  • Relative directory name: An example is cups, which represents the cups subdirectory of the current directory, whatever that may be. Think of the relative directory name as giving the postal carrier directions from your house to the one next door so the carrier can deliver the package.

If I type cd cups in /usr/lib, the current directory changes to /usr/lib/cups. However, if I type the same command in /home/edulaney, the shell tries to change the current directory to /home/edulaney/cups.

Use the cd command without any arguments to change the current directory back to your home directory. No matter where you are, typing cd at the shell prompt brings you back home!

The tilde character (~) refers to your home directory. Thus, you can change the current directory to your home directory also by using the command cd ~. You can refer to another user’s home directory by appending that user’s name to the tilde. Thus, cd ~superman changes the current directory to the home directory of superman.

Wait, there’s more. A single dot (.) and two dots (. .) — often cleverly referred to as dot-dot — also have special meanings. A single dot (.) indicates the current directory, whereas two dots (. .) indicate the parent directory. For example, if the current directory is /usr/share, you go one level up to /usr by typing the following:

cd . .

Commands for directory listings and permissions

You can get a directory listing by using the ls command. By default, the ls command — without options — displays the contents of the current directory in a compact, multicolumn format. For example, type the next two commands to see the contents of the /etc/X11 directory:

cd /etc/X11
ls

The output looks like this. (On the console, you see some items in different colors.)

X Xsession.d cursors rgb.txt xkb
XF86Config-4 Xsession.options default-display-manager rstart xserver
Xresources Xwrapper.config fonts sysconfig xsm
Xsession app-defaults gdm xinit

From this listing (without the colors), you can’t tell whether an entry is a file or a directory. To tell the directories and files apart, use the -F option with ls, like this:

ls –F

This time, the output gives you some more clues about the file types:

X@ Xsession.d/ cursors/ rgb.txt xkb/
XF86Config-4 Xsession.options default-display-manager rstart/ xserver/
Xresources/ Xwrapper.config fonts/ sysconfig/ xsm/
Xsession* app-defaults/ gdm@ xinit/

The output from ls -F shows the directory names with a slash (/) appended to them. Plain filenames appear as is. The at sign (@) appended to a filename (for example, notice the file named X) indicates that this file is a link to another file. (In other words, this filename simply refers to another file; it’s a shortcut.) An asterisk (*) is appended to executable files. (Xsession, for example, is an executable file.) The shell can run any executable file.

You can see even more detailed information about the files and directories with the -l option:

ls –l

For the /etc/X11 directory, a typical output from ls -l looks like the following:

total 84
lrwxrwxrwx 1 root root 20 Jul 15 20:32 X -> /usr/bin/X11/XFree86
-rw-r--r-- 1 root root 2878 Jul 16 14:50 XF86Config-4
drwxr-xr-x 2 root root 4096 Jul 15 20:32 Xresources
-rwxr-xr-x 1 root root 3456 Jun 1 01:59 Xsession
drwxr-xr-x 2 root root 4096 Jul 15 20:34 Xsession.d
-rw-r--r-- 1 root root 217 Jun 1 01:59 Xsession.options
-rw------- 1 root root 771 Jul 15 20:32 Xwrapper.config
drwxr-xr-x 2 root root 4096 Jul 15 20:35 app-defaults
. . . lines deleted . . .

This listing shows considerable information about every directory entry — each of which can be a file or another directory. Looking at a line from the right column to the left, you see that the rightmost column shows the name of the directory entry. The date and time before the name show when the last modifications to that file were made. To the left of the date and time is the size of the file in bytes.

The file’s group and owner appear to the left of the column that shows the file size. The next number to the left indicates the number of links to the file. (A link is like a shortcut in Windows.) Finally, the leftmost column shows the file’s permission settings, which determine who can read, write, or execute the file.

The first letter has a special meaning, as the following list shows:

  • l: The file is a symbolic link to another file. In other words, it’s a shortcut to something else.

  • d: The file is a directory. It will appear as a folder in a GUI.

  • - (hyphen): The file is normal. By normal, I mean that it isn’t a directory, a link, or anything else odd. Most of the items on your system are just normal files.

  • b: The file represents a block device, such as a disk drive.

  • c: The file represents a character device, such as a serial port or a terminal.

After that first letter, the leftmost column shows a sequence of nine characters, which appear as rwxrwxrwx when each letter is present. Each letter indicates a specific permission. A hyphen (-) in place of a letter indicates no permission for a specific operation on the file. Think of these nine letters as three groups of three letters (rwx), interpreted as follows:

  • Leftmost rwx group: Controls the read, write, and execute permission of the file’s owner. In other words, if you see rwx in this position, the file’s owner can read (r), write (w), and execute (x) the file. A hyphen in the place of a letter indicates no permission. Thus, the string rw- means the owner has read and write permission but not execute permission. Although executable programs (including shell programs) typically have execute permission, directories treat execute permission as equivalent to use permission: A user must have execute permission on a directory before he or she can open and read the contents of the directory.
  • Middle rwx group: Controls the read, write, and execute permission of any user belonging to that file’s group.

  • Rightmost rwx group: Controls the read, write, and execute permission of all other users (collectively thought of as the world).

Thus, a file with the permission setting rwx------ is accessible only to the file’s owner, whereas the permission setting rwxr--r-- makes the file readable by the world.

An interesting feature of the ls command is that it doesn’t list any file whose name begins with a period. To see these files, you must use the ls command with the -a option, as follows:

ls –a

Try this command in your home directory and then compare the result with what you see when you don’t use the -a option:

  1. Type cd to change to your home directory.
  2. Type ls -F to see the files and directories in your home directory.
  3. Type ls -aF to see everything, including hidden files.

Most Linux commands take single-character options, each with a hyphen as a prefix. When you want to use several options, type a hyphen and concatenate (string together) the option letters, one after another. Thus, ls -al is equivalent to ls -a -l as well as ls -l -a.

next Linux Commands for changing permissions and ownerships

Share this article :

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Linux - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger