Guide to Unix/Commands/File Viewing

cat displays the contents of a file to screen. It can also display multiple files concatenated together, and using the shell, its standard output can be redirected from the screen to the file.

Some useful options are:

-n  line numbering
-s  squeeze sequences of blank lines into one
-v  show non-printing characters

Examples

$ cat example.txt
The contents of the file
example.txt are now 
displayed.
$ cat -n example.txt
     1  The contents of the file
     2  example.txt are now
     3  displayed.

Concatenating multiple files (in this example, the same file twice):

$ cat example.txt example.txt
The contents of the file
example.txt are now
displayed.
The contents of the file
example.txt are now
displayed.

Concatenate the files, but use the shell to redirect the result to a new file.

$ cat example.txt example.txt > double.txt

We can concatenate binary files, too. Some programs limit downloads to 2 GB; this is bad for larger files like some DVD images. Suppose a web site helpfully divides the file "sodalinux.dvd" into three parts, 2 GB or less, for downloading and later concatentation. We combine the files, and use the > shell redirection output to put the DVD image in a file:

$ cat sodalinux.dvd1 sodalinux.dvd2 sodalinux.dvd3 > sodalinux.dvd

If we want to type less, then most shells also allow this:

$ cat sodalinux.dvd{1,2,3} > sodalinux.dvd

The -v option is useful for viewing control characters embedded mostly in text. In this example, the file "/usr/share/man/cat1/pax.0" is mostly text but contains control characters which the pagers "less" and "more" use to make text bold. Using cat -v we can see the control characters. Here are the first four lines:

$ cat -v /usr/share/man/cat1/pax.0 | head -4
PAX(1)                     OpenBSD Reference Manual                     PAX(1)

N^HNA^HAM^HME^HE
     p^Hpa^Hax^Hx - read and write file archives and copy directory hierarchies

Using "cat" with no arguments makes it copy standard input to standard output. Combined with shell redirection, this makes it easy to write a very short text file. All one needs to know is to press Control-D (^D) to indicate end of input, finish the file, and return to the shell. Here is how to write "example.txt":

$ cat > example.txt
The contents of the file
example.txt are now
displayed.
^D $

If you put "cat" with no arguments in a pipe, it only copies standard input to standard output. This might seem useless. For example, the following two pipes have the same function:

$ dmesg | less
$ dmesg | cat | less

However, "cat" can be used as insulation to make programs think that they are not running on terminals. In the next example, GNU bc does not print its copyright message on startup. We enter one calculation ("3 + 9") and then quit (^D):

$ bc | cat
3 + 9
12
^D

Links:

tac (cat spelled backwards) works like cat, but reverse the order of the lines (last line is written out first). It does not seem to be part of traditional Unix. When multiple files are given, they are printed out in the order they appear.

$ tac foo
Third Line
Second Line
First Line

Links:

more

edit

more paginates output. The problem with "cat" is that if a file is too long, then it falls beyond the top of the screen. The job of "more" is to stop and wait when it fills the screen. Most users find it easier to use "less", but on some systems "more" has all of the features of "less".

Keys:

  • return read next line
  • space bar read next screen
  • q quit

Examples: The pager will act like "cat" if the file is short enough.

 $ more hello.txt
 Hello World

Links:

less

edit

less paginates output. The program is called "less" because of the joke that "less is more", "less" actually has several features which "more" lacks.

Keys:

  • h read help. You might forget the other commands, but remember this one!
  • j go down one line. The down-arrow key might also work.
  • k go up one line. The up-arrow key might also work.
  • d go down one-half screen.
  • u go up one-half screen.
  • f go forward one screen.
  • b go back one screen.
  • p return to the top of the file.
  • q quit the pager.

Number arguments:

  • 0 through 9: type a number. The number will be used as the argument N to the next command.
  • j go down N lines.
  • k go up N lines.
  • p jump to the N% position, where 0% is the first line and 100% the last line of the file.

Examples:

Read some file:

$ less example.txt

Pipe "dmesg" into "less" so that the dmesg does not scroll off the screen:

$ dmesg | less

Links:

Lets you view binary files by outputting octal representation, hexadecimal representation and more. See also Od.

Examples:

View a file in octal format:

$ od wordlist.dat
0000000 064506 071562 006564 051412 061545 067157 006544 040412
0000020 070160 062554 005015 072522 061155 062554 005015 061501

View a file in hex format:

$ od -x wordlist.dat
0000000 6946 7372 0d74 530a 6365 6e6f 0d64 410a
0000020 7070 656c 0a0d 7552 626d 656c 0a0d 6341

View a file in character format:

$ od -c wordlist.dat
0000000   F   i   r   s   t  \r  \n   S   e   c   o   n   d  \r  \n   A
0000020   p   p   l   e  \r  \n   R   u   m   b   l   e  \r  \n

Links:

edit

Outputs 10 lines or other count of lines from the head (top) of a file, files or standard input. See also tail, outputting last lines.

Examples:

  • head wordlist.dat
    • Outputs first 10 lines of the file.
  • head -n 2 wordlist.dat
    • Outputs first 2 lines of the file.
  • head -2 wordlist.dat
    • Outputs first 2 lines of the file. A legacy option no longer supported by POSIX, still supported by GNU head.
  • seq 10 | head -n -2
    • In GNU head, outputs all but the last 2 lines of the input stream, in this case of 1 ... 10. A GNU extension not supported by POSIX.
  • seq 10 | sed 2q
    • A sed alternative that avoids head, outputting the first two lines.

Links:

hexdump

edit

Hexdump is a command-line tool used to show the raw bytes of a file in various ways including hexadecimal, available on Linux, FreeBDS, OS X, and other platforms. Hexdump is not part of traditional Unix systems or GNU commands, and does not seem to be covered by POSIX. A similar tool is #od (POSIX covered) and #xxd.

Examples of hexdump use:

  • hexdump myfile
    • Outputs hexadecimal of the file content, with no ASCII. Seems to default to outputting 16-bit chunks separated by space, 16 bytes per line.
  • hexdump -C myfile
    • Outputs bytes in hexadecimal, each byte separated by a space, together with ASCII at a right column.
  • hexdump -v -e '"" 1/1 "%02X" " "' myfile
    • Outputs the bytes in hexadecimal using capitalized letters, each byte separated by a space. The first "" specifies and empty prefix, while the last " " specifies a single-space suffix.

Links:

Outputs hexadecimal content of a file or standard input, or creates a file based on a hex dump. Not covered by POSIX and not part of GNU tools; seems to be part of Vim project, distributed with it.

Examples:

  • xxd myfile
    • Outputs hexadecimal output together with ASCII at a right column; the default seems to be to output 16-bit space-separated chunks. Does not seem to respect the endianness of the platform in these 16-bit chunks.
  • xxd -g 1 myfile
    • As above but separating each byte by a space.
  • xxd -b myfile.
    • Outputs binary output.

Links:

tail

edit

Outputs last 10 lines or other specified number of lines or bytes of the file, files or standard input. Its counterpart is #head. Whether POSIX allows multiple input files is unclear.

Examples:

  • tail wordlist.dat
    • Outputs last 10 lines.
  • tail -n 2 wordlist.dat
    • Outputs last 2 lines.
  • tail -2 wordlist.dat
    • Outputs last 2 lines. A legacy option no longer supported by POSIX, still supported by GNU tail.
  • printf hello | tail -c 2
    • Outputs two last bytes/characters of the input, and thus, "lo".
  • tail file1.txt file2.txt
    • In GNU tail, processes multiple files, introducing the output for each file with a header.

The -f option displays the tail, then waits for and displays any new options to the file. This is normally used to watch log files. (The next example has only three lines from tail, but the 80-column terminal was too narrow, so the lines were broken into five lines.)

$ tail -f /var/log/messages
Apr 14 00:05:33 redserver sshd[1575]: Accepted password for rumbear from 24.52.1
45.23 port 33372 ssh2
Apr 14 00:05:34 redserver sshd[1594]: subsystem request for sftp
Apr 14 00:06:35 redserver sshd[1594]: Received disconnect from 242.122.35.47: 11
: Disconnect requested by Windows SSH Client.

Links: