Last modified on 9 April 2014, at 02:14

Guide to Unix/Commands/File Viewing

catEdit

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

tacEdit

tac (cat spelled backwards) works like cat, but reverse the order of the lines (last line is written out first). When multiple files are given, they are printed out in the order they appear.

$ tac foo
Third Line
Second Line
First Line

moreEdit

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

lessEdit

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

odEdit

od is a utility that lets you view binary files.

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

headEdit

head displays 10 lines from the head (top) of a given file

Examples:

$ head wordlist.dat
First
Second
Third
Fourth
Fifth
Sixth
Seventh
Eighth
Ninth
Tenth

Display the top two lines:

$ head -2 wordlist.dat
First
Second

tailEdit

tail displays last 10 lines of the file

Examples:

$ tail wordlist.dat
Ninety-First
Ninety-Second
Ninety-Third
Ninety-Fourth
Ninety-Fifth
Ninety-Sixth
Ninety-Seventh
Ninety-Eighth
Ninety-Ninth
One Hundredth

Display the bottom two lines:

$ tail -2 wordlist.dat 
Ninety-Ninth
One Hundredth

Tips: 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.