UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 25.6 What's in That White Space? Chapter 25
Showing What's in a File
Next: 25.8 Finding File Types
 

25.7 Show Non-Printing Characters with cat -v or od -c

Especially if you use an ASCII-based terminal, files can have characters that your terminal can't display. Some characters will lock up your communications software or hardware, make your screen look strange, or cause other weird problems. So if you'd like to look at a file and you aren't sure what's in there, it's not a good idea to just cat the file!

Instead, try cat -v. It turns non-printable characters into a printable form. In fact, although most manual pages don't explain how, you can read the output and see what's in the file. Another utility for displaying non-printable files is od. I usually use its -c option when I need to look at a file character by character.

Let's look at a file that's almost guaranteed to be unprintable: a directory file. This example is on a standard V7 (UNIX Version 7) filesystem. (Unfortunately, some UNIX systems won't let you read a directory. If you want to follow along on one of those systems, try a compressed file (24.7) or an executable program from /bin.) A directory usually has some long lines, so it's a good idea to pipe cat's output through fold (43.8):

-f 














% ls -fa
.
..
comp
% cat -v . | fold -62
M-^?^N.^@^@^@^@^@^@^@^@^@^@^@^@^@>^G..^@^@^@^@^@^@^@^@^@^@^@^@
M-a
comp^@^@^@^@^@^@^@^@^@^@^@^@MassAveFood^@^@^@^@^@hist^@^@^
@^@^@^@^@^@^@^@
% od -c .
0000000 377 016   .  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020   > 007   .   .  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000040 341  \n   c   o   m   p  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000060  \0  \0   M   a   s   s   A   v   e   F   o   o   d  \0  \0  \0
0000100  \0  \0   h   i   s   t  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000120

Each entry in a V7-type directory is 16 bytes long (that's also 16 characters, in the ASCII (51.3) system). The od -c command starts each line with the number of bytes, in octal, shown since the start of the file. The first line starts at byte 0. The second line starts at byte 20 (that's byte 16 in decimal, the way most of us count). And so on. Enough about od for now, though. We'll come back in a minute. Time to dissect the cat -v output:

cat has two options, -t and -e, for displaying white space in a line. The -v option doesn't convert TAB and trailing space characters to a visible form without those options. See article 25.6.

Next, time for od -c; it's easier to explain than cat -v:

The strings (27.19) program finds printable strings of characters (such as filenames) inside mostly non-printable files (like executable binaries).

- JP


Previous: 25.6 What's in That White Space? UNIX Power ToolsNext: 25.8 Finding File Types
25.6 What's in That White Space? Book Index25.8 Finding File Types

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System