4.1 Reading Text Files

Since Linux is basically a large collection of text files. Reading and manipulating them is an important skill for managing your Linux system. We will go through the most used tools to read text files.

more

The more command is used to view the text files in the command prompt, displaying one screen at a time in case the file is large (for example log files). The more command also allows the user to scroll up and down through the page. The syntax, along with options and command, is as follows:

more [-options] [-num] [+/pattern] [+linenum] [file_name]
        [-options]: any option that you want to use in order to change the way the file is displayed. Choose any one from the followings: (-d, -l, -f, -p, -c, -s, -u)
        [-num]: type the number of lines that you want to display per screen.
        [+/pattern]: replace the pattern with any string that you want to find in the text file.
        [+linenum]: use the line number from where you want to start displaying the text content.
        [file_name]: name of the file containing the text that you want to display on the screen.

Another application of more is to use it with some other command after a pipe. When the output is large, we can use the more command to see output one by one.

Basic Controls:

  • Enter key: to scroll down line by line.

  • Space bar: to go to the next page.

  • b key: to go to back one page.

Let’s try to use the more command a bit,

more /media/onthegenealogyofmorality

You should now see the contents of the file.

Press space to go down.

Press b to go up.

Press enter to scroll line by line.

Press q to quit.

less

The less command is linux utility, which can be used to read contents of text file one page(one screen) per time. It has faster access, because if the file is large, it doesn’t access the complete file, but it will only access the file page by page.

For example, if it’s a large file and you are reading it using any text editor, then the complete file will be loaded to main memory. The less command doesn’t load the entire file, but it will load the file bit by bit, which makes it faster.

The less help is extensive to say the least, you can check the less help yourself by typing.
less --help

The most used switches are:

-E : causes less to automatically exit the first time it reaches end of file
-f : forces non-regular file to open.
-F : causes less to exit if entire file can be displayed on first screen
-g : highlight the string which was found by last search command
-G : suppresses all highlighting of strings found by search commands
-i : cause searches to ignore case
-n : suppresses line numbers
-p pattern : it tells less to start at the first occurrence of pattern in the file
-s : causes consecutive blank lines to be squeezed into a single blank line. 

Let’s try the less command a bit.

less /usr/share/vim/vim80/doc/message.txt

You can navigate the document now with the following buttons.


    / – search for a pattern which will take you to the next occurrence.
    n – for next match in forward
    N – for previous match in backward

    ? – search for a pattern which will take you to the previous occurrence.
    n – for next match in backward direction
    N – for previous match in forward direction

    CTRL+F – forward one window
    CTRL+B – backward one window
    CTRL+D – forward half window
    CTRL+U – backward half window

    j – navigate forward by one line
    k – navigate backward by one line
    G – go to the end of file
    g – go to the start of file
    q or ZZ – exit the less pager

cat

Cat, other then an animal means, concatenate files and print on the standard output. We have used this command before; it should explain itself.

It takes a file and puts it in stdout[1]. After this, you can do manipulations like:

cat /var/log/secure | grep rhcsa. With this you can check the log’s when the user rhcsa has logged in. We will also see failed login attempts in the /var/log/secure.

tail

Shows the bottom of the file: tail /media/onthegenealogyofmorality

Now let’s say we only want the last 2 lines: tail -n 2 /media/onthegenealogyofmorality

nl

cat, but with line numbers: nl /media/onthegenealogyofmorality

You can try this command to check it out.