xref: /freebsd/usr.bin/hexdump/hexdump.1 (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
1.\" Copyright (c) 1989, 1990, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"	@(#)hexdump.1	8.2 (Berkeley) 4/18/94
29.\" $FreeBSD$
30.\"
31.Dd June 29, 2020
32.Dt HEXDUMP 1
33.Os
34.Sh NAME
35.Nm hexdump , hd
36.Nd ASCII, decimal, hexadecimal, octal dump
37.Sh SYNOPSIS
38.Nm
39.Op Fl bcCdovx
40.Op Fl e Ar format_string
41.Op Fl f Ar format_file
42.Op Fl n Ar length
43.Bk -words
44.Op Fl s Ar offset
45.Ek
46.Ar
47.Nm hd
48.Op Fl bcdovx
49.Op Fl e Ar format_string
50.Op Fl f Ar format_file
51.Op Fl n Ar length
52.Bk -words
53.Op Fl s Ar offset
54.Ek
55.Ar
56.Sh DESCRIPTION
57The
58.Nm
59utility is a filter which displays the specified files, or
60the standard input, if no files are specified, in a user specified
61format.
62.Pp
63The options are as follows:
64.Bl -tag -width indent
65.It Fl b
66.Em One-byte octal display .
67Display the input offset in hexadecimal, followed by sixteen
68space-separated, three column, zero-filled, bytes of input data,
69in octal, per line.
70.It Fl c
71.Em One-byte character display .
72Display the input offset in hexadecimal, followed by sixteen
73space-separated, three column, space-filled, characters of input
74data per line.
75.It Fl C
76.Em Canonical hex+ASCII display .
77Display the input offset in hexadecimal, followed by sixteen
78space-separated, two column, hexadecimal bytes, followed by the
79same sixteen bytes in %_p format enclosed in ``|'' characters.
80.Pp
81Calling the command
82.Nm hd
83implies this option.
84.It Fl d
85.Em Two-byte decimal display .
86Display the input offset in hexadecimal, followed by eight
87space-separated, five column, zero-filled, two-byte units
88of input data, in unsigned decimal, per line.
89.It Fl e Ar format_string
90Specify a format string to be used for displaying data.
91.It Fl f Ar format_file
92Specify a file that contains one or more newline separated format strings.
93Empty lines and lines whose first non-blank character is a hash mark
94.Pf ( Cm \&# )
95are ignored.
96.It Fl n Ar length
97Interpret only
98.Ar length
99bytes of input.
100.It Fl o
101.Em Two-byte octal display .
102Display the input offset in hexadecimal, followed by eight
103space-separated, six column, zero-filled, two byte quantities of
104input data, in octal, per line.
105.It Fl s Ar offset
106Skip
107.Ar offset
108bytes from the beginning of the input.
109By default,
110.Ar offset
111is interpreted as a decimal number.
112With a leading
113.Cm 0x
114or
115.Cm 0X ,
116.Ar offset
117is interpreted as a hexadecimal number,
118otherwise, with a leading
119.Cm 0 ,
120.Ar offset
121is interpreted as an octal number.
122Appending the character
123.Cm b ,
124.Cm k ,
125or
126.Cm m
127to
128.Ar offset
129causes it to be interpreted as a multiple of
130.Li 512 ,
131.Li 1024 ,
132or
133.Li 1048576 ,
134respectively.
135.It Fl v
136Cause
137.Nm
138to display all input data.
139Without the
140.Fl v
141option, any number of groups of output lines, which would be
142identical to the immediately preceding group of output lines (except
143for the input offsets), are replaced with a line comprised of a
144single asterisk.
145.It Fl x
146.Em Two-byte hexadecimal display .
147Display the input offset in hexadecimal, followed by eight, space
148separated, four column, zero-filled, two-byte quantities of input
149data, in hexadecimal, per line.
150.El
151.Pp
152For each input file,
153.Nm
154sequentially copies the input to standard output, transforming the
155data according to the format strings specified by the
156.Fl e
157and
158.Fl f
159options, in the order that they were specified.
160.Ss Formats
161A format string contains any number of format units, separated by
162whitespace.
163A format unit contains up to three items: an iteration count, a byte
164count, and a format.
165.Pp
166The iteration count is an optional positive integer, which defaults to
167one.
168Each format is applied iteration count times.
169.Pp
170The byte count is an optional positive integer.
171If specified it defines the number of bytes to be interpreted by
172each iteration of the format.
173.Pp
174If an iteration count and/or a byte count is specified, a single slash
175must be placed after the iteration count and/or before the byte count
176to disambiguate them.
177Any whitespace before or after the slash is ignored.
178.Pp
179The format is required and must be surrounded by double quote
180(" ") marks.
181It is interpreted as a fprintf-style format string (see
182.Xr fprintf 3 ) ,
183with the
184following exceptions:
185.Bl -bullet -offset indent
186.It
187An asterisk (*) may not be used as a field width or precision.
188.It
189A byte count or field precision
190.Em is
191required for each ``s'' conversion
192character (unlike the
193.Xr fprintf 3
194default which prints the entire string if the precision is unspecified).
195.It
196The conversion characters ``h'', ``l'', ``n'', ``p'' and ``q'' are
197not supported.
198.It
199The single character escape sequences
200described in the C standard are supported:
201.Bd -ragged -offset indent -compact
202.Bl -column <alert_character>
203.It "NUL	\e0"
204.It "<alert character>	\ea"
205.It "<backspace>	\eb"
206.It "<form-feed>	\ef"
207.It "<newline>	\en"
208.It "<carriage return>	\er"
209.It "<tab>	\et"
210.It "<vertical tab>	\ev"
211.El
212.Ed
213.El
214.Pp
215The
216.Nm
217utility also supports the following additional conversion strings:
218.Bl -tag -width Fl
219.It Cm \&_a Ns Op Cm dox
220Display the input offset, cumulative across input files, of the
221next byte to be displayed.
222The appended characters
223.Cm d ,
224.Cm o ,
225and
226.Cm x
227specify the display base
228as decimal, octal or hexadecimal respectively.
229.It Cm \&_A Ns Op Cm dox
230Identical to the
231.Cm \&_a
232conversion string except that it is only performed
233once, when all of the input data has been processed.
234.It Cm \&_c
235Output characters in the default character set.
236Nonprinting characters are displayed in three character, zero-padded
237octal, except for those representable by standard escape notation
238(see above),
239which are displayed as two character strings.
240.It Cm _p
241Output characters in the default character set.
242Nonprinting characters are displayed as a single
243.Dq Cm \&. .
244.It Cm _u
245Output US ASCII
246characters, with the exception that control characters are
247displayed using the following, lower-case, names.
248Characters greater than 0xff, hexadecimal, are displayed as hexadecimal
249strings.
250.Bl -column 000_nu 001_so 002_st 003_et 004_eo
251.It "000 NUL" Ta "001 SOH" Ta "002 STX" Ta "003 ETX" Ta "004 EOT" Ta "005 ENQ"
252.It "006 ACK" Ta "007 BEL" Ta "008 BS" Ta "009 HT" Ta "00A LF" Ta "00B VT"
253.It "00C FF" Ta "00D CR" Ta "00E\ SO" Ta "00F SI" Ta "010 DLE" Ta "011 DC1"
254.It "012 DC2" Ta "013 DC3" Ta "014 DC4" Ta "015\ NAK" Ta "016 SYN" Ta "017 ETB"
255.It "018 CAN" Ta "019 EM" Ta "01A SUB" Ta "01B ESC" Ta "01C FS" Ta "01D GS"
256.It "01E RS" Ta "01F US" Ta "07F DEL" Ta \& Ta \& Ta \&
257.El
258.El
259.Pp
260The default and supported byte counts for the conversion characters
261are as follows:
262.Bl -tag -width  "Xc,_Xc,_Xc,_Xc,_Xc,_Xc" -offset indent
263.It Li \&%_c , \&%_p , \&%_u , \&%c
264One byte counts only.
265.It Xo
266.Li \&%d , \&%i , \&%o ,
267.Li \&%u , \&%X , \&%x
268.Xc
269Four byte default, one, two and four byte counts supported.
270.It Xo
271.Li \&%E , \&%e , \&%f ,
272.Li \&%G , \&%g
273.Xc
274Eight byte default, four and twelve byte counts supported.
275.El
276.Pp
277The amount of data interpreted by each format string is the sum of the
278data required by each format unit, which is the iteration count times the
279byte count, or the iteration count times the number of bytes required by
280the format if the byte count is not specified.
281.Pp
282The input is manipulated in ``blocks'', where a block is defined as the
283largest amount of data specified by any format string.
284Format strings interpreting less than an input block's worth of data,
285whose last format unit both interprets some number of bytes and does
286not have a specified iteration count, have the iteration count
287incremented until the entire input block has been processed or there
288is not enough data remaining in the block to satisfy the format string.
289.Pp
290If, either as a result of user specification or
291.Nm
292modifying
293the iteration count as described above, an iteration count is
294greater than one, no trailing whitespace characters are output
295during the last iteration.
296.Pp
297It is an error to specify a byte count as well as multiple conversion
298characters or strings unless all but one of the conversion characters
299or strings is
300.Cm \&_a
301or
302.Cm \&_A .
303.Pp
304If, as a result of the specification of the
305.Fl n
306option or end-of-file being reached, input data only partially
307satisfies a format string, the input block is zero-padded sufficiently
308to display all available data (i.e., any format units overlapping the
309end of data will display some number of the zero bytes).
310.Pp
311Further output by such format strings is replaced by an equivalent
312number of spaces.
313An equivalent number of spaces is defined as the number of spaces
314output by an
315.Cm s
316conversion character with the same field width
317and precision as the original conversion character or conversion
318string but with any
319.Dq Li \&+ ,
320.Dq \&\ \& ,
321.Dq Li \&#
322conversion flag characters
323removed, and referencing a NULL string.
324.Pp
325If no format strings are specified, the default display is equivalent
326to specifying the
327.Fl x
328option.
329.Sh EXIT STATUS
330.Ex -std hexdump hd
331.Sh EXAMPLES
332Dump input in canonical (hex+ASCII) form:
333.Bd -literal -offset indent
334$ echo "FreeBSD: The power to serve" | hexdump -C
33500000000  46 72 65 65 42 53 44 3a  20 54 68 65 20 70 6f 77  |FreeBSD: The pow|
33600000010  65 72 20 74 6f 20 73 65  72 76 65 0a              |er to serve.|
3370000001c
338.Ed
339.Pp
340Same as above but skipping the first 4 bytes of stdin and interpreting only 3
341bytes of input:
342.Bd -literal -offset indent
343$ echo "FreeBSD: The power to serve" | hexdump -C -s 4 -n 3
34400000004  42 53 44                                          |BSD|
34500000007
346.Ed
347.Pp
348Assuming a format file named
349.Pa format.txt
350with the following contents that specify a perusal format:
351.Bd -literal -offset indent
352"%06.6_ao "  12/1 "%3_u "
353"\et\et" "%_p "
354"\en"
355.Ed
356.Pp
357Dump input in canonical form using the format in
358.Pa format.txt :
359.Bd -literal -offset indent
360$ echo "FreeBSD" | hexdump -f format.txt -C
361000000   F   r   e   e   B   S   D  lf                          F r e e B S D .
36200000000  46 72 65 65 42 53 44 0a                           |FreeBSD.|
36300000008
364.Ed
365.Pp
366Assuming a format file named
367.Pa format.txt
368with the following contents that simulate the
369.Fl x
370option:
371.Bd -literal -offset indent
372"%07.7_Ax\en"
373"%07.7_ax  " 8/2 "%04x " "\en"
374.Ed
375.Pp
376Dump input in canonical form using the format in
377.Pa format.txt :
378.Bd -literal -offset indent
379$ echo "FreeBSD: The power to serve" | hexdump -f format.txt -C
3800000000  7246 6565 5342 3a44 5420 6568 7020 776f
38100000000  46 72 65 65 42 53 44 3a  20 54 68 65 20 70 6f 77  |FreeBSD: The pow|
3820000010  7265 7420 206f 6573 7672 0a65
38300000010  65 72 20 74 6f 20 73 65  72 76 65 0a              |er to serve.|
3840000001c
385.Ed
386.Sh SEE ALSO
387.Xr od 1
388