Recommended: One Linux command per day (1) : xargs

Function introduction

The od (Octal Dump) command is used to display a specified file in the format of Octal, decimal, hexadecimal, floating point, or ASCII encoded characters. It is usually used to display or view the characters in the file that cannot be directly displayed on the terminal. The default od command display mode is octal.

Common files are text files and binary files. The OD command is mainly used to view the values saved in the binary file, interpret the data in the file according to the specified format, and output the values, whether IEEE754 floating point number or ASCII code, as required.

You can also read about the hexdump command, which is printed in hexadecimal format, but feels less powerful than the OD command.

The command format

od [OPTION]... [FILE]...

Copy the code

Option to show

-j BYTES --skip-bytes=BYTES # Skip the specified number of BYTES. -n BYTES --read-bytes=BYTES -s [BYTES] --strings[=BYTES] -s [BYTES] --strings[=BYTES] -w [BYTES] --width[=BYTES] # Set the number of BYTES displayed in each line. BYTES The default value is 32 BYTES. -t TYPE --format=TYPE # Specifies the output format. The format can be a, C, D, f, O, u, or x. For example, newlines appear as nl C: printable characters or escape characters represented by backslashes; For example, the newline character is displayed as n d[SIZE] : SIZE bytes form a signed decimal integer. SIZE The default value is sizeof(int) f[SIZE] : SIZE bytes that form a floating point number. SIZE The default is sizeof(double) o[SIZE] : SIZE bytes constitute an octal integer. SIZE The default value is sizeof(int) U [SIZE] : SIZE bytes that form an unsigned decimal integer. SIZE The default value is sizeof(int) x[SIZE] : SIZE bytes that form a hexadecimal integer. SIZE The default value is sizeof(int). The SIZE can be a number or an uppercase letter. If TYPE is one of [doux], then SIZE can be C = sizeof(char), S = sizeof(short), I = sizeof(int), L = sizeof(long). If TYPE is f, then SIZE can be f = sizeof(float), D = sizeof(double), L = sizeof(long double) --help # online help --version # Display version informationCopy the code

Commonly used sample

1. Set the offset address of the first column to decimal.

The offset address displays the cardinality as: D for Decimal, O for octal, x for hexadecimal or N for None.Copy the code

2. Od does not display the offset address of the first column.

od -An testfile

Copy the code

3. Output in hexadecimal format. The output is displayed in a group (column) of four bytes by default.

od -tx testfile

Copy the code

4. Output in hexadecimal format, with one byte for each column.

od -tx1 testfile

Copy the code

5, display ASCII characters and ASCII character names, pay attention to the difference between the display of newlines.

[b3335 @ # display ASCII localhost] $echo LVLV | od - a 0000000 l v l v nl 0000005 # displays the name of the ASCII characters [b3335 @ localhost] $echo LVLV | od -tc 0000000 l v l v n 0000005Copy the code

6. Display original characters in hexadecimal format.

[b3335@localhost]$ echo lvlv|od -tcx1
0000000   l   v   l   v  n
         6c  76  6c  76  0a
0000005

Copy the code

7. Specify 512 bytes per line.

od -w512 -tx1 testfile

Copy the code

8. Remove Spaces between columns during od command output.

When we want to display the contents of a file in hexadecimal, we need to output successive single bytes, each byte displayed in hexadecimal. In this case, we can use the OD command to group the file as a group of single bytes, print the hexadecimal output on the same line, and remove the Spaces between each byte. It is not known how to remove Spaces between columns by specifying options in the OD command, which may not be supported by the OD command itself. Here’s how I did it:

  • (a) use -an without output offset address;

  • (b) Do not omit duplicate data when using -v output;

  • (c) Use -tx1 to output in hexadecimal format as a group of single bytes, and -w1 to output one byte per column;

  • (d) Standard input that is finally piped to AWK, which outputs all lines without newlines through AWK and concatenates them into one line output.

Specific commands are as follows:

od -An -w1 -tx1 testfile|awk '{for(i=1; i<=NF; ++i){printf "%s",$i}}'Copy the code

Source:Dablelv.blog.csdn.net/article/det…