Linux Command Line and Shell Scripting Bible. Christine Bresnahan. Читать онлайн. Newlib. NEWLIB.NET

Автор: Christine Bresnahan
Издательство: Автор
Серия:
Жанр произведения: Зарубежная образовательная литература
Год издания: 0
isbn: 9781118984192
Скачать книгу
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:

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

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

      Note

      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.

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

      Tip

      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.

      You can move to any level within the entire Linux virtual directory structure from any level using the absolute directory reference:

      You can also quickly jump to your home directory from any level within the Linux virtual directory structure:

      However, 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:

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

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

      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.

      Tip

      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.

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

      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:

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

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

      Note

      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.

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

      Listing Files and Directories

      To see what files are available on the system, use the list command (ls). This section describes the ls command and options available to format the information it can display.

Displaying a basic listing

      The ls command at its most basic form displays the files and directories located in your current directory:

      Notice that the ls command produces the listing in alphabetical order (in columns rather than rows). If you're using a terminal emulator that supports color, the ls command may also show different types of entries in different colors. The LS_COLORS environment variable controls this feature. (Environment variables are covered in Chapter 6). Different Linux distributions set this environment variable depending on the capabilities of the terminal emulator.

      If you don't have a color terminal emulator, you can use the – F parameter with the ls command to easily distinguish files from directories. Using the – F parameter produces the following output:

      The