Linux xxd Command Tutorial for Beginners (with Examples)
0

Want a hex dump for a textual content file? Searching for a command line utility that may do that? Properly, you might be in luck, as there exists a command dubbed xxd that may do that for you. On this tutorial, we are going to talk about xxd utilizing some straightforward to know examples. However earlier than we do this, it is value mentioning that every one examples right here have been examined on an Ubuntu 18.04 LTS machine.

Linux xxd command

The xxd command in Linux enables you to create a hexdump and even do the reverse. Following is its syntax:

xxd [OPTIONS] [file]

And this is how the software’s man web page explains it:

xxd  creates  a hex dump of a given file or commonplace enter.  It might probably additionally convert a hex dump again to its unique binary type.  Like
uuencode(1) and uudecode(1) it permits the transmission of binary knowledge in a `mail-safe' ASCII illustration, however has the  benefit
of decoding to straightforward output.  Furthermore, it may be used to carry out binary file patching.

Following are some Q&A-styled examples that ought to offer you a greater thought on how xxd works.

Q1. Find out how to use xxd command?

It is easy. For instance, suppose you’ve got a textual content file ‘take a look at.txt’ with contents as proven beneath:

No.|Nation|Sure/No
01|India|Y
02|US|Y
03|Australia|Y

04|China|N
05|Russia|Y
06|Japan|Y

07|Singapore|Y
08|South Korea|N
09|Finland|Y
10|Eire|Y

This is how you need to use xxd to transform the file contents into hex:

xxd take a look at.txt

And this is the output:

00000000: 4e6f 2e7c 436f 756e 7472 797c 5965 732f  No.|Nation|Sure/
00000010: 4e6f 0a30 317c 496e 6469 617c 590a 3032  No.01|India|Y.02
00000020: 7c55 537c 590a 3033 7c41 7573 7472 616c  |US|Y.03|Austral
00000030: 6961 7c59 0a0a 3034 7c43 6869 6e61 7c4e  ia|Y..04|China|N
00000040: 0a30 357c 5275 7373 6961 7c59 0a30 367c  .05|Russia|Y.06|
00000050: 4a61 7061 6e7c 590a 0a30 377c 5369 6e67  Japan|Y..07|Sing
00000060: 7061 6f72 657c 590a 3038 7c53 6f75 7468  paore|Y.08|South
00000070: 204b 6f72 6561 7c4e 0a30 397c 4669 6e61   Korea|N.09|Fina
00000080: 6c61 6e64 7c59 0a31 307c 4972 656c 616e  land|Y.10|Irelan
00000090: 647c 590a                                d|Y.

Q2. Find out how to skip a number of strains with xxd?

Suppose you do not need hexdump of the whole file. As an alternative, you need the software to begin changing from a selected line. Then this may be achieved utilizing the -s command.

For instance, if you would like xxd to provide hex dump from line three onwards, then this is the way you inform xxd to do that:

xxd -s 0x30 take a look at.txt

This is the output produced:

00000030: 6961 7c59 0a0a 3034 7c43 6869 6e61 7c4e  ia|Y..04|China|N
00000040: 0a30 357c 5275 7373 6961 7c59 0a30 367c  .05|Russia|Y.06|
00000050: 4a61 7061 6e7c 590a 0a30 377c 5369 6e67  Japan|Y..07|Sing
00000060: 7061 6f72 657c 590a 3038 7c53 6f75 7468  paore|Y.08|South
00000070: 204b 6f72 6561 7c4e 0a30 397c 4669 6e61   Korea|N.09|Fina
00000080: 6c61 6e64 7c59 0a31 307c 4972 656c 616e  land|Y.10|Irelan
00000090: 647c 590a                                d|Y.

So you’ll be able to see the hexdump is produced line three onwards on this case.

Q3. Find out how to restrict xxd output to a specific size?

Within the earlier part, we mentioned how you can make xxd begin changing from a specific level. However there’s additionally a technique to limitize its conversion to a specific level. This may be achieved utilizing the -l command line choice.

For instance, to ensure xxd creates dump for under the primary three strains of take a look at.txt, use it within the following means:

xxd -l 0x30 take a look at.txt

This is the output it produced:

00000000: 4e6f 2e7c 436f 756e 7472 797c 5965 732f  No.|Nation|Sure/
00000010: 4e6f 0a30 317c 496e 6469 617c 590a 3032  No.01|India|Y.02
00000020: 7c55 537c 590a 3033 7c41 7573 7472 616c  |US|Y.03|Austral

This autumn. Find out how to set column size?

In order for you xxd to provide fewer or extra columns in output, use the -c choice and specify the variety of columns there. This is an instance command utilizing this feature:

xxd -c 5 take a look at.txt

And this is the output:

00000000: 4e6f 2e7c 43  No.|C
00000005: 6f75 6e74 72  ountr
0000000a: 797c 5965 73  y|Sure
0000000f: 2f4e 6f0a 30  /No.0
00000014: 317c 496e 64  1|Ind
00000019: 6961 7c59 0a  ia|Y.
0000001e: 3032 7c55 53  02|US
00000023: 7c59 0a30 33  |Y.03
00000028: 7c41 7573 74  |Aust
0000002d: 7261 6c69 61  ralia
00000032: 7c59 0a0a 30  |Y..0
00000037: 347c 4368 69  4|Chi
0000003c: 6e61 7c4e 0a  na|N.
00000041: 3035 7c52 75  05|Ru
00000046: 7373 6961 7c  ssia|
0000004b: 590a 3036 7c  Y.06|
00000050: 4a61 7061 6e  Japan
00000055: 7c59 0a0a 30  |Y..0
0000005a: 377c 5369 6e  7|Sin
0000005f: 6770 616f 72  gpaor
00000064: 657c 590a 30  e|Y.0
00000069: 387c 536f 75  8|Sou
0000006e: 7468 204b 6f  th Ko
00000073: 7265 617c 4e  rea|N
00000078: 0a30 397c 46  .09|F
0000007d: 696e 616c 61  inala
00000082: 6e64 7c59 0a  nd|Y.
00000087: 3130 7c49 72  10|Ir
0000008c: 656c 616e 64  eland
00000091: 7c59 0a       |Y.

So you’ll be able to see solely 5 columns have been produced in output.

Q5. Find out how to make xxd produce binary dump?

Sure, that is additionally attainable. The -b command line choices makes xxd produce dump in binary digits. This is what the person web page says about this software:

-b | -bits
Change to bits (binary digits) dump, fairly than hexdump.  This feature writes octets as eight digits "1"s and  "0"s  as an alternative
of  a  regular  hexadecimal  dump. Every line is preceded by a line quantity in hexadecimal and adopted by an ascii (or ebcdic)
illustration. The command line switches -r, -p, -i don't work with this mode.

This is an instance:

Linux xxd Command Tutorial for Newbies (with Examples) 13

Conclusion

So you’ll be able to see, the xxd command is helpful in case you deal in hex and binary file contents. Right here, on this tutorial, we have simply mentioned some primary choices xxd affords. When you’re achieved training these, head to the software’s man web page to study extra about it.

Como Instalar e Usar o Docker no Ubuntu 18.04

Previous article

Cease Appearing Like an Novice Freelance Web Designer

Next article

You may also like

Comments

Leave a Reply

More in Linux