Examining File Contents


Chapter 3 described several commands for viewing text files: cat, head, tail, and the pagers pg, more, and less. These are adequate for most purposes, but they are of limited use with files that contain nonprinting ASCII characters, and they are of no use at all with files that contain binary data. This section describes the od and strings commands, which help you view the contents of files that contain nonprinting characters or binary data. It also includes the tac command, which is a backward version of cat.

od

The od command shows you the exact contents of a file, including nonprinting characters. It can be used for both text and data files. od prints the contents of each byte of the file in any of several different representations, including octal, hexadecimal, and “character” formats. The following discussion deals only with the character representation, which is invoked with the c (character) option. To illustrate how od works, consider how it displays an ordinary text file. For example,

 $ cat example The UNIX Operating System is becoming increasingly popular. $ od −c example 0000000  T  h  e     U  N  I  X     O  p  e  r  a  t  i 0000020  n  g     S  y  s  t  e  m     i  s     b  e  c 0000040  o  m  i  n  g    \n  i  n  c  r  e  a  s  i  n 0000060  g  l  y     p  o  p  u  l  a  r  . \n 0000076

Each line of the output shows 16 bytes of data, interpreted as ASCII characters. The number at the beginning of each line is the octal representation of the offset, or position, in the file of the first byte in the line. The other fields show each byte in its character representation. The file in this example is an ordinary text file, so the output consists mostly of normal characters. The only thing that is special is the \n, which represents the newline at the end of each line in the file. Newline is an ASCII character, but od uses the special sequence \n to make it visible. Other special sequences include \t (tab), \b (backspace), and \r (return). Less common nonprinting characters are shown as a threedigit octal representation of their ASCII encoding.

You can specify an offset, a number of bytes of input to skip before displaying the data, as an octal number following the filename. For example, the following command skips 16 bytes (octal 20):

 $ od −c data_file 20

strings

Some files are mostly binary data but may contain a few readable strings. If these files are very long, then using od to read them can take a very long time. The command strings will search a file for printable characters. By default, strings prints any chains of four or more printable characters that it finds. In this example, strings searches the binary file ping for printable characters and prints chains of six or more characters.

 $ strings −6 ping

The strings command can be used on multiple files at once. The f option tells it to print the name of the file when it prints a string of characters, so that you know which file the string came from.

 $ strings −f /bin/* | grep version more

In this example, strings searches all the files in /bin. It sends the results to grep, which searches for lines containing the word “version”. Each of these lines is printed to the screen, along with the name of the file it came from.

tac

The tac command is a backward version of cat. It takes a list of files and prints them line by line to standard output, but in reverse line order. Like cat, tac can accept standard input.

You can use the s option to tell tac to use a separator other than newline to mark breaks between “lines”. For example, if the individual records in the file accounts are separated by ***, the following command will print them in reverse order.

 $ tac −s "***" accounts




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net