How to View Contents of File in Linux

How to View Contents of File in Linux?

We all interact more with files on our personal computers. Viewing and editing the contents of important files such as configuration files, log files, text-based documents, and scripts is essential for various purposes. Configuration files are used for changes in system settings and programs, and log files are essential for detecting errors and troubleshooting the system. As a result, checking and analyzing the contents of log files and configuration files play an effective role in improving the stability and performance of the Linux system. On the other hand, to use educational documents in the form of text files and to access text files shared between colleagues, you need to view the file’s contents in Linux. The reasons for accessing the content of files in Linux do not end with mentioning these things but are very wide, and all users are well aware of the importance of reading the file. However, the topic that deserves discussion is how to view contents of File in Linux.

Viewing the contents of file in the Linux terminal is not as simple as reading the file in the Linux GUI, especially for beginner users who have just entered the Linux terminal environment, reading the file in Linux is a challenge. With the support of advanced tools, the powerful command line of Linux has made it possible to view file content in Linux. In this article, we will introduce 7 Linux command line tools to view file content in different distributions of Linux like Debian, Ubuntu, CentOS, etc., and teach how to use these powerful tools.

To use the powerful Linux command line tools to view file contents in the terminal environment that will be discussed in this article, you must use a Linux system or a Linux VPS. The Linux terminal is one of the most advanced and powerful environments to respond to various purposes, and that’s why it always wins in the competition with other operating systems. Therefore, using advanced Linux terminal tools allows you to manage your server more optimally. Let’s get acquainted with the most popular ways to read files in the Linux terminal.

1. cat command for viewing entire file contents

The simple text editor cat (concatenate) is one of the basic and powerful tools in Linux that is used to view the contents of files in Linux terminal directly. Initially, this command line tool was used to concatenate multiple files together, but now it is used to access the contents of files. To use the cat tool to view the contents of the file, type the file name along with the cat command in the Linux terminal as follows:

cat filename

Substitute the name of the desired file in the previous command so that you can access the contents of the file you want. The ls command in Linux is useful for accessing a list of files and subdirectories in Linux and knowing the exact names of files. In the output of the cat command, the entire contents of the file is displayed on the screen, that is, the entire page is filled with the entire text. If you read large files with the cat command, displaying and loading long information on the page may not be pleasant, and you may have trouble navigating between lines; Therefore, the cat command is not ideal for viewing the contents of large files.

Substitute the name of the desired file in the previous command so that you can access the content of the file you want. The ls command helps access a list of files and subdirectories in Linux and know the exact names of files. In the output of the cat command, the entire file content is displayed on the screen, that is, the entire page is filled with the text. If you read large files with the cat command, displaying and loading long information on the page may not be pleasant, and you may have trouble navigating between lines. Therefore, the cat command is not ideal for viewing the contents of large files.

One of the advantages of the cat command is to view the contents of several files simultaneously. For this purpose, you can use the following syntax:

cat filename1 filename2 filename3

In the previous command’s output, the files you specify will be displayed simultaneously in the Linux terminal.

The cat command is a versatile Linux command that offers various options to improve the display of file contents; here are some of the most common options:

  • -n or –number: numbers the lines of the file contents to facilitate referencing specific lines in the file.
  • -b or –number-nonblank: Adds line numbers to non-blank lines.
  • E or–show-ends: displays the $ character at the end of each line for detecting line breaks.
  • -A or –show-all: It shows tabs, line endings, and non-printable characters together.

The syntax of the cat command with options is as follows:

cat [option] [filename]

Also, keep in mind that you can combine options to customize and optimize the output of the cat command.

2. nl command for viewing and numbering file contents

The nl command, which stands for number lines, is a Linux command line tool for adding line numbers to the content of a text file. The nl command has almost the same function as the cat command, and in addition to displaying the file’s content, it numbers the lines of the content by default. Usually, the nl command is used to prepare and improve the readability of a text file for printing. Linux users run the nl command to facilitate checking configuration or log files whose contents are not numbered by default. Therefore, after finding the desired file through the Find command, enter the following command to view the numbered content of your desired file in Linux:

nl filename

The nl command also offers various options to improve the display and management of numbering, Including:

  • -b: Displays file contents without line numbering
  • -nln: Formats the line numbers to be left-justified
  • rn: Right-justified line numbers
  • a: Number all lines (default)
  • t: Numbers only non-empty lines
  • -s separator: Specifies a custom separator between the line numbers and the content
  • -v: Specifies the starting line number. (By default, nl starts numbering from 1)

If you want to learn more options of the nl command, the nl command man page will help you.

3. less command for paging and navigation in large file contents

The less command is a modern command-line pager tool for displaying file content in Linux, which simplifies the search in the text content of the file by paging the file content and supporting the ability to scroll in the Linux terminal ecosystem. Unlike the cat command, the less command optimizes and simplifies the reading of large files with pagination and the ability to scroll up and down and even horizontally in the file contents through arrow keys. The main syntax of the less command to view file contents in Linux is as follows:

less filename

As we explained, the output of the less command is much cleaner and more organized than the cat command and displays the contents of large files in the form of several pages that you can navigate and search in the contents of the file using the keyboard keys. In the following, we will teach you how to use the keyboard keys to navigate through the pages you received in the results of the less command:

  • F key: Scrolls one screen forward in the output (useful for monitoring log files)
  • B key: Scrolls one screen backward
  • Page Up key: Scrolls one screenful up
  • Page Down key: Scrolls one screenful down
  • Q key: closes the less viewer
  • G key: Goes to the end of the file
  • Up Arrow (↑): moves up in line
  • Down Arrow (↓): moves down in line
  • Right Arrow (→): Scrolls one character to the right
  • Left Arrow (←): Scrolls one character to the left
  • Home (Home or Fn+Left): Goes to the beginning of the file
  • End (End or Fn+Right): Goes to the end of the file
  • <number>G: Goes to a specific line number

Like the cat command, the less command supports more advanced features to improve its performance in displaying file content. Its most common options are as follows:

/ or (/word): You can search for a string or a pattern in the file’s content using this option.

-N: Display line numbers on the left side of the screen

-S: Prevent long lines from wrapping

-i: Perform case-insensitive searches while using / to search within the file

-P pattern: Start displaying the file at the first occurrence of the specified pattern (less -P "search_pattern" filename)

-p string: Customize the prompt displayed at the bottom of the screen (less -p "Enter your command:" filename)

+F: displays the latest data in the file.

In addition to the grep command in Linux, which is used to search for specified strings in text files, the less command, along with /, is used to search and view a specific word in the file’s content. To view the first occurrence of a particular word through the less command, use the following command:

less +/target-word /path-to-file

You can also use the less command with other commands to get more helpful information.

4. more command for viewing and navigation in large file contents

The “more” command, similar to the “less” command, is a pager tool in Linux used to view large files’ content. The “more” command is not as advanced as “less” in navigation in file content and is a simple alternative to the “less” command. The more command provides basic navigation through the spacebar and Enter key; By using Enter, the next line of content will be displayed, and through the spacebar, you will see the next screenful of content.

Compared to “less,” the “more” command provides fewer features and minimum interaction; for example, it does not support scrolling backward and is limited in providing features such as searching, line numbering, and advanced navigation. How to use the more command to view file contents in Linux is as follows:

more filename

In the output of the more command, in addition to the spacebar and Enter key, you can navigate using mouse scroll or Shift+Page-up and Shift+Page-down, and with the help of other keyboard keys that you learned in the previous section of the article in using the less command, navigation in long files becomes more efficient.

Although the more command is not as rich as the less command, it supports advanced options, some of which we will mention here:

  • -x: Displays the number of lines you want to see from the file’s content on each page.
  • –help: Displays the help message and a summary of command-line options for the” more” command.

5. head command to view the Specific Portions of the file contents

The head command is used to display a specific part of the contents of the file. As it is clear from the concept of the head command, by using the head command, you can see the beginning part of the contents of the text file, which by default displays the first 10 lines of a file. Still, by providing the -n option, it allows customizing the line numbers of content to be displayed. The main syntax of the head command to view the first part of the file content in Linux is as follows:

head [options] [filename]

You can run the following command to display the first 10 lines of the file:

head filename

To view a specific number of the beginning lines of the file content, as we said, use the -n option along with the head command. For example, to display the first 30 lines of the file, run the following command:

head -n 30 filename

The head command also supports displaying the beginning part of several files simultaneously. For this purpose, use the following command:

head file1 file2 file3

The previous command output will display the first 10 lines of the files you specified. If you want to see the file name along with the beginning part of several files in the output, run the -v option along with the head command:

head -v file1 file2 file3

The head command, like other Linux commands, offers various options for customizing the way a file’s content is displayed, and here we discuss the most common options:

  • -n: Specifies the number of lines to display.
  • -c: Specifies a specific part of the article based on the number of bytes instead of lines to display.
  • -v: When specifying multiple files to display their contents, it displays file headers (filenames).
  • -q: When used with multiple files, it suppresses the display of the file headers (filenames).
  • –help: provides information about how to use the head command and its options.

The head command is executed to display the beginning (head) of the text file, and it is not an ideal choice to display the entire content of a file.

6. tail command to view the end part of the Contents of File

The tail command has the same function as the head command and is intended to display part of the file content. The difference between tail command and head is that the tail command is used to view the end part (tail) of a text file. The tail command is useful for monitoring and viewing the latest lines added to the log file. By default, the tail command displays the last 10 lines of a file, but it allows specifying the number of different lines. The main syntax of the tail command to view the end of the file content in linux is as follows:

tail [options] [filename]

To view the last 10 lines of the file content using the tail, run the following command :

tail filename

Like the head command to specify the different number of lines, use the -n option followed by the desired line count; for example, to view the last 30 lines of the file, run the following command:

tail -n 30 filename

You can use the -f option to monitor new lines added to the file continuously:

tail -f filename

The output of this command is constantly updated by adding new lines in addition to displaying the last 10 lines of the file.

The tail command options are as follows:

  • -n: Customizes the number of content lines from the end of the file to display.
  • -f: Continuously displays new lines as they are added to a file.
  • –help: Displays a help message explaining the usage of the tail command and its options.

As a result, you can use the tail tool to view the end part of text files, which is a handy command to monitor and view the latest updates of important text-based files, such as log files.

The good news is that to customize and improve the output when reading Linux files, you can combine the introduced commands with each other. Therefore, you can combine the head and tail commands to view specific lines of the file content. For example, if you want to view lines from 45 to 55, run the following command:

head -n 55 filename | tail -n +45

The head -n 55 filename command displays the first 55 lines of the file, and the tail -n +45 command displays from line 45 to the end of the file, so the result you get is viewing lines 45 to 55 of the file’s content.

Note: Do not forget to type “+” in the previous command because it affects the behavior of the tail command.

7. View Contents of File using strings command

Viewing the readable text and strings of compiled executables, libraries, and other binary files is helpful in debugging and error messages, But how can we view the readable text content of binary files?

The strings command displays the readable text contained in binary files. Do not misunderstand, the strings command cannot convert binary files into text files, but if the binary file contains human-readable text strings, it will extract them. The main syntax of the strings command to view the readable text of binary files is as follows:

strings [options] [filename]

The previous command checks the binary file and finds and displays the human-readable text in the file content. Therefore, to review and view the content of binary files, do not go to cat, head, or tail commands. Of course, it should be noted that not all binary files contain readable text strings, so before using the strings command, check the nature of the file you intend to analyze.

FAQ

Use the command xdg-view followed with the filename. Based on the distribution and desktop environment you installed, you can use the gnome-view or kde-view commands along with the filename.

Editors such as Nano and Vi provide the possibility to edit the file. After viewing the file, press the" i "button to enter insert mode. The "wq" button is used to save the changes and exit the editor, and the changes applied to the file are deleted using the "q" button.

Conclusion

Now, using the 7 valuable commands that we introduced, you can view the content of your desired text files for various purposes. The cat and nl commands are used to display the entire file content, but the less command is ideal for reading large files and getting cleaner output; Because it paging the file’s content and provides more advanced features for browsing and searching the file’s content. If you do not need to view the entire content of the text file, using the head and tail commands can meet your needs and avoid occupying the system memory by loading a part of the file content instead of the entire content of large files.

We hope that reading this article has been beneficial for you and you have achieved what you wanted from reading this article.

If you know a more helpful command other than the commands listed in this article to view the contents of the file, we will be happy to share it with us and users.

Leave a Reply

Your email address will not be published. Required fields are marked.