This tutorial explains Linux “od” command, options and its usage with examples.
DESCRIPTION
octal dump displays contents as octal numbers. This can be useful when the output contains non-printable characters. For example, a filename may contain non-printable characters. It can also be handy to view binary files.
The name is an acronym for “octal dump” since it defaults to printing in the octal data format. It can also displaying output in a variety of other formats, including hexadecimal, decimal, and ASCII. It is useful for visualizing data that isn’t in a human-readable format, like the executable code of a program.
SYNOPSIS
od [-abcdfhiloxv] [-s[bytes]] [-w[bytes]] [-A radix] [-j bytes] [-N bytes] [-t type] [–skip-bytes=bytes] [–address-radix=radix] [–read-bytes=bytes] [–format=type] [–output-duplicates] [–strings[=bytes]] [–width[=bytes]] [–traditional] [–help] [–version] [file…]
OPTIONS :
-A, –address-radix=radix
Select the base in which file offsets are printed.
radix can be one of the following:
d decimal
o octal
x hexadecimal
n none (do not print offsets)
The default is octal.
-j, –skip-bytes=bytes
Skip bytes input bytes before formatting and writing. If bytes begins with ‘0x’ or ‘0X’, it is interpreted in hexadecimal; otherwise, if it begins with ‘0’, in octal; otherwise, in decimal. Appending ‘b’ multiplies it by 512, ‘k’ by 1024, and ‘m’ by 1048576.
-N, –read-bytes=bytes
Only output up to bytes bytes of each input file. Any prefixes and suffixes on bytes are interpreted as for the -j option.
-t, –format=type
Select the format in which to output the file data. type is a string of one or more of the below type indicator characters. If you include more than one type indicator character in a single type string or use this option more than once, od writes one copy of each output line using each of the data types that you specified, in the order that you specified.
a named character
c ASCII character or backslash escape
d signed decimal
f floating point
o octal
u unsigned decimal
x hexadecimal
C char
S short
I int
L long
For floating point (f):
F float
D double
L long double
-v, –output-duplicates
Output consecutive lines that are identical. By default, when two or more consecutive output lines would be equal, od outputs only the first line, and puts just an asterisk on the following line to indicate that identical lines have been elided.
-s, –strings[=bytes]
Instead of the normal output, output only string constants in the input, which are a run of at least bytes ASCII graphic (or formatting) characters, terminated by a NUL. If bytes is omitted, it defaults to 3.
-w,–width[=bytes]
The number of input bytes to format per output line. It must be a multiple of the least common multiple of the sizes associated with the specified output types. If bytes is omitted, it defaults to 32. If this option is not given, it defaults to
16.
-a
Output as named characters. Equivalent to -t a.
-b
Output as octal bytes. Equivalent to -t oC.
-c
Output as ASCII characters or backslash escapes.
Equivalent to -t c.
-d
Output as unsigned decimal shorts. Equivalent to -t u2.
-f
Output as floats. Equivalent to -t fF.
-h
Output as hexadecimal shorts. Equivalent to -t x2.
-i
Output as decimal shorts. Equivalent to -t d2.
-l
Output as decimal longs. Equivalent to -t d4.
-o
Output as octal shorts. Equivalent to -t o2.
-x
Output as hexadecimal shorts. Equivalent to -t x2.
EXAMPLES
1. Display contents of file in octal
The following is the input file used for this example:
$ cat input 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Now execute od command on this input file.
$ od -b input 0000000 061 012 062 012 063 012 064 012 065 012 066 012 067 012 070 012 0000020 071 012 061 060 012 061 061 012 061 062 012 061 063 012 061 064 0000040 012 061 065 012 061 066 012 061 067 012 061 070 012 061 071 012 0000060 062 060 012 0000063
The first column in the output of od represents the byte offset in file.
2. Display contents of file in character format
$ od -c input 0000000 1 \n 2 \n 3 \n 4 \n 5 \n 6 \n 7 \n 8 \n 0000020 9 \n 1 0 \n 1 1 \n 1 2 \n 1 3 \n 1 4 0000040 \n 1 5 \n 1 6 \n 1 7 \n 1 8 \n 1 9 \n 0000060 2 0 \n 0000063
3. Display the byte offsets in different formats
The byte offset can be displayed in any of the following formats :
Hexadecimal (using -x along with -A)
Octal (using -o along with -A)
Decimal (using -d along with -A)
$ od -Ax -c input 000000 1 \n 2 \n 3 \n 4 \n 5 \n 6 \n 7 \n 8 \n 000010 9 \n 1 0 \n 1 1 \n 1 2 \n 1 3 \n 1 4 000020 \n 1 5 \n 1 6 \n 1 7 \n 1 8 \n 1 9 \n 000030 2 0 \n 000033
4. Display no offset information using ‘-An’ option
$ od -An -c input 1 \n 2 \n 3 \n 4 \n 5 \n 6 \n 7 \n 8 \n 9 \n 1 0 \n 1 1 \n 1 2 \n 1 3 \n 1 4 \n 1 5 \n 1 6 \n 1 7 \n 1 8 \n 1 9 \n 2 0 \n
5. Display output after skipping some bytes
$ od -j9 -c input 0000011 \n 6 \n 7 \n 8 \n 9 \n 1 0 \n 1 1 \n 1 0000031 2 \n 1 3 \n 1 4 \n 1 5 \n 1 6 \n 1 7 0000051 \n 1 8 \n 1 9 \n 2 0 \n 0000063
Initial 9 bytes were skipped from output.
6. Display limited bytes in output using -N option
$ od -N9 -c input 0000000 1 \n 2 \n 3 \n 4 \n 5 0000011
Only 9 bytes were displayed in the output.
7. Display output as decimal integers
$ od -i input 0000000 171051569 171182643 171313717 171444791 0000020 808520249 170995978 822751793 875629107 0000040 171258122 822752817 942737975 171520266 0000060 667698 0000063
8. Display output as hexadecimal 2 byte units
$ od -x input 0000000 0a31 0a32 0a33 0a34 0a35 0a36 0a37 0a38 0000020 0a39 3031 310a 0a31 3231 310a 0a33 3431 0000040 310a 0a35 3631 310a 0a37 3831 310a 0a39 0000060 3032 000a 0000063
9. Display the contents as two byte octal units
$ od -o input 0000000 005061 005062 005063 005064 005065 005066 005067 005070 0000020 005071 030061 030412 005061 031061 030412 005063 032061 0000040 030412 005065 033061 030412 005067 034061 030412 005071 0000060 030062 000012 0000063
10. Customize the output width using -w option
$ od -w1 -c -Ad input 0000000 1 0000001 \n 0000002 2 0000003 \n 0000004 3 0000005 \n 0000006 4 0000007 \n 0000008 5 0000009 \n 0000010 6 0000011 \n 0000012 7 0000013 \n 0000014 8 0000015 \n 0000016 9 0000017 \n 0000018 1 0000019 0 0000020 \n 0000021 1 * 0000023 \n 0000024 1 0000025 2 0000026 \n 0000027 1 0000028 3 0000029 \n 0000030 1 0000031 4 0000032 \n 0000033 1 0000034 5 0000035 \n 0000036 1 0000037 6 0000038 \n 0000039 1 0000040 7 0000041 \n 0000042 1 0000043 8 0000044 \n 0000045 1 0000046 9 0000047 \n 0000048 2 0000049 0 0000050 \n 0000051
11. Output duplicates using -v option
As can be observed in the output of example 10 above, a * was printed. This is done to suppress the output of lines that are same or duplicates. But through -v option these lines can also be printed.
$ od -w1 -v -c -Ad input 0000000 1 0000001 \n 0000002 2 0000003 \n 0000004 3 0000005 \n 0000006 4 0000007 \n 0000008 5 0000009 \n 0000010 6 0000011 \n 0000012 7 0000013 \n 0000014 8 0000015 \n 0000016 9 0000017 \n 0000018 1 0000019 0 0000020 \n 0000021 1 0000022 1 0000023 \n 0000024 1 0000025 2 0000026 \n 0000027 1 0000028 3 0000029 \n 0000030 1 0000031 4 0000032 \n 0000033 1 0000034 5 0000035 \n 0000036 1 0000037 6 0000038 \n 0000039 1 0000040 7 0000041 \n 0000042 1 0000043 8 0000044 \n 0000045 1 0000046 9 0000047 \n 0000048 2 0000049 0 0000050 \n 0000051
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Check Information Technology Books
- Apply for Programming Internship
- Practice Programming MCQs
- Check Linux Books