View Text Files in Linux Shell

Before you start

Objectives: Learn about specific commands which can be used to show the content of text files in Linux.

Prerequisites: you have to know basic commands used to navigate Linux file system.

Key terms: command, file, text, pager, redirector, files, show, content, echo, output, page, screen


CAT Command

To show the content of the text file we can use the “cat” command. Originally, this command was used to concatenate two files, but if we only specify one file in the command, it will show us the content of that file. In our example, first we have used the “pwd” command to see where we are in the file system, then we have changed directory to Documents by using the “cd Documents” command, then we have listed all files in that directory by using the “ls” command, and then we have used the “cat SampleTextFile” command to show the content of the “SampleTextFile” file. The output is the “Sample Text” and one empty row.

 cat Command

cat Command

If we use a second filename parameter with the “cat” command, we will concatenate two files and display the result on the screen.

We can also add our own text to the file by using the “echo” command. To do that we have to specify the string that we want to add, and use the “>>” operator (double redirector) to specify the file to which we will add our text. In our example we will add “Sample text added” string to our “SampleTextFile” file.

echo Command

echo Command

Notice that the output of the file has changed.

Redirectors

Redirectors are used to take the output of some command and input that into some file. There are several types of redirectors. We already used the Double redirector (>>) together with the “echo” command in our example above. The whole command was “echo “Sample text added” >> SampleTextFile“. The double redirector simply appends the data to the end of the existing file. If the file doesn’t exist, it will create a new file.

The second type of redirector is the Single redirector (>). The single redirector will always overwrite the contents of the file. For example, let’s use the “echo “New text added” > SampleTextFile” command and check the output of the file.

 Single Redirector

Single Redirector

Notice that the whole content was overwritten with the “Sample text added” string.

Another type of the redirector is the “<” redirector. It can be used to define a file which will serve as a standard input for some command. For example, in the command “cat < /home/cicnavi/Documents/SampleTextFile” we have defined that the “SampleTextFile” will act as input for the cat command.

Another type of redirector is the Pipe redirector ( | ). Pipes are used to redirect the output of one command and use it as an input for the next command. The pipe redirector is often used to show long lists or long text on the terminal one screen at the time. To do that we can use the pager operator.

Pagers

We have two types of pagers. The first one is called “more”. The pager will show things that are too long for one terminal screen, one page at the time. For example, we can use the “ls -l” command to list all things in one directory and pipe it in to the pager operator “more”. The command looks like this: “ls -l | more“. In our case we have moved to the /bin directory and used the mentioned command.

More Operator

more Pager

To move to the next screen we can simply hit the space key. We can also use this same principle with text files. For example, if we have long text file, we can use the command “cat filename | more“.

The second type of pager is called “less”. This is the updated “more” pager. “Less” pager allows us to page down and up. With “more” pager we can only page down. To move between screens with the “less” pager, we can use the Page Up and Page Down keys. We can also use arrows to move one line at the time. When we use the “less” pager we will see a colon at the bottom of the screen.

Less Pager

less Pager

To exit the pager we can simply hit the “Q” key.