In this short tutorial, we cover see some simple commands that will help users learn how to open a file in linux or view its contents on the command line.
This is a pager which is useful to view long files for instance:
For instance the command below will display the content of the file output_report.txt :
Linux view text file
Read: How to exit Vim editor
By passing the -F and -X switches, less can behave like cat when applied to small files but will act normally otherwise.
For instance :
less -X new_file
applying cat to the same file that we used above, .i.e. :
Linux show file content
The parenthesis ( ) are used to return the value of a given expression which in this case, is the contents of file_name .
Linux display file contents
printf “%s” “$(<filename)”
Much like the previous command, the parenthesis ( ) are used to return the value of a given expression which in this case, is the contents of file_name .
printf “%s” “$(<new_file)”
To display the last few lines
applying tail to the file output_in_html will give the following result :
which displays the last lines of the provided file.
Used to display the first few lines.
Applying head to the file output_in_html will give the following result :
The more command enables you to display the contents of text files in a scrollable manner. The text is shown one screenful at a time. You also have the possibility to scroll forwards and backwards through the text. You can even perform a search on the text.
For example :
Linux command to display contents of a file
Will show the following :
- Space key : Used to scroll the display, .i.e. one screenful at a time
- Enter key : Used to scroll the display one line
- b key : Used to scroll the display backwards one screenful at a time
- / key : Used to search the a given pattern much like in the vi/vim editor
You have seen some simple utilities that allow you to display a text file on the command line. For small files, you can use the less command, or the echo command. If you know other similar tools, do not hesitate to write them in the comments section below.
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.