Overview
Similar to `hexdump` or `od`, `xxd` displays the contents of binary files in hexadecimal and ASCII strings. However, its most significant feature is the **reverse conversion function**, which allows you to convert dumped content back into the original binary file. This feature enables you to modify binary files in hexadecimal form using a text editor and then save them back as binary files using `xxd -r`. This flexibility is very powerful for binary file analysis and modification, especially when used with the `vim` editor.
Key Features
The key features of the `xxd` command are as follows:
- Outputs file contents in hexadecimal and ASCII format.
- Can reverse convert hexadecimal dumps back to original binary files.
- Can be used with general text editors to modify binary files.
- Offers various options for detailed control over output format.
Differences between xxd and hexdump
While `xxd` and `hexdump` have similar hexadecimal dumping capabilities, the biggest difference lies in the presence or absence of the reverse conversion function.
- xxd: Provides a `reverse conversion` feature (`-r`) to convert hexadecimal dumps back to original binaries. Offers strong integration with `vim`.
- hexdump: A tool focused solely on dumping, with no reverse conversion feature.
Key Options
Commonly used `xxd` command options are grouped by purpose.
1) Dump and Reverse Conversion Options
2) Help
Generated command:
Try combining the commands.
Description:
`xxd` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn `xxd` command functionalities through various usage examples.
Hex Dump File Content
echo 'Hello World!' > test.txt
xxd test.txt
Outputs the content of `test.txt` file in hexadecimal and ASCII characters.
Save Hex Dump to File
xxd /bin/ls > ls_dump.txt
Saves the content of the binary file `/bin/ls` to `ls_dump.txt`.
Reverse Convert Dump File to Original
xxd -r ls_dump.txt ls_copy
Converts the previously saved `ls_dump.txt` file back to the original binary file `ls_copy`. **This feature is `xxd`'s most significant characteristic.**
Edit Binary Files with vim
vim file.bin
(vim에서) :%!xxd
(편집)
(vim에서) :%!xxd -r
In `vim`, use `%!xxd` to switch the current file to hexadecimal dump mode, edit it, and then use `%!xxd -r` to revert and save.
Installation
`xxd` is included in the `vim` package. Since `vim-common` or `vim` packages are typically installed by default on most Linux distributions, separate installation is usually not required.
Debian/Ubuntu
sudo apt update
sudo apt install -y vim-common
RHEL/CentOS/Fedora
sudo dnf install -y vim-common
Tips & Cautions
Important points to note when using the `xxd` command are summarized here.
Tips
- The `-r` option of `xxd` can only correctly reverse convert dump files generated by `xxd` itself. It may not work with hexadecimal dump files in other formats.
- Be extremely cautious when modifying binary files. A single incorrect byte can corrupt the entire program.
- When reverse converting, it's safer to always save to a different file name to avoid overwriting the original file. (e.g., `xxd -r old.hex > new.bin`)