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
Syntax: cat [Option] [File_Name]
How to Use cat Command on Linux
New users often use this command to read a file, which is good, but not efficient.
# cat file-demo.txt This is a test file to experiment the "File View" command. GNU Linux Debian Ubuntu Arch Linux Linux Mint CentOS Red Hat Fedora openSUSE Manjaro elementary MX Linux Solus Deepin
Add the -n
option to the cat command to print the line number in the cat command output.
# cat -n file-demo.txt 1 This is a test file to experiment the "File View" command. 2 GNU Linux 3 Debian 4 Ubuntu 5 Arch Linux 6 Linux Mint 7 CentOS 8 Red Hat 9 Fedora 10 openSUSE 11 Manjaro 12 elementary 13 MX Linux 14 Solus 15 Deepin
Use the below cat command format to read multiple files at once.
# cat file-demo.txt file-demo-1.txt
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.
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
Syntax: less [Option] [File_Name]
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.
# less file-demo.txt This is a test file to experiment the "File View" command. GNU Linux Debian Ubuntu Arch Linux Linux Mint CentOS Red Hat Fedora openSUSE Manjaro elementary MX Linux Solus Deepin (END)
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
Syntax: more [Option] [File_Name]
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.
# more file-demo.txt This is a test file to experiment the "File View" command. GNU Linux Debian Ubuntu Arch Linux Linux Mint CentOS Red Hat Fedora openSUSE Manjaro elementary MX Linux Solus Deepin --More--(18%)
4) What is head Command
The head command displays the beginning of a file (first part of the file).
Common Syntax for head Command
Syntax: head [Option] [File_Name]
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.
# head file-demo.txt This is a test file to experiment the "File View" command. GNU Linux Debian Ubuntu Arch Linux Linux Mint CentOS Red Hat Fedora openSUSE
5) What is tail Command
The tail command displays the last part of a file.
Common Syntax for tail Command
Syntax: tail [Option] [File_Name]
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.
# tail file-demo.txt Linux Mint CentOS Red Hat Fedora openSUSE Manjaro elementary MX Linux Solus Deepin
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
Syntax: nl [Option] [File_Name]
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.
# nl file-demo.txt 1 This is a test file to experiment the "File View" command. 2 GNU Linux 3 Debian 4 Ubuntu 5 Arch Linux 6 Linux Mint 7 CentOS 8 Red Hat 9 Fedora 10 openSUSE 11 Manjaro 12 elementary 13 MX Linux 14 Solus 15 Deepin
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
Syntax: od [Option] [File_Name]
How to Use od Command on Linux
# od file-demo.txt 0000000 064124 071551 064440 020163 020141 062564 072163 063040 0000020 066151 020145 067564 062440 070170 071145 066551 067145 0000040 020164 064164 020145 043042 066151 020145 064526 073545 0000060 020042 067543 066555 067141 027144 043412 052516 046040 0000100 067151 074165 042012 061145 060551 005156 061125 067165 0000120 072564 040412 061562 020150 064514 072556 005170 064514 0000140 072556 020170 064515 072156 041412 067145 047564 005123 0000160 062522 020144 060510 005164 062506 067544 060562 067412 0000200 062560 051556 051525 005105 060515 065156 071141 005157 0000220 066145 066545 067145 060564 074562 046412 020130 064514 0000240 072556 005170 067523 072554 005163 062504 070145 067151 0000260 000012 0000261
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
Syntax: xxd [Option] [File_Name]
How to Use xxd Command on Linux
# xxd file-demo.txt 00000000: 5468 6973 2069 7320 6120 7465 7374 2066 This is a test f 00000010: 696c 6520 746f 2065 7870 6572 696d 656e ile to experimen 00000020: 7420 7468 6520 2246 696c 6520 5669 6577 t the "File View 00000030: 2220 636f 6d6d 616e 642e 0a47 4e55 204c " command..GNU L 00000040: 696e 7578 0a44 6562 6961 6e0a 5562 756e inux.Debian.Ubun 00000050: 7475 0a41 7263 6820 4c69 6e75 780a 4c69 tu.Arch Linux.Li 00000060: 6e75 7820 4d69 6e74 0a43 656e 744f 530a nux Mint.CentOS. 00000070: 5265 6420 4861 740a 4665 646f 7261 0a6f Red Hat.Fedora.o 00000080: 7065 6e53 5553 450a 4d61 6e6a 6172 6f0a penSUSE.Manjaro. 00000090: 656c 656d 656e 7461 7279 0a4d 5820 4c69 elementary.MX Li 000000a0: 6e75 780a 536f 6c75 730a 4465 6570 696e nux.Solus.Deepin 000000b0: 0a .
9) What is gv Command
The gv command is used to view the Postscript and PDF files.
Common Syntax for gv Command
Syntax: gv [Option] [File_Name]
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
Syntax: xdvi [Option] [File_Name]