Home Linux Commands View the Contents of a File in Linux Command Line

View the Contents of a File in Linux Command Line

The usual way to view the contents of a file is to simply open it in a text editor. However, for more quick viewing and in fact, also for automating in a shell script, the method of using a text editor does not suit.

There are many commands in Linux to solve this problem: to display the file contents on the command line.

Let us take a look at a few such commands.

View Contents of File Using Cat Command

A cat command is the most commonly used command to view the contents of a file. The syntax to use cat is quite simple, as shown below:

$ cat samplefile.txt
View Contents of File
View Contents of File

View File Contents By Line Numbers

The nl is another command in Linux which displays the contents of a file. It stands for ‘Number lines’, as the output displayed by this command is numbered lines.

$ nl samplefile.txt
Display File Contents By Line Numbers
Display File Contents By Line Numbers

The nl command has even more options to format the output numbering in various ways. However, to simply display the contents, it can be used as discussed above.

View File Contents Using More or Less Commands

The ‘more’ or ‘less’ are pretty much the same command in Linux. They display a file on the terminal one page at a time and are thus very useful in displaying large files.

For smaller files, ‘more’ works the same as the cat command, whereas ‘less’ works the same for both larger and smaller files.

$ more samplefile.txt
View Contents of File One Screen at Time
View Contents of File One Screen at Time
$ less samplefile.txt
View Contents of File One Page at Time
View Contents of File One Page at Time

The output of ‘less’ is displayed on an overlay screen, and can be exited by pressing ‘q’. Thus the output is not permanently written to the screen.

View File Contents Using head or tail Commands

The head command is used to print the first 10 lines of a file, instead of printing the whole file. Similar tail prints the last 10 lines of a file.

For example, the following respectively print the first and last 10 lines of the huge logfile ‘/var/log/syslog‘.

$ head /var/log/syslog
Print Beginning of File Contents
Print Beginning of File Contents
$ tail samplefile.txt
Print End of File Contents
Print End of File Contents

If you need to print a different number of top or bottom lines of the file, use the argument ‘-n<no_of_lines>’.

$ head -n5 /var/log/syslog
$ tail -n5 /var/log/syslog
Conclusion

Today we learned about various ways to display the contents of a file in the Linux command line. If you know any other cool way to display file contents in the command line, do share it with everyone in the comments below!

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.