How to examine files on Linux

$ ls -ld TOPs
-rw-r--r--. 1 jdoe jdoe 9571 Oct 18 12:58 TOPs
$ ls -ldh TOPs
-rw-r--r--. 1 jdoe jdoe 9.4K Oct 18 12:58 TOPs

Viewing file content

The cat, head, tail, more and less commands allow you to view the content of text files. The cat command displays the entire file while head and tail display the first and last lines (defaulting to 10). The commands below display only three lines.

$ head -3 TZs
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
$ tail -3 TZs
W-SU
WET
Zulu

The more command will show you a screenful of lines at a time or fewer if you add an option (e.g., more -10). Both the more and less commands allow you to back up while viewing file content, but less backs up a line at a time while more backs up a screen at a time.

Using the nl command

If you want to view the lines in a text file in a numbered format, you can use the nl command. Here’s an example of using nl along with head to see only the first three lines of the output.

$ nl TZs | head -3
1 Africa/Abidjan
2 Africa/Accra
3 Africa/Addis_Ababa

Viewing other types of files

To view the content of files other than text files while working on the command line, you can use the od -bc command, but it’s going to show you the content in the octal and character (where available) formats shown below. The “E L F” in the output below is short for “executable and linkable format”. This format is used for storing binaries, libraries, core dumps and such.

$ od -bc /usr/bin/more | head -4
0000000 177 105 114 106 002 001 001 000 000 000 000 000 000 000 000 000
177 E L F 002 001 001
0000020 003 000 076 000 001 000 000 000 040 070 000 000 000 000 000 000
003 > 001 8

The JFIF sequence in the example below identifies the file as being in jpeg format.

$ od -bc hummingbird.jpg | head -6
0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 001 000 110
377 330 377 340 020 J F I F 001 001 001 H
0000020 000 110 000 000 377 341 032 130 105 170 151 146 000 000 111 111
H 377 341 032 X E x i f I I
0000040 052 000 010 000 000 000 005 000 032 001 005 000 001 000 000 000
* b 005 032 001 005 001

In the command below, output from the echo command is piped to the od -bc command to provide an easy example of how this command works. It shows the letters in “hello” followed by a newline character.



Source link