{"id":7252,"date":"2018-11-19T18:57:53","date_gmt":"2018-11-19T15:57:53","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-xxd-command\/"},"modified":"2018-11-19T18:57:53","modified_gmt":"2018-11-19T15:57:53","slug":"linux-xxd-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-xxd-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux xxd Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<p>Need a hex dump for a text file? Looking for a command line utility that can do this? Well, you are in luck, as there exists a command dubbed <strong>xxd<\/strong> that can do this for you. In this tutorial, we will discuss xxd using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-xxd-command\">Linux xxd command<\/h2>\n<p>The xxd command in Linux lets you create a hexdump or even do the reverse. Following is its syntax:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">xxd [OPTIONS] [file]<\/code><\/pre>\n<p>And here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre><code spellcheck=\"false\">xxd\u00a0 creates\u00a0 a hex dump of a given file or standard input.\u00a0 It can also convert a hex dump back to its original binary form.\u00a0 Like<br\/>uuencode(1) and uudecode(1) it allows the transmission of binary data in a `mail-safe' ASCII representation, but has the\u00a0 advantage<br\/>of decoding to standard output.\u00a0 Moreover, it can be used to perform binary file patching.<\/code><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how xxd works.<\/p>\n<h2 id=\"q-how-to-use-xxd-command\">Q1. How to use xxd command?<\/h2>\n<p>It&#8217;s simple. For example, suppose you have a text file &#8216;test.txt&#8217; with contents as shown below:<\/p>\n<pre readability=\"5\"><code spellcheck=\"false\" readability=\"4\">No.|Country|Yes\/No<br\/>01|India|Y<br\/>02|US|Y<br\/>03|Australia|Y<p>04|China|N<br\/>05|Russia|Y<br\/>06|Japan|Y<\/p><p>07|Singapore|Y<br\/>08|South Korea|N<br\/>09|Finland|Y<br\/>10|Ireland|Y<\/p><\/code><\/pre>\n<p>Here&#8217;s how you can use xxd to convert the file contents into hex:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">xxd test.txt<\/code><\/pre>\n<p>And here&#8217;s the output:<\/p>\n<pre><code spellcheck=\"false\">00000000: 4e6f 2e7c 436f 756e 7472 797c 5965 732f\u00a0 No.|Country|Yes\/<br\/>00000010: 4e6f 0a30 317c 496e 6469 617c 590a 3032\u00a0 No.01|India|Y.02<br\/>00000020: 7c55 537c 590a 3033 7c41 7573 7472 616c\u00a0 |US|Y.03|Austral<br\/>00000030: 6961 7c59 0a0a 3034 7c43 6869 6e61 7c4e\u00a0 ia|Y..04|China|N<br\/>00000040: 0a30 357c 5275 7373 6961 7c59 0a30 367c\u00a0 .05|Russia|Y.06|<br\/>00000050: 4a61 7061 6e7c 590a 0a30 377c 5369 6e67\u00a0 Japan|Y..07|Sing<br\/>00000060: 7061 6f72 657c 590a 3038 7c53 6f75 7468\u00a0 paore|Y.08|South<br\/>00000070: 204b 6f72 6561 7c4e 0a30 397c 4669 6e61\u00a0\u00a0 Korea|N.09|Fina<br\/>00000080: 6c61 6e64 7c59 0a31 307c 4972 656c 616e\u00a0 land|Y.10|Irelan<br\/>00000090: 647c 590a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 d|Y.<\/code><\/pre>\n<h2 id=\"q-how-to-skip-a-few-lines-with-xxd\">Q2. How to skip a few lines with xxd?<\/h2>\n<p>Suppose you don&#8217;t want hexdump of the complete file. Instead, you want the tool to start converting from a specific line. Then this can be achieved using the -s command.<\/p>\n<p>For example, if you want xxd to produce hex dump from line 3 onwards, then here&#8217;s how you tell xxd to do this:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">xxd -s 0x30 test.txt<\/code><\/pre>\n<p>Here&#8217;s the output produced:<\/p>\n<pre><code spellcheck=\"false\">00000030: 6961 7c59 0a0a 3034 7c43 6869 6e61 7c4e\u00a0 ia|Y..04|China|N<br\/>00000040: 0a30 357c 5275 7373 6961 7c59 0a30 367c\u00a0 .05|Russia|Y.06|<br\/>00000050: 4a61 7061 6e7c 590a 0a30 377c 5369 6e67\u00a0 Japan|Y..07|Sing<br\/>00000060: 7061 6f72 657c 590a 3038 7c53 6f75 7468\u00a0 paore|Y.08|South<br\/>00000070: 204b 6f72 6561 7c4e 0a30 397c 4669 6e61\u00a0\u00a0 Korea|N.09|Fina<br\/>00000080: 6c61 6e64 7c59 0a31 307c 4972 656c 616e\u00a0 land|Y.10|Irelan<br\/>00000090: 647c 590a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 d|Y.<\/code><\/pre>\n<p>So you can see the hexdump is produced line 3 onwards in this case.<\/p>\n<h2 id=\"q-how-to-limit-xxd-output-to-a-particular-length\">Q3. How to limit xxd output to a particular length?<\/h2>\n<p>In the previous section, we discussed how to make xxd start converting from a particular point. But there&#8217;s also a way to limitize its conversion to a particular point. This can be done using the -l command line option.<\/p>\n<p>For example, to make sure xxd creates dump for only the first three lines of test.txt, use it in the following way:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">xxd -l 0x30 test.txt<\/code><\/pre>\n<p>Here&#8217;s the output it produced:<\/p>\n<pre><code spellcheck=\"false\">00000000: 4e6f 2e7c 436f 756e 7472 797c 5965 732f\u00a0 No.|Country|Yes\/<br\/>00000010: 4e6f 0a30 317c 496e 6469 617c 590a 3032\u00a0 No.01|India|Y.02<br\/>00000020: 7c55 537c 590a 3033 7c41 7573 7472 616c\u00a0 |US|Y.03|Austral<\/code><\/pre>\n<h2 id=\"q-how-to-set-column-length\">Q4. How to set column length?<\/h2>\n<p>If you want xxd to produce fewer or more columns in output, use the -c option and specify the number of columns there. Here&#8217;s an example command using this option:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">xxd -c 5 test.txt<\/code><\/pre>\n<p>And here&#8217;s the output:<\/p>\n<pre><code spellcheck=\"false\">00000000: 4e6f 2e7c 43\u00a0 No.|C<br\/>00000005: 6f75 6e74 72\u00a0 ountr<br\/>0000000a: 797c 5965 73\u00a0 y|Yes<br\/>0000000f: 2f4e 6f0a 30\u00a0 \/No.0<br\/>00000014: 317c 496e 64\u00a0 1|Ind<br\/>00000019: 6961 7c59 0a\u00a0 ia|Y.<br\/>0000001e: 3032 7c55 53\u00a0 02|US<br\/>00000023: 7c59 0a30 33\u00a0 |Y.03<br\/>00000028: 7c41 7573 74\u00a0 |Aust<br\/>0000002d: 7261 6c69 61\u00a0 ralia<br\/>00000032: 7c59 0a0a 30\u00a0 |Y..0<br\/>00000037: 347c 4368 69\u00a0 4|Chi<br\/>0000003c: 6e61 7c4e 0a\u00a0 na|N.<br\/>00000041: 3035 7c52 75\u00a0 05|Ru<br\/>00000046: 7373 6961 7c\u00a0 ssia|<br\/>0000004b: 590a 3036 7c\u00a0 Y.06|<br\/>00000050: 4a61 7061 6e\u00a0 Japan<br\/>00000055: 7c59 0a0a 30\u00a0 |Y..0<br\/>0000005a: 377c 5369 6e\u00a0 7|Sin<br\/>0000005f: 6770 616f 72\u00a0 gpaor<br\/>00000064: 657c 590a 30\u00a0 e|Y.0<br\/>00000069: 387c 536f 75\u00a0 8|Sou<br\/>0000006e: 7468 204b 6f\u00a0 th Ko<br\/>00000073: 7265 617c 4e\u00a0 rea|N<br\/>00000078: 0a30 397c 46\u00a0 .09|F<br\/>0000007d: 696e 616c 61\u00a0 inala<br\/>00000082: 6e64 7c59 0a\u00a0 nd|Y.<br\/>00000087: 3130 7c49 72\u00a0 10|Ir<br\/>0000008c: 656c 616e 64\u00a0 eland<br\/>00000091: 7c59 0a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |Y.<\/code><\/pre>\n<p>So you can see only 5 columns were produced in output.<\/p>\n<h2 id=\"q-how-to-make-xxd-produce-binary-dump\">Q5. How to make xxd produce binary dump?<\/h2>\n<p>Yes, this is also possible. The -b command line options makes xxd produce dump in binary digits. Here&#8217;s what the man page says about this tool:<\/p>\n<pre><code spellcheck=\"false\">-b | -bits<br\/>Switch to bits (binary digits) dump, rather than hexdump.\u00a0 This option writes octets as eight digits \"1\"s and\u00a0 \"0\"s\u00a0 instead<br\/>of\u00a0 a\u00a0 normal\u00a0 hexadecimal\u00a0 dump. Each line is preceded by a line number in hexadecimal and followed by an ascii (or ebcdic)<br\/>representation. The command line switches -r, -p, -i do not work with this mode.<\/code><\/pre>\n<p>Here&#8217;s an example:<\/p>\n<p><a class=\"fancybox\" id=\"img-xxd-b-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/xxd-b-option.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-xxd-command-tutorial-for-beginners-with-examples.png\" alt=\"\" title=\"\"><\/a><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>So you can see, the xxd command is useful if you deal in hex and binary file contents. Here, in this tutorial, we&#8217;ve just discussed some basic options xxd offers. Once you&#8217;re done practicing these, head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/xxd\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to learn more about it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Need a hex dump for a text file? Looking for a command line utility that can do this? Well, you are in luck, as there exists a command dubbed xxd that can do this for you. In this tutorial, we will discuss xxd using some easy to understand examples. But before we do that, it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7252","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7252"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7252\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}