Navigating the Filesystem On Linux
When you log into the system and reach the shell command prompt, you are usually placed in your home directory. Often, you want to explore other areas in the Linux system besides just your home directory. This section describes how to do that using shell commands. To start, you need to take a tour of just what the Linux filesystem looks like so you know where you are going.
The first difference you’ll notice is that Linux does not use drive letters in pathnames. In the Windows world, the physical drives installed on the computer determine the pathname of the file. Windows assigns a letter to each physical disk drive, and each drive contains its own directory structure for accessing files stored on it.
For example, in Windows you may be used to seeing the file paths such as:
This is not the method used by Linux. Linux stores files within a single directory structure, called a virtual directory. The virtual directory contains file paths from all the storage devices installed on the computer, merged into a single directory structure.
The Linux virtual directory structure contains a single base directory, called the root. Directories and files beneath the root directory are listed based on the directory path used to get to them, similar to the way Windows does it.
The tricky part about the Linux virtual directory is how it incorporates each storage device. The first hard drive installed in a Linux system is called the root drive. The root drive contains the virtual directory core. Everything else builds from there.
On the root drive, Linux can use special directories as mount points. Mount points are directories in the virtual directory where you can assign additional storage devices. Linux causes files and directories to appear within these mount point directories, even though they are physically stored on a different drive.
Often system files are physically stored on the root drive. User files are typically stored on a separate drive or drives, as shown in Figure below.
Figure The Linux file structure shows two hard drives on the computer. One hard drive is associated with the root of the virtual directory (indicated by a single forward slash). Other hard drives can be mounted anywhere in the virtual directory structure. In this example, the second hard drive is mounted at the location /home, which is where the user directories are located.
The Linux filesystem structure originally evolved from the Unix file structure. In a Linux filesystem, common directory names are used for common functions. The Table below lists some of the more common Linux virtual top-level directory names and their contents.
The common Linux directory names are based upon the Filesystem Hierarchy Standard (FHS). Many Linux distributions maintain compliance with FHS. Therefore, you should be able to easily find files on any FHS-compliant Linux systems.
You can move around the virtual directory using a graphical interface. However, to move around the virtual directory from a CLI prompt, you need to learn to use the cd command.
The cd command may take a single parameter, destination, which specifies the directory name you want to go to. If you don’t specify a destination on the cd command, it takes you to your home directory.
The destination parameter can be expressed using two different methods. One method is using an absolute directory reference. The other method uses a relative directory reference.
The following sections describe each of these methods. The differences between these two methods are important to understand as you traverse the filesystem.
An absolute directory reference always begins with a forward slash (/), indicating the virtual directory system’s root. Thus, to reference user binaries, contained within the bin directory stored within the usr directory, you would use an absolute directory reference as follows:
The tilde indicates that your shell session is located in your home directory. After you move out of your home directory, the absolute directory reference is shown in the prompt, if the prompt has been configured to do so.
Instead, a relative directory reference starts with either a directory name (if you’re traversing to a directory under your current directory) or a special character. For example, if you are in your home directory and want to move to your Documents subdirectory, you can use the cd command along with a relative directory reference:
Also notice in the example that if the prompt is configured to display the present working directory, it keeps the tilde in the display. This shows that the present working directory is in a directory under the user’s home directory.
The two special characters used for relative directory references are:
You can use the single dot, but it doesn’t make sense to use it with the cd command. Later in the chapter, you will see how another command uses the single dot for relative directory references effectively.
The double dot character is extremely handy when trying to traverse a directory hierarchy. For example, if you are in the Documents directory under your home directory and need to go to your Downloads directory, also under your home directory, you can do this:
Looking at the Linux filesystem
If you’re new to the Linux system, you may be confused by how it references files and directories, especially if you’re used to the way the Microsoft Windows operating system does that. Before exploring the Linux system, it helps to have an understanding of how it’s laid out.The first difference you’ll notice is that Linux does not use drive letters in pathnames. In the Windows world, the physical drives installed on the computer determine the pathname of the file. Windows assigns a letter to each physical disk drive, and each drive contains its own directory structure for accessing files stored on it.
For example, in Windows you may be used to seeing the file paths such as:
c:\Users\Rich\Documents\test.docThe Windows file path tells you exactly which physical disk partition contains the file named test.doc. For example, if you saved test.doc on a flash drive, designated by the J drive, the file path would be J:∖test.doc. This path indicates that the file is located at the root of the drive assigned the letter J.
This is not the method used by Linux. Linux stores files within a single directory structure, called a virtual directory. The virtual directory contains file paths from all the storage devices installed on the computer, merged into a single directory structure.
The Linux virtual directory structure contains a single base directory, called the root. Directories and files beneath the root directory are listed based on the directory path used to get to them, similar to the way Windows does it.
TipIn Linux, you will see file paths similar to the following:
You’ll notice that Linux uses a forward slash (/) instead of a backward slash (∖) to
denote directories in file paths. The backslash character in Linux denotes an escape
character and causes all sorts of problems when you use it in a file path. This may
take some getting used to if you’re coming from a Windows environment.
/home/Rich/Documents/test.docThis indicates the file test.doc is in the directory Documents, under the directory rich, which is contained in the directory home. Notice that the path doesn’t provide any information as to which physical disk the file is stored on.
The tricky part about the Linux virtual directory is how it incorporates each storage device. The first hard drive installed in a Linux system is called the root drive. The root drive contains the virtual directory core. Everything else builds from there.
On the root drive, Linux can use special directories as mount points. Mount points are directories in the virtual directory where you can assign additional storage devices. Linux causes files and directories to appear within these mount point directories, even though they are physically stored on a different drive.
Often system files are physically stored on the root drive. User files are typically stored on a separate drive or drives, as shown in Figure below.
The Linux file structure |
Figure The Linux file structure shows two hard drives on the computer. One hard drive is associated with the root of the virtual directory (indicated by a single forward slash). Other hard drives can be mounted anywhere in the virtual directory structure. In this example, the second hard drive is mounted at the location /home, which is where the user directories are located.
The Linux filesystem structure originally evolved from the Unix file structure. In a Linux filesystem, common directory names are used for common functions. The Table below lists some of the more common Linux virtual top-level directory names and their contents.
Common Linux Directory Names Table |
The common Linux directory names are based upon the Filesystem Hierarchy Standard (FHS). Many Linux distributions maintain compliance with FHS. Therefore, you should be able to easily find files on any FHS-compliant Linux systems.
NoteWhen you log in to your system and reach a shell CLI prompt, your session starts in your home directory. Your home directory is a unique directory assigned to your user account. When a user account is created, the system normally assigns a unique directory for the account (7).
The FHS is occasionally updated. You may find that some Linux distributions are still using an older FHS standard, while other distributions only partially implement the current standard. To keep up to date on the FHS standard, visit its official home at
http://www.pathname.com/fhs/.
You can move around the virtual directory using a graphical interface. However, to move around the virtual directory from a CLI prompt, you need to learn to use the cd command.
Traversing directories
You use the change directory command (cd) to move your shell session to another directory in the Linux filesystem. The cd command syntax is pretty simplistic: cd destination.The cd command may take a single parameter, destination, which specifies the directory name you want to go to. If you don’t specify a destination on the cd command, it takes you to your home directory.
The destination parameter can be expressed using two different methods. One method is using an absolute directory reference. The other method uses a relative directory reference.
The following sections describe each of these methods. The differences between these two methods are important to understand as you traverse the filesystem.
Using absolute directory references
You can reference a directory name within the virtual directory system using an absolute directory reference. The absolute directory reference defines exactly where the directory is in the virtual directory structure, starting at the root. Think of the absolute directory reference as the full name for a directory.An absolute directory reference always begins with a forward slash (/), indicating the virtual directory system’s root. Thus, to reference user binaries, contained within the bin directory stored within the usr directory, you would use an absolute directory reference as follows:
/usr/binWith the absolute directory reference, there’s no doubt as to exactly where you want to go. To move to a specific location in the filesystem using the absolute directory reference, you just specify the full pathname in the cd command:
christine@server01:∼$ cd /usr/binNotice in the preceding example that the prompt originally had a tilde (∼) in it. After the change to a new directory occurred, the tilde was replaced by /usr/bin. This is where a CLI prompt can help you keep track of where you are in the virtual directory structure.
christine@server01:/usr/bin$
The tilde indicates that your shell session is located in your home directory. After you move out of your home directory, the absolute directory reference is shown in the prompt, if the prompt has been configured to do so.
NoteIf your prompt has not been configured to show the shell session’s current absolute directory location, then you can display the location via a shell command. The pwd command displays the shell session’s current directory location, which is called the present working directory. An example of using the pwd command is shown here.
If your shell CLI prompt does not show your shell session’s current location, then it
has not been configured to do so. Chapter 6 shows you how to make configuration
changes, if you desire modifications to your CLI prompt.
christine@server01:/usr/bin$ pwd
/usr/bin
christine@server01:/usr/bin$
TipYou can move to any level within the entire Linux virtual directory structure from any level using the absolute directory reference:
It is a good habit to use the pwd command whenever you change to a new present working directory. Because many shell commands operate on the present working
directory, you always want to make sure you are in the correct directory before
issuing a command.
christine@server01:/usr/bin$ cd /var/logYou can also quickly jump to your home directory from any level within the Linux virtual directory structure:
christine@server01:/var/log$
christine@server01:/var/log$ pwd
/var/log
christine@server01:/var/log$
christine@server01:/var/log$ cdHowever, if you’re just working within your own home directory structure, often using absolute directory references can get tedious. For example, if you’re already in the directory /home/christine, it seems somewhat cumbersome to have to type the command:
christine@server01:~$
christine@server01:∼$ pwd
/home/christine
christine@server01:∼$
cd /home/christine/Documentsjust to get to your Documents directory. Fortunately, there’s a simpler solution.
Using relative directory references
Relative directory references allow you to specify a destination directory reference relative to your current location. A relative directory reference doesn’t start with a forward slash (/).Instead, a relative directory reference starts with either a directory name (if you’re traversing to a directory under your current directory) or a special character. For example, if you are in your home directory and want to move to your Documents subdirectory, you can use the cd command along with a relative directory reference:
christine@server01:∼$ pwdIn the preceding example, note that no forward slash (/) was used. Instead a relative directory reference was used and the present work directory was changed from /home/christine to /home/christine/Documents, with much less typing.
/home/christine
christine@server01:~$
christine@server01:∼$ cd Documents
christine@server01:∼/Documents$ pwd
/home/christine/Documents
christine@server01:~/Documents$
Also notice in the example that if the prompt is configured to display the present working directory, it keeps the tilde in the display. This shows that the present working directory is in a directory under the user’s home directory.
TipYou can use a relative directory reference with the cd command in any directory containing subdirectories. You can also use a special character to indicate a relative directory location.
If you are new to the command line and the Linux directory structure, it is
recommended that you stick with absolute directory references for a while. After you
become more familiar with the directory layout, switch to using relative directory
references.
The two special characters used for relative directory references are:
- The single dot (.) to represent the current directory
- The double dot (..) to represent the parent directory
You can use the single dot, but it doesn’t make sense to use it with the cd command. Later in the chapter, you will see how another command uses the single dot for relative directory references effectively.
The double dot character is extremely handy when trying to traverse a directory hierarchy. For example, if you are in the Documents directory under your home directory and need to go to your Downloads directory, also under your home directory, you can do this:
christine@server01:∼/Documents$ pwdThe double dot character takes you back up one level to your home directory; then the /Downloads portion of the command takes you back down into the Downloads directory. You can use as many double dot characters as necessary to move around. For example, if you are in your home directory (/home/christine) and want to go to the /etc directory, you could type the following:
/home/christine/Documents
christine@server01:∼/Documents$ cd ../Downloads
christine@server01:∼/Downloads$ pwd
/home/christine/Downloads
christine@server01:∼/Downloads$
christine@server01:∼$ cd ../../etcOf course, in a case like this, you actually have to do more typing rather than just typing the absolute directory reference, /etc. Thus, use a relative directory reference only if it makes sense to do so.
christine@server01:/etc$ pwd
/etc
christine@server01:/etc$
NoteNow that you know how to traverse the directory system and confirm your present working directory, you can start to explore what’s contained within the various directories. The next section takes you through the process of looking at files within the directory structure.
It’s helpful to have a long informative shell CLI prompt, as used in this chapter
section. However, for clarity purposes, a simple $ prompt is used in the rest of the
book’s examples.
0 Response to "Navigating the Filesystem On Linux"
Post a Comment