Files are one of the most important things that you interact with on a Linux PC. Some of the most common files you will encounter on a Linux system include configuration files, log files, and scripts.

The ability to easily view files from the command line is a powerful feature that Linux provides to its users. This guide will show you the different command-line utilities that you can use to view files in Linux.

1. Cat

The cat utility is one of the most used commands for viewing file content in Linux. You can use the command for concatenating and printing standard file output. To view the contents of a file using cat, simply type the command name followed by the file you want to view.

        cat /etc/passwd
    

In the command above, the cat command displays the contents of the passwd file. The passwd file contains user-related details on a Linux machine.

By default, the output of the cat command will not be numbered. Therefore, if you want to number the lines in the output then you can use the -n option as follows.

        cat -n /etc/passwd 
    

You can also use the cat utility to view multiple files at a time.

        sudo cat /etc/passwd /etc/shadow
    

Note: The aforementioned cat command uses sudo because the /etc/shadow file requires the user to have elevated privileges in order to view it.

2. Nl

The nl command, short for number lines, is very similar to the cat command, with the exception that the nl command numbers the output lines by default.

        nl /etc/passwd 
    
output of a file in linux using nl command

In addition to numbering the output, the nl utility gives you the ability to format the output and align the numbering of the output. For example, you can format the line numbers to be left-justified as follows.

        nl -nln /etc/passwd 
    

Although the nl utility is primarily used for numbering output lines, you can also choose not to number the lines using the -b option as follows.

         nl -b n /etc/passwd 
    

3. More

Some of the file output that you will encounter can be pretty large. The more utility enables easier viewing of large files one screenful at a time.

To view the passwd file in smaller sections, you can use the more command:

        more /etc/passwd 
    

The command above will only display output that can fit the size of your terminal. Use the F keyboard key to move forward in the output and the B key to move backward.

If you wish to specify the number of lines displayed in each section at a time then you can use the -x option, where x is the number of lines you want the command to display. For example, the following command will display four lines per screen:

        more -4 /etc/passwd
    

Use the command below to learn more about navigating the output generated by the more command and how to search strings within the output.

        more --help
    

4. Less

The less utility is a successor of the more command as it provides additional enhancements and emulation than the latter one. In addition, the less utility is faster and has increased efficiency because it does not wait to read the entire file content before it can display some output.

        less /etc/passwd
    

Similar to the more command, use the F keyboard key to move forward in the output and the B key to move backward.

To display line numbers in the output, use the -N option as follows.

        less -N /etc/passwd
    

Searching for Text

To search for a string or a pattern within the less utility output, simply press the / key on your keyboard followed by the string you want to search for. For example, to search for the string games in the output of less /etc/passwd, type /games on your keyboard followed by the Enter key.

search output within in less command output on linux

The text you are searching for will be highlighted as above. To move forward in the search, press the n key on the keyboard, and to move backward. press N. Note that the n character is case-sensitive depending on the direction of movement.

Another powerful feature of the less utility is that you can use it as a pipe in some output stream or for other commands. For example, the command dmesg displays kernel ring buffer messages or other information related to the kernel during bootup. As this log output can be pretty long, you can use the less command to limit the output and for easy navigation.

        sudo dmesg | less
    

You can also use the less utility to display data in an interactive manner. For example, when used with the dmesg command, you can set the less command to always show you the latest data as the system keeps adding more lines to the output. To do the same, use the +F option with the command as follows:

        sudo dmesg | less +F
    
less command in linux used interactively

As you can see from the output above, the less utility shows that it is waiting for more data to display in the output. Press Ctrl + C to abort followed by Q to clear the output.

Sometimes you might only want to view the first few lines of a file, and this is where the head utility comes in handy. By default, only the first 10 lines of a file are shown.

        head /etc/passwd
    

To customize the number of lines you want to view, use the -x option, where x is the number of lines you want to view. For example, to view the first 20 lines:

        head -20 /etc/passwd
    

6. Tail

The tail command works in an almost opposite manner to the head utility i.e. it outputs the last part of a file. By default, the last 10 lines of a file are shown.

        tail /etc/passwd
    

Like the head command, you can also customize the number of lines you want to view.

        tail -10 /etc/passwd
    

To display live data in interactive mode, use the -f flag with the tail command. For example, to always view the latest 10 log messages in the syslog file:

        tail -f /var/log/syslog
    

Working With Files in the Linux Command Line

This guide has shown you the different ways in which you can view files in Linux. Being able to view and work with files directly from the command line is key. While these utilities offer features that allow you to search for strings, there are various other commands like the grep utility that you can use for filtering output on your system.

In addition to the terminal, users can also manage and navigate through their file system graphically. Several file manager applications are available on Linux that you can try for free.