Computer scienceSystem administration and DevOpsCommand lineWorking with files

Viewing files in shell

6 minutes read

A sufficient part of programming is usually spent working with files. Unix has many options to make it easier. Viewing files directly from the terminal is one of them, and Unix has a number of utilities for it. We will discuss some of them below.

How to use cat utility

The first utility is called cat utility. Cat (a shortening of the word concatenate) can output specified files sequentially, concatenating them into a single stream. To get started, you need to open a terminal from the corresponding directory with files and use cat. Imagine we have two files unix_cat.txt and ascii_cat.txt in the working directory. To view the contents of unix_cat.txt, we need to write cat unix_cat.txt and the result will be as follows:

$ cat unix_cat.txt

Unix cat, Unix cat, where have you been?
I've been in the shell, that's my daily routine.

Unix cat, Unix cat, what's more to tale?
I read all the files from head to tail!

To display the contents of multiple files, we just need to list their names after cat like cat unix_cat.txt ascii_cat.txt:

$ cat unix_cat.txt ascii_cat.txt

Unix cat, Unix cat, where have you been?
I've been in the shell, that's my daily routine.

Unix cat, Unix cat, what's more to tale?
I read all the files from head to tail! 

       \`*-.
        )  _`-.
       .  : `. .
       : _   '  \
       ; *` _.   `*-._
       `-.-'          `-.
         ;       `       `.
         :.       .        \
         . \  .   :   .-'   .
         '  `+.;  ;  '      :
         :  '  |    ;       ;-.
         ; '   : :`-:     _.`* ;
      .*' /  .*' ; .*`- +'  `*'
      `*-*   `*-*  `*-*'

So, the cat can read, what else? There is a whole list of additional options that are set with a dash and a letter. We will show one of them, -n, which numbers all the lines in the file. The command syntax looks like this: cat -n filename. As a result, we get all the lines in the file with their corresponding numbers from 1 to n:

$ cat -n unix_cat.txt

1  Unix cat, Unix cat, where have you been?
2  I have been in the shell, that's my daily routine.
3
4  Unix cat, Unix cat, what's more to tale?
5  I read all the files from head to tail!    

Note, that -n enumerates all the lines, even the empty ones. If you want to count only non-empty lines, call -b instead of -n.

Okay, we've learned how to open and read files using cat utility, as well as to number the lines. What else do we have in the arsenal?

Head and tail

There is a pair of commands, both of them open files for reading. They do it either from the beginning of a file or from the end, respectively. Also, in the case of large files, they can be read in parts. That is, the head file.txt command, for example, will read file.txt from the beginning. By default, it will read the first ten lines. If there are less than 10 lines, it will display all of them.

$ head ascii_cat.txt

       \`*-.
        )  _`-.
       .  : `. .
       : _   '  \
       ; *` _.   `*-._
       `-.-'          `-.
         ;       `       `.
         :.       .        \
         . \  .   :   .-'   .
         '  `+.;  ;  '      :

The head command returns 10 first lines in the file, including empty ones. The tail file.txt command does a similar thing, only it will print the last 10 lines. If you need a different number of lines, for example, only the first 6, you can use the -n option and ask it to count exactly to 6, that is, -n6. All together it will look like this head -n6 file.txt:

$ head -n6 ascii_cat.txt

       \`*-.
        )  _`-.
       .  : `. .
       : _   '  \
       ; *` _.   `*-._
       `-.-'          `-.

It is also possible to shorten the command a little and write it as head -6 file.txt, and the result will be the same. Another command, head -c 8 file.txt, returns the first 8 characters:

$ head -c 8 unix_cat.txt

Unix cat

The number of characters can be changed.

You can also change a number of lines in tail utility, i.e. tail -4 ascii_cat.txt will show only the last four lines:

$ tail -4 ascii_cat.txt

         :  '  |    ;       ;-.
         ; '   : :`-:     _.`* ;
      .*' /  .*' ; .*`- +'  `*'
      `*-*   `*-*  `*-*'

If you enter a number larger than the total number of lines, you will get all the lines. Additionally, tail -f and tail --follow display the last lines in the file and any new lines that are written to it. This is particularly useful for monitoring real-time activity, such as logs.

Cool, we've also learned how to read files from the beginning and from the end! However, that's still not all there is to learn. Until now we've only worked with small files but what if we have a large file? We may want to read it page by page, flipping through it back and forth. The utilities described above won't be very convenient for us in this case. Gladly, there is the less command. Let's talk about it in more detail.

Less utility

The less command allows to scroll a text not only forward but also backwards, search in both directions, and go right to the end or to the beginning of the file. Imagine you want to read a story about a penguin named Tux, who loves to eat and looks for fish every day. The less Tux.txt opens the file and then the command line disappears so that you see only the document you wanted to read in a terminal window.

Little penguin Tux is very fond of eating.
He already weighs like a real big penguin.
His path lies through a winter forest and an ice pass.
The road may seem very difficult, but Tux is used to it.

Tux.txt (ENG)

Then you can read it by using the Enter and cursor keys or <Page-Up> and <Page-Down> to scroll the text forward and backward, just like in a regular text editor. You can also search with /<some_word> like this: type /penguin to find all matches with the word penguin in the file. You will get the following result:

Little penguin Tux is very fond of eating.
He already weighs like a real big penguin.
His path lies through a winter forest and an ice pass.
The road may seem very difficult, but Tux is used to it.

Note that the search is case-sensitive. You can navigate between matches with the n and N keys in the English keyboard layout. When you finish, press q to exit. Please remember that with less utility you can only read documents, not edit them.

Conclusion

So, you have mastered key utilities for working with files: cat and less. Now you know how to open and read files in the terminal. You can also search for words and number lines in the files. These utilities are not the only ones, there exists plenty more. For those who wish, you can consider a more colorful analog called bat.

120 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo