-->

Viewing File Contents On Linux

You can use several commands for looking inside files without having to pull out a text editor utility. This section demonstrates a few of the commands you have available to help you examine files.

Viewing File Contents On Linux
Viewing File Contents On Linux

Viewing the file type

Before you go charging off trying to display a file, try to get a handle on what type of file it is. If you try to display a binary file, you get lots of gibberish on your monitor and may even lock up your terminal emulator.

The file command is a handy little utility to have around. It can peek inside of a file and determine just what kind of file it is:

  1. $ file my_file
  2. my_file: ASCII text
  3. $

The file in the preceding example is a text file. The file command determined not only that the file contains text but also the character code format of the text file, ASCII.

This following example shows a file that is simply a directory. Thus, the file command gives you another method to distinguish a directory:

  1. $ file New_Dir
  2. New_Dir: directory
  3. $

This third file command example shows a file, which is a symbolic link. Note that the file command even tells you to which file it is symbolically linked:

  1. $ file sl_data_file
  2. sl_data_file: symbolic link to ‘data_file’
  3. $

The following example shows what the file command returns for a script file. Although the file is ASCII text, because it’s a script file, you can execute (run) it on the system:

  1. $ file my_script
  2. my_script: Bourne-Again shell script, ASCII text executable
  3. $

The final example is a binary executable program. The file command determines the platform that the program was compiled for and what types of libraries it requires. This is an especially handy feature if you have a binary executable program from an unknown source:

  1. $ file /bin/ls
  2. /bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV),
  3. dynamically linked (uses shared libs), for GNU/Linux 2.6.24,
  4. […]
  5. $

Now that you know a quick method for viewing a file’s type, you can start displaying and viewing files.

Viewing the whole file

If you have a large text file on your hands, you may want to be able to see what’s inside of it. Linux has three different commands that can help you here.

Using the cat command

The cat command is a handy tool for displaying all the data inside a text file:

  1. $ cat test1
  2. hello
  3. This is a test file.
  4. That we’ll use to test the cat command.
  5. $
  6. Nothing too exciting, just the contents of the text file. However, the cat command has a
  7. few parameters that can help you out.
  8. The -n parameter numbers all the lines for you:
  9. $ cat -n test1
  10. 1 hello
  11. 2
  12. 3 This is a test file.
  13. 4
  14. 5
  15. 6 That we’ll use to test the cat command.
  16. $

That feature will come in handy when you’re examining scripts. If you just want to number the lines that have text in them, the -b parameter is for you:

  1. $ cat -b test1
  2. 1 hello
  3. 2 This is a test file.
  4. 3 That we’ll use to test the cat command.
  5. $
  6. Finally, if you don’t want tab characters to appear, use the -T parameter:
  7. $ cat -T test1
  8. hello
  9. This is a test file.
  10. That we’ll use to∧Itest the cat command.
  11. $

The -T parameter replaces any tabs in the text with the ∧I character combination. For large files, the cat command can be somewhat annoying. The text in the file just quickly scrolls off the display without stopping. Fortunately, we have a simple way to solve this problem.

Using the more command

The main drawback of the cat command is that you can’t control what’s happening after you start it. To solve that problem, developers created the more command. The more command displays a text file, but stops after it displays each page of data. We typed the command more /etc/bash.bashrc to produce the sample more screen shown in Picture below.

Using the more command to display a text file
Using the more command to display a text file


Notice at the bottom of the screen in Figure 3.3 that the more command displays a tag showing that you’re still in the more application and how far along (56%) in the text file you are. This is the prompt for the more command.

The more command is a pager utility. Remember from earlier in this chapter a pager utility displays selected bash manual pages when you use the man command. Similarly to navigating through the man pages, you can use more to navigate through a text file by pressing the spacebar or you can go forward line by line using the Enter key. When you are finished navigating through the file using more, press the q key to quit.

The more command allows some rudimentary movement through the text file. For more advanced features, try the less command.

Using the less command

From its name, it sounds like it shouldn’t be as advanced as the more command. However, the less command name is actually a play on words and is an advanced version of the more command (the less command name comes from the phrase “less is more”). It provides several very handy features for scrolling both forward and backward through a text file, as well as some pretty advanced searching capabilities.

The less command can also display a file’s contents before it finishes reading the entire file. The cat and more commands cannot do this.

The less command operates much the same as the more command, displaying one screen of text from a file at a time. It supports the same command set as the more command, plus many more options.

Tip
To see all the options available for the less command, view its man pages by typing man less. You can do the same for the more command to see the reference material concerning its various options as well.

One set of features is that the less command recognizes the up and down arrow keys as well as the Page Up and Page Down keys (assuming that you’re using a properly defined terminal). This gives you full control when viewing a file.

Viewing parts of a file

Often the data you want to view is located either right at the top or buried at the bottom of a text file. If the information is at the top of a large file, you still need to wait for the cat or more commands to load the entire file before you can view it. If the information is located at the bottom of a file (such as a log file), you need to wade through thousands of lines of text just to get to the last few entries. Fortunately, Linux has specialized commands to solve both of these problems.

Using the tail command

The tail command displays the last lines in a file (the file’s “tail”). By default, it shows the last 10 lines in the file.

For these examples, we created a text file containing 20 text lines. It is displayed here in its entirety using the cat command:

  1. $ cat log_file
  2. line1
  3. line2
  4. line3
  5. line4
  6. line5
  7. Hello World - line 6
  8. line7
  9. line8
  10. line9
  11. line10
  12. line11
  13. Hello again - line 12
  14. line13
  15. line14
  16. line15
  17. Sweet - line16
  18. line17
  19. line18
  20. line19
  21. Last line - line20
  22. $

Now that you have seen the entire text file, you can see the effect of using tail to view the file’s last 10 lines:

  1. $ tail log_file
  2. line11
  3. Hello again - line 12
  4. line13
  5. line14
  6. line15
  7. Sweet - line16
  8. line17
  9. line18
  10. line19
  11. Last line - line20
  12. $

You can change the number of lines shown using tail by including the -n parameter. In this example, only the last two lines of the file are displayed, by adding -n 2 to the tail command:

  1. $ tail -n 2 log_file
  2. line19
  3. Last line - line20
  4. $

The -f parameter is a pretty cool feature of the tail command. It allows you to peek inside a file as the file is being used by other processes. The tail command stays active and continues to display new lines as they appear in the text file. This is a great way to monitor the system log files in real-time mode.

Using the head command

The head command does what you’d expect; it displays a file’s first group of lines (the file’s “head”). By default, it displays the first 10 lines of text:

  1. $ head log_file
  2. line1
  3. line2
  4. line3
  5. line4
  6. line5
  7. Hello World - line 6
  8. line7
  9. line8
  10. line9
  11. line10
  12. $

Similar to the tail command, the head command supports the -n parameter so you can alter what’s displayed. Both commands also allow you to simply type a dash along with the number of lines to display, as shown here:

  1. $ head -5 log_file
  2. line1
  3. line2
  4. line3
  5. line4
  6. line5
  7. $

Usually the beginning of a file doesn’t change, so the head command doesn’t support the - f parameter feature as the tail command does. The head command is a handy way to just peek at the beginning of a file.

Summary

This chapter covered the basics of working with the Linux filesystem from a shell prompt. We began with a discussion of the bash shell and showed you how to interact with the shell. The command line interface (CLI) uses a prompt string to indicate when it’s ready for you to enter commands.

The shell provides a wealth of utilities you can use to create and manipulate files. Before you start playing with files, you should understand how Linux stores them. This chapter discussed the basics of the Linux virtual directory and showed you how Linux references storage media devices. After describing the Linux filesystem, the chapter walked you through using the cd command to move around the virtual directory.

After showing you how to get to a directory, the chapter demonstrated how to use the ls command to list the files and subdirectories. Lots of parameters can customize the output of the ls command. You can obtain information on files and directories by using the ls command.

The touch command is useful for creating empty files and for changing the access or modification times on an existing file. The chapter also discussed using the cp command to copy existing files from one location to another. It walked you through the process of linking files instead of copying them, providing an easy way to have the same file in two locations without making a separate copy. The ln command provides this linking ability.

Next, you learned how to rename files (called moving) in Linux using the mv command and saw how to delete files (called removing) using the rm command. This chapter also showed you how to perform the same tasks with directories, using the mkdir and rmdir commands.

Finally, the chapter closed with a discussion on viewing the contents of files. The cat, more, and less commands provide easy methods for viewing the entire contents of a file, while the tail and head commands are great for peeking inside a file to just see a small portion of it.

The next chapter continues the discussion on bash shell commands. We’ll look at more advanced administrator commands that come in handy as you administer your Linux system.

0 Response to "Viewing File Contents On Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel