The 6 Best Command Line Ways to View Files in Linux

In Linux, the content of the files can be viewed from the terminal and the Graphical User Interface (if supported by the distribution). Various commands, such as cat, nano, and gedit, allow you to view the file’s content. This is more effective and time-saving than if the user does not have to navigate to the directory to open a file and view its content.

This Linux post will address the best 6 ways to view files in Linux using the command line utilities. 

Method 1: Using the cat Command Utility

The most convenient method for viewing the file content is the cat command which displays the content of the file on the terminal.

Example: 

For example, if we want to view the file’s contents, “itsLinuxFoss”, run the command:

$ cat itsLinuxFoss

For more details on using the cat command, Click Here to read this detailed article on cat command.

Method 2: Using the more Command Utility

Next method of viewing the file’s contents in Linux is by using the more command. The “more” command displays the text files on one page at a time, and further content can be seen by pressing enter or the arrow key for a sharp jump. 

Example:

For example, the more command is applied on the file named “itsLinuxFoss.txt” as follows:

$ more itsLinuxFoss.txt

It shows the maximum percentage of the document that a terminal window can show. The rest of the file’s content can be viewed by pressing the enter key. 

Method 3: Using the less Command Utility

The usage of the “less” command is the same as that of the “more” command in Linux. The difference between more and less is: 

  • The less command offers the searching in text and smooth backward navigation, while the more command does not offer backward navigation.
  • The less command needs to be terminated using the “q” key, while the more automatically terminates when the file’s content is ended.

Example: 

The filename “ItsLinuxFoss.txt” is targeted using the less command as follows: 

$ less itsLinuxFoss.txt

Method 4: Using the nl Command Utility

The “nl” command is used to view the file contents with their line numbers. It is useful to accept input from a file and display the content:

Example:

For example, we will view the contents of the itsLinuxFoss using the nl command:

$ nl itsLinuxFoss

The line number can be observed in the output of the nl command.

Method 5: Using the xdg-open Command Utility

The “ xdg-open” command utility is utilized to open a file or URL and display the contents of the file in the application. After executing the script, it navigates to the specified file: 

Example:

For example, use the xdg-open command to view the “itsLinuxFoss” file:

$ xdg-open itsLinuxFoss

Method 6: Using the Text Editors

Another approach to view the contents of the file is by using the text editors. Various text editors, as per their specifications, are used. The most common one is “nano,” which comes by default in various distributions. 

Install Nano on Linux: 

$ sudo apt install nano                                   #For Debian/Ubuntu
$ sudo yum install nano                                 #For CentOS/RHEL

Example:

To use it, write the file name with it as shown below. 

$ nano itsLinuxFoss

The file will be opened: 

Using the text editors, the text file is opened in the editing mode. 

Conclusion

The 6 best ways to view files in Linux using the commands cat, more, less, nl, xdg-open, and the text editors. These tools assist the user to access the content of the file on the terminal without accessing/opening them directly. This post has listed the 6 best ways to open a file on the terminal.