Linux view file contents

How to display the contents of a text file on the terminal in Linux/Ubuntu

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.

Using less

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

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

Using cat

applying cat to the same file that we used above, .i.e. :

Linux show file content

Using Echo

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

Using printf

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 .

Using tail

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.

Using head

Used to display the first few lines.

Applying head to the file output_in_html will give the following result :

Using more

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.

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

Conclusion :

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.

Читайте также:  Windows 10 иконки рабочего стола стали белыми

If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

Источник

5 Commands to View the Content of a File in Linux Command Line

If you are new to Linux and you are confined to a terminal, you might wonder how to view a file in the command line.

Reading a file in Linux terminal is not the same as opening file in Notepad. Since you are in the command line mode, you should use commands to read file in Linux.

Don’t worry. It’s not at all complicated to display a file in Linux. It’s easy as well essential that you learn how to read files in the line.

Here are five commands that let you view the content of a file in Linux terminal.

5 commands to view files in Linux

Before you how to view a file in Unix like systems, let me clarify that when I am referring to text files here. There are different tools and commands if you want to read binary files.

1. Cat

This is the simplest and perhaps the most popular command to view a file in Linux.

Cat simply prints the content of the file to standard display i.e. your screen. It cannot be simpler than this, can it?

cat displays the content of the file on the screen

Cat becomes a powerful command when used with its options. I recommend reading this detailed tutorial on using cat command.

The problem with cat command is that it displays the text on the screen. Imagine if you use cat command with a file that has 2000 lines. Your entire screen will be flooded with the 200 lines and that’s not the ideal situation.

So, what do you do in such a case? Use less command in Linux (explained later).

The nl command is almost like the cat command. The only difference is that it prepends line numbers while displaying the text in the terminal.

nl command displays text with line numbers

There are a few options with nl command that allows you to control the numbering. You can check its man page for more details.

3. Less

Less command views the file one page at a time. The best thing is that you exit less (by pressing q), there are no lines displayed on the screen. Your terminal remains clean and pristine.

I strongly recommend learning a few options of the Less command so that you can use it more effectively.

There is also more command which was used in olden days but less command has more friendly features. This is why you might come across the humorous term ‘less is more’.

4. Head

Head command is another way of viewing text file but with a slight difference. The head command displays the first 10 lines of a text file by default.

You can change this behavior by using options with head command but the fundamental principle remains the same: head command starts operating from the head (beginning) of the file.

5. Tail

Tail command in Linux is similar and yet opposite to the head command. While head command displays file from the beginning, the tail command displays file from the end.

Читайте также:  Snap camera mac os

By default, tail command displays the last 10 lines of a file.

Head and Tail commands can be combined to display selected lines from a file. You can also use tail command to see the changes made to a file in real time.

Bonus: Strings command

Okay! I promised to show only the commands for viewing text file. And this one deals with both text and binary files.

Strings command displays the readable text from a binary file.

No, it doesn’t convert binary files into text files. If the binary file consists of actual readable text, strings command displays those text on your screen. You can use the file command to find the type of a file in Linux.

Conclusion

Some Linux users use Vim to view the text file but I think that’s overkill. My favorite command to open a file in Linux is the less command. It leaves the screen clear and has several options that makes viewing text file a lot easier.

Since you now know ways to view files, maybe you would be interested in knowing how to edit text files in Linux. Cut and Paste are two such commands that you can use for editing text in Linux terminal. You may also read about creating files in Linux command line.

Источник

10 Methods to View Different File Formats in Linux

Viewing the contents of a file on Linux is one of the basic functions of a Linux administrator.

It enables users to view and manipulate file content.

There are many applications for displaying file contents in Linux in different ways.

You can use any of the following commands to view a text file or any other file.

You can choose the command based on your requirements.

If you want to monitor Linux log files in real-time, see the following article. Also, if you want to get a Color Man page on Linux, go to the following article.

To test this, I’ve created the file-demo.txt and file-demo-1.txt files, and added a definition and some contents to experiment all these commands.

Command Short Description
cat To display a file contents in screen.
less To display a file contents in screen page wise.
more To display a file contents in screen page wise.
head The head command displays the beginning of a file
tail To view the last “N” lines of a file
nl To print a file contents with line number
od To print a file contents as a octal dump format
xxd To print a file contents as a hex dump format
gv View Postscript/PDF files
xdvi View TeX DVI files

1) What is cat Command

The cat stands for (concatenate). It is one of the most frequently used commands on Unix-like operating systems. The most common use of cat is to read the contents of files.

Common Syntax for cat Command

How to Use cat Command on Linux

New users often use this command to read a file, which is good, but not efficient.

Add the -n option to the cat command to print the line number in the cat command output.

Use the below cat command format to read multiple files at once.

2) What is less Command

The less command allows you to quickly view file contents on the screen from top to bottom using the UP & DOWN arrow keys or the Page UP & Page Down button.

Читайте также:  Linux проверка изменения файла

Alternatively, press g to move the file up, and press G to move the file end, and if you want to move to the corresponding line, press g (when you use “g” does not appear on the screen) and continue to type the concern line number you want to jump. To exit the file (Esc, q, enter).

Common Syntax for less Command

How to Use less Command on Linux

The less command is a good option if you want to search any line with the format that matches in the file. If it is found it highlights the given pattern. Press CTRL+n to go to the next matching pattern, and press CTRL+p to come back to previous matching pattern.

To do so, run the following command, then press backslash (/) and enter the pattern to find it.

3) What is more Command

The more command allows you to quickly view the file contents on screen from top to bottom page wise.

Common Syntax for more Command

How to Use more Command on Linux

It shows pages with percentage. Also, allow users to find model matching. However, it does not highlight the pattern, and it does not find relevance on the current page.

4) What is head Command

The head command displays the beginning of a file (first part of the file).
Common Syntax for head Command

How to Use head Command on Linux

By default it will print the first 10 lines, and you can use the (N) option if you want to print additional lines.

5) What is tail Command

The tail command displays the last part of a file.
Common Syntax for tail Command

How to Use tail Command on Linux

By default it will print the last 10 lines, and you can use the (N) option if you want to print additional lines.

6) What is nl Command

The nl command is used to print a file contents on screen with line numbers, line numbers will start from “1” to “N”, this will print a file on screen in one go and it similar to the cat command.
Common Syntax for nl Command

How to Use nl Command on Linux

Enter a file name followed by the nl command to print the contents of a file with line number.

7) What is od Command

od stands for Octal Dump, it diplay the contents of a file in different formats with the octal format (binary format) being the default.
Common Syntax for od Command

How to Use od Command on Linux

8) What is xxd Command

The xxd command creates a hex dump of a given file. It can also convert a hex dump back to its original binary form.
Common Syntax for xxd Command

How to Use xxd Command on Linux

9) What is gv Command

The gv command is used to view the Postscript and PDF files.
Common Syntax for gv Command

10) What is xdvi Command

The xdvi command is a program for previewing dvi files. Xdvi can show the file shrunken by various integer factors, and it has a “magnifying glass” for viewing parts of the page enlarged
Common Syntax for xdvi Command

Источник

Оцените статью