Viewing Files in Linux

Viewing files from the command line

How to view a file from the shell


In the following examples, we will discover that there are many ways to view a file from within your terminal. Some we have already seen in action and others will be new to you.





Cat Command Examples


As we have seen from some of the other tutorials, the cat command can be used to view a single file and send its output to Standard Out (stdout). Simply by typing "cat filename" will normally allow you to view the contents of a file. However, there is more to the "cat" command. The name cat comes from the word "concatenation" meaning the joining of objects or files.

If we type "cat file1 file2", both of these files will be displayed to the screen in order. If we were to combine this command with a little redirection, we can create a third file comprising of the file1 and file2. To achieve this, we type "cat file1 file2 > file3".

Cat can also view files with special characters. You can even add numbers to the lines, condense numerous blank lines into a single line. Below are some examples of the "cat" command.

cat filename



john@john-desktop:~/test_examples$ cat textfile.txt
Linux Mint
Mageia
Ubuntu
Fedora
openSUSE
Debian
Arch Linux
PCLinuxOS
Zorin OS
CentOS

cat - number all lines


Line numbers are added to beginning of each line.


john@john-desktop:~/test_examples$ cat -n textfile.txt
     1	Linux Mint
     2	Mageia
     3	Ubuntu
     4	Fedora
     5	openSUSE
     6	Debian
     7	Arch Linux
     8	PCLinuxOS
     9	Zorin OS
    10	CentOS

cat multiple files



john@john-desktop:~/test_examples$ cat file1 file2
Line 1 of file1
Line 2 of file1
Line 3 of file1
Line 1 of file2
Line 2 of file2
Line 3 of file2

Show end of line marker ($)



john@john-desktop:~/test_examples$ cat -E file1
Line 1 of file1$
Line 2 of file1$
Line 3 of file1$

Show Tab characters - Tab shown as ^I



john@john-desktop:~/test_examples$ cat file3
Line 1 of file 3        Directly before me is a Tab
Line 2 of file 3        Directly before me is a Tab
Line 3 of file 3        Directly before me is a Tab

john@john-desktop:~/test_examples$ cat -T file3
Line 1 of file 3^IDirectly before me is a Tab
Line 2 of file 3^IDirectly before me is a Tab
Line 3 of file 3^IDirectly before me is a Tab

Show non printable characters - Tab shown as ^I, End of Line shown as a $



john@john-desktop:~/test_examples$ cat -vet file3
Line 1 of file 3^IDirectly before me is a Tab$
Line 2 of file 3^IDirectly before me is a Tab$
Line 3 of file 3^IDirectly before me is a Tab$


tac - Displaying files in reverse order


Sometimes you might want to display a file in reverse order. To achieve this you can use the cat command backwards "tac"


john@john-desktop:~/test_examples$ tac file1
Line 3 of file1
Line 2 of file1
Line 1 of file1

more


Sometimes you may need to use cat with a large file. If you type cat followed by this large file, the output will probably go whizzing past your screen. To get around this we can use cat in conjunction with the "more" command. To do this we simply modify our command to: "cat file1 | more". Now output is displayed one page at a time. If you press the "Space Bar" on your keyboard, this will then display the next screen of information. You can specify the "-d" option on the more command: cat largefile.txt | more -d. By adding the "-d" option, we are then prompted with "--More--[Press space to continue, 'q' to quit.]".


john@john-desktop:~/test_examples$ more file1
Line 1 of file1
Line 2 of file1
Line 3 of file1

less


That's right, less is the opposite of more! As you can gather, "less" is a program similar to the "more" program. The main difference is that the "less" program will allow you to move backwards and forwards through a file. Like most pager programs, "less" can receive output from a pipe command or you can open your file directly with the command. After the last line of the file has been displayed, you can press "q" to exit:


john@john-desktop:~/test_examples$ less file1
Line 1 of file1
Line 2 of file1
Line 3 of file1
file1 (END)


pg


pg is another program that allows you to view the contents of a file. Information is displayed one screen at a time. However, it is possible to open a file directly at a given page. To do this, simply pass the page number after the "+" sign.


john@john-desktop:~/test_examples$ pg +2 file1
Line 3 of file1
(EOF):

In the above example, the file is displayed immediately after line 2.

head and tail


Two extremely useful programs that can be used for displaying lines of information from a file are "head" and "tail". Basically "head" will display information from the beginning of the file and "tail" will display information from the end of the file.

What makes these two programs really useful is the ability to specify how many lines to be included within the output. By default both the "head" and "tail" program display the first 10 or last 10 lines respectively:


john@john-desktop:~/test_examples$ head concatfile2.txt
     1	Linux Mint
     2	Mageia
     3	Ubuntu
     4	Fedora
     5	openSUSE
     6	Debian
     7	Arch Linux
     8	PCLinuxOS
     9	Zorin OS
    10	CentOS
john@john-desktop:~/test_examples$ tail concatfile2.txt
    21	Linux Mint
    22	Mageia
    23	Ubuntu
    24	Fedora
    25	openSUSE
    26	Debian
    27	Arch Linux
    28	PCLinuxOS
    29	Zorin OS
    30	CentOS

To amend the number of lines displayed by either we can use the "-n" flag for number of lines displayed. In the example below we are going to issue the first 5 lines of a file and then the last lines of a file:


john@john-desktop:~/test_examples$ head -n 5 concatfile2.txt
     1	Linux Mint
     2	Mageia
     3	Ubuntu
     4	Fedora
     5	openSUSE
john@john-desktop:~/test_examples$ tail -n 5 concatfile2.txt
    26	Debian
    27	Arch Linux
    28	PCLinuxOS
    29	Zorin OS
    30	CentOS

Where these commands become really useful is if you wanted to view only the content in the middle of a file. In our example we only want to see output from lines 11 through to 20. To achieve this, we need to construct a combination of the "head" command in conjunction with the "tail" command:


john@john-desktop:~/test_examples$ tail -n 20 concatfile2.txt | head -n 10
    11	Linux Mint
    12	Mageia
    13	Ubuntu
    14	Fedora
    15	openSUSE
    16	Debian
    17	Arch Linux
    18	PCLinuxOS
    19	Zorin OS
    20	CentOS

In the above example the "tail" command found the last 20 lines of output. These lines were then passed to the "head" command where we specified only the top 10 lines of output please!. The result, lines eleven through to twenty are displayed.