Formatting output of xxd
Skipping n bytes from start (-s offset)
The xxd command allows you to specify an offset, effectively skipping a certain number of lines or bytes in the input file when generating a hexadecimal dump.
xxd -s [offset] [file path]
Here, xxd skipped 5 bytes of the word ‘Hello’ and generated a hexadecimal dump for the remaining text.
Display last n bytes (-s -offset)
The “xxd -s” option allows you to specify an offset or skip a certain number of bytes at the beginning of a file when generating a hexadecimal dump.
xxd -s -[number of bytes] [file path]
By using the ‘-‘ operator in the offset, the resulting hexadecimal dump is generated for the last 5 bytes of the file.
Display first n bytes (-l)
The “xxd -l” option permits you to limit the number of bytes processed or displayed when generating a hexadecimal dump.
xxd -l [number of bytes] [file path]
We are generating a hex dump of the the first 20 bytes (0x14 is hexadecimal equivalent of 20).
Skipping n output lines
Here’s another example where we skip lines in the hexadecimal dump by specifying offsets like 0x10 (which is equivalent to 16 in decimal) and 0x20 (equivalent to 32 in decimal). It’s the same as skipping bytes, we are just omitting the display of 16 bytes of data per skipped line.
Number of octets (grouped bytes) per group (-g)
The -g option allows us to organize the output by grouping a certain number of bytes.
xxd -g [number of octets] [file path]
Here, we have generated a hexadecimal dump with 4 bytes per group.
Limiting the number of columns (-c)
We can use the -c option to limit the number of columns per line in the output dump.
xxd -c [number of columns] [file path]
Here, we are limiting the output to 5 columns per line, you can use 0x05 instead of 5 to get the same output.
Note: Other than using hexadecimal as input for options, you can use other formats like decimal and octal too.
For example: To display the first 20 bytes of the file the commands can be:
Here, the number of bytes is 20 in decimal, 0x14 in hexadecimal, and 024 in octal.
xxd Command in Linux
xxd is a command-line tool that is primarily used for creating and analyzing hexadecimal dumps from files. It can also be used to reverse the process and convert a hexadecimal dump back into binary form. In this article, let’s uncover the practical applications of the “xxd” command in the Linux ecosystem.
xxd Command in Linux
- What is a ‘hexadecimal dump’?
- Installing xxd on Linux
- Using xxd command:
- Generating hexadecimal dumps
- Converting hexadecimal dump to the original file
- Formatting output of xxd
- Different dump output styles
- Embedding Binary Data in C/C++ Code (xxd -i)