Generic Top

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE

1. Introduction

Linux provides a number of commands for viewing files. In this tutorial, we'll look at the most commonly used cat, more and less commands.

2. The cat command

The cat command is the simplest way to view the contents of a file. It displays the contents of the file(s) specified on to the output terminal.

Let's look at an example:

cat a.txt

This will print the contents of the file a.txt:

A sample
file
to be used
for
cat command
examples

Sometimes, we might want to number the lines in the output.

We can do this by using the -n option:

cat -n a.txt

This will number each line in the output:

     1  A sample
     2  file
     3  to be used
     4  for
     5  cat command
     6  examples

Note that the line numbering starts from 1.

Let's look at a few other significant options:

  • -e     displays control and non-printing characters followed by a $ symbol at the end of each line
  • -t      each tab will display as ^I and each form feed will display as ^L
  • -v     displays control and non-printing characters

3. The more command

The cat command is all well and good for small files. But, if the file is large, the contents will zoom past and we'll only see the last screen worth of content.

One way to overcome this is by using the more command.

The more command displays the contents of the file one screen at a time for large files. If the contents of the file fit a single screen, the output will be the same as the cat command.

Let's use this command on a pom.xml file:

Note the text “–More–(46%)” at the end of the output. Here, the text “(46%)” tells us that the file is big and we're currently seeing only 46% of the content. This percentage will increase as we traverse through the file.

The cursor will stay at the end of this text. Then, we can scroll through the contents of the file using the Enter key, one line at a time.

We can also scroll through the file page by page by using the Space bar. And to scroll back to the previous page, we can use the b key. We'll use the q key to go back to the command prompt.

The more command can also be used to view multiple files. We just have to list each of them one after another:

more pom.xml a.txt b.txt

Later, we'll see how to move between these files.

Along with files, we can also pipe the more command with the output of other commands:

ls -l | more

It's important to note that more allows backward movement only with files, not with pipes.

3.1. Commands

As well as the keys used above, we can use a few other commands while viewing the file.

Let's look at a few important ones:

  • [k]/<text>   searches for the kth match of the regular expression <text>
  • !<cmd>       executes <cmd> in a subshell
  • [k]:n             goes to the kth next file
  • [k]:p             goes to the kth previous file
  • [k]z              displays the next k lines of text. If we don't specify k, it defaults to the current page size
  • :f                  displays the current file name and line number
  • =                  displays the current line number

Also, we can use h or ? at any point to list all the commands that can be used with more.

The more command also allows us to specify various options on the command line to customize the output. Let's look at a few of these.

3.2. Alter Page Size

Let's suppose we want to view only a certain number of lines at a time. We can do this by specifying the number of lines as an option:

more -5 pom.xml

This will display the first 5 lines of the file instead of a screen worth of content.

Subsequently, when we use the Space bar, the next 5 lines will be shown. Similarly, the b key will show the previous 5 lines.

3.3. Specify Start of Content

We can also specify the line number in the file from where we want to start viewing the content:

more +10 pom.xml

This will cause the output to start from the 10th line.

3.4. Start From the First Occurrence of a Text

It's possible to search for a particular text in the file and start viewing the file from that point:

more +/slf4j pom.xml

The above command will output the file contents from the first occurrence of the text “slf4j” in the file.

Apart from the above, the more command provides a few other options. Let's briefly look at these:

  • -d  this option is used to help the user navigate; it'll prompt the user with the message “[Press space to continue, ‘q' to quit.]”; it'll also display “[Press ‘h' for instructions.]” when an illegal key is pressed
  • -l   the more command usually treats ^L (form feed) as a special character and will pause after any line that contains a form feed; the -l option will prevent this behavior
  • -f   this option stops the wrapping of long lines
  • -p  clears the screen and then displays the text
  • -c  displays the pages on the same area by overlapping the previously displayed text
  • -u  suppresses underlining
  • -s  squeezes multiple blank lines into one

4. The less command 

Now, let's move to the less command. The less command is similar to the more command but provides extensive features. One important one is that it allows backward as well as forward movement in the file, even with pipes.

Also, since it does not read the entire file before starting, it starts up faster compared to text editors — especially when we're viewing large files.

To view our pom.xml file, we'll simply replace more with less:

less pom.xml

This should show us the first page of the file with a prompt at the end:

Note how the file name is displayed at the prompt.

Unlike more, if the file content fits the screen, less will still display the prompt. To override this we have to specify the -F option.

Like cat, it's possible to number the lines in the file. For this, we've to specify the -N or –LINE-NUMBERS option.

Just like more, we can execute a number of commands while viewing the contents. There are too many to list here, so let's look at the most common ones.

4.1. Moving Around the File

The keys SpaceEnterb, and q work the same way as with more. Apart from this, we can use the arrow keys to move horizontally and vertically. As an alternative to the up arrow, we can use j to move one line forward. Similarly, k can be used to move one line backward.

We can prefix the above keys with a number to override the default movement. For example, 5 followed by j will take us 5 lines forward.

While scrolling through large files, it's useful to be able to go back to the start of the file, or to the end of the file, quickly. The g key will take us to the start of the file, and the G key will take us to the end of the file.

4.2. Searching for Text

The less command's especially useful for viewing large log files. And most times, the reason we're viewing a log file is to search for an error or look for a log statement.

So, to search for a particular text, we'll use /<pattern>. Here, <pattern> is the text we are searching for and can be a regular expression.

Subsequently, we can use the n key to move to the next occurrence of the pattern, and the N key to move to the previous occurrence of the pattern.

This command will search for the pattern forward in the file. But, sometimes we might want to go to the end of the file and search backward for the latest occurrence of the text. The G key will take us to the end of the file. Then, we can use ?<pattern> to search for the pattern backward in the file.

Now, it's also possible to view only the lines in the file that match the pattern. This way, we'll have less text to go through. We can do this by using &<pattern>.

4.3. Monitoring the File

One interesting feature that less provides is the ability to monitor files. So, every time the content of the file changes, we'll be able to view the changes.

We can achieve this by using the F key. It'll take us forward and keep trying to read when the end of file is reached. This behavior is similar to the “tail -f” command.

5. Conclusion

In this article, we saw how to view files using the catmore, and less commands. As for less, we only covered the most commonly used features. But, less provides a lot more.

We can use “less -h” or the h key, while in the viewer, to list all the features provided.

Generic bottom

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE

Leave a Reply

avatar
  Subscribe  
Notify of