How to use cat, more, head, and tail commands to view the contents of Linux files, not just text files.

Linux provides many commands to view the contents of files, including cat, more, head, and tail, but this is just the beginning.

For one thing, even the most obvious commands have many, many options that users will never use. There are also run-of-the-mill commands that provide unique functionality. In this article, we’ll look at commands to view the contents of files and options for how to customize these views to better meet your needs.

cat

The cat command sends the entire contents of the text file to the terminal window for viewing. In fact, if you type cat and then a file name with thousands of lines of content, those lines will scroll through your window so fast that you won’t be able to see any text except the last screen. The cat command is familiar to Linux users, but even this basic command provides a number of useful options, such as numbering lines in the output, that many of us have probably never used. Taking this one step further, you can not only number the rows, but also choose how to number them.

Number each row like this:

$ cat -n msg
     1  Hello --
     2
     3  I hope you are having a wonderful day!
     4
     5
     6  That's it for ... now 7 8 bye! 9 10 s.Copy the code

You can also number only the lines that have content. Note that for this command, lines that contain only Spaces are not considered “empty” but are numbered.

$ cat -b msg
     1  Hello --

     2  I hope you are having a wonderful day!


     3  That's it for ... now 4 bye! 5 s.Copy the code

The cat command allows you to ignore repeated blank lines using the -s option, but to ignore blank lines completely you must add another command.

$ cat -s msg
Hello --

I hope you are having a wonderful day!

That's it for ... now bye! s.Copy the code

To ignore all blank lines, simply pipe the output of cat to the grep command as follows. Dot (.) Matches text that contains any character, so it will display any non-empty line that is used to match the carriage return newline that ends the line.

$ cat msg | grep .
Hello --
I hope you are having a wonderful day!
That's it for ... now bye! s.Copy the code

The -e option provides a visual indication of whether there is extra space at the end of each line by adding the $character.

$ cat -E msg
Hello --$
$
I hope you are having a wonderful day!  $
$
$
That's it for ... now$ $ bye! $ $ s.$Copy the code

With -a, you can display both the $character at the end of each line and the TAB as ^I instead of blank.

$ cat -A msg Hello --$ $ I hope you are having a wonderful day! $$$That's itfor. ^Inow$ $bye! $ $ s.$Copy the code

Use head and tail to display portions of the file

Head and tail Display the header or tail of the file, which is 10 lines by default. You can specify the number of additional lines to view using strings like -3 (to display 3 lines) or -11 (to display 11 lines). The tail command works the same way as head, but displays the tail of the file instead of the head.

$ head -3 msg
Hello --
I hope you are having a wonderful day!
$ tail -3 msg
bye!

s.
Copy the code

You can also use a combination of the head and tail commands to view the text in the middle of a file. You simply select the starting point and the number of rows you want to view. In this case, the command displays the second hundred lines in the file and numbers them with the help of CAT.

$ cat -b mybigfile | head -200 | tail -100
   101  Invoice #2020-06-07a sent to vendor.Copy the code

Browse a screen of text using more or less

The more command is a natural choice for browsing one screen of content at a time, while less adds the ability to move up and down through a file by using up and down keyboard arrows, so you can walk through the content and then back up within the file.

Two ways to view text using OD

The OD (octal dump) command views a file as regular text and a series of ASCII values (that is, how the text is actually encoded in the file). As you can see in the example below, the numbered lines display ASCII numeric values, while the other lines display text and non-printable characters.

$ od -bc msg
0000000 110 145 154 154 157 040 055 055 012 012 111 040 150 157 160 145
          H   e   l   l   o       -   -  \n  \n   I       h   o   p   e
0000020 040 171 157 165 040 141 162 145 040 150 141 166 151 156 147 040
              y   o   u       a   r   e       h   a   v   i   n   g
0000040 141 040 167 157 156 144 145 162 146 165 154 040 144 141 171 041
          a       w   o   n   d   e   r   f   u   l       d   a   y   !
0000060 012 012 012 124 150 141 164 047 163 040 151 164 040 146 157 162
         \n  \n  \n   T   h   a   t   ' s i t f o r 0000100 040 056 056 056 011 156 157 167 012 012 142 171 145 041 012 012 . . . \t n o w \n \n b y e ! \n \n 0000120 163 056 012 s . \nCopy the code

Note that the newline character is displayed as \n (octal 012) and the TAB character as \t (octal 011).

One of the particularly useful uses of the OD command is to view non-text files for information that identifies the file type. Here, we see the JFIF (JPEG File Interchange Format) tag, which allows commands like file to report file types to mark it as a JPG file. There’s plenty of other useful information here, especially if you’re curious about the format of these files.

In the following command, we look at the beginning of the JPG file.

$ od -bc arrow.jpg | head -12 0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 000 000 001 377 330 377 340 \0  020 J F I F \0 001 001 \0 \0 001 0000020 000 001 000 000 377 333 000 103 000 003 002 002 002 002 002 003 \0 001 \0 \0 377 333 \0 C \0 003 002 002 002 002 002 003 0000040 002 002 002 003 003 003 003 004 006 004 004 004 004 004 010 006 002 002 002 003 003 003 003 004 006 004 004 004 004 004 \b 006 0000060 006 005 006 011 010 012 012 011 010 011 011 012 014 017 014 012 006 005 006 \t \b \n \n \t \b \t \t \n \f 017 \f \n 0000100 013 016 013 011 011 015 021 015 016 017 020 020 021 020 012 014 \v 016 \v \t \t \r 021 \r 016 017 020 020 021 020 \n \f 0000120 022 023 022 020 023 017 020 020 020 377 333 000 103 001 003 003 022 023 022 020 023 017 020 020 020 377 333 \0 C 001 003 003Copy the code

If we wanted the file command to provide information about this image, we might see something like the following. The file command extracts all this descriptive information from the data at the beginning of the file:

$ file arrow.jpg
arrow.png: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 500x375, components 3
Copy the code

Use JP2A to treat files as text-based images

If you can only work from the command line and want to know what a particular image contains, you can use a tool like JP2A (JPEG to ASCII) to provide character rendering. How recognizable an image is in this format depends on the file. Don’t expect too much, because the version of the image you will see is tested in “low resolution”! This is an emperor penguin with a very low resolution. (Please look from a distance)

$ jp2a Emperor_Penguin.jpg
MMMMMMMMWOdkNMMMMMMMMMMMMMMMMMMM
MMMXK0kc.... ,OKMMMMMMMMMMMMMMMM
MMNK0Ol...   :Xx'dNMMMMMMMMMMMMM MMMMMMMd; lx00Oo. .. xMMMMMMMMMMMM MMMMMMK.OXMMMMMN,... lMMMMMMMMMMM MMMMMMx'KXNNMMMMK.... 0MMMMMMMMMM MMMMMMx:kkKNWWMMMl..'NMMMMMMMMM
MMMMMMddx0NNNWMMMK'. ; NMMMMMMMM MMMMMMck0NNWWWWWMMd .. lMMMMMMMM MMMMMM.d0KXNWWWWMMo ... WMMMMMMM MMMMMM.xOXNNWNMMMW. .... KMMMMMMM MMMMMM'kKNKWXWMMMK  ..'.0MMMMMMM MMMMMMxckXNNNNMMMX .:.. XMMMMMMM MMMMMMW; xKNWWWMMMM. .; . NMMMMMMM MMMMMMMok0NNWNWMMMx .l.. MMMMMMMM MMMMMMMkxOKXWXNMMMMl.:'dMMMMMMMM MMMMMMM0dKOdKXXNMMMMNx,WMMMMMMMM MMMMMMMWoKxldXKNNMMMMM; MMMMMMMMM MMMMMMMMxxxxdNWNXNMMMM; MMMMMMMMM MMMMMMMMxOcoo0XOOOOWMW,kMMMMMMMM MMMMMMM0xK; .cO0dNX:0XXd; NMMMMMMM MMMNkdd:,'ldXXO0xl; x0kx:; lKMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMCopy the code

conclusion

There are many commands on Linux to view the contents of files in various ways. Some of these options can be very useful if you need to work with file contents. The rest is just… Interesting.


Via: www.networkworld.com/article/356…

Author: Sandra henry-stocker lujun9972

This article is originally compiled by LCTT and released in Linux China