xxd Command in Linux

Learn via video courses
Topics Covered

Overview

In the realm of Linux, the 'xxd command in Linux' stands as a powerful utility that creates a hex dump of a given file. It is capable of displaying file data in a human-readable format, thus playing an instrumental role in debugging efforts. This guide explores the usage of the xxd command in Linux, outlining its syntax, options, and various applications.

Syntax of xxd Command in Linux

The basic syntax of the xxd command in Linux is as follows:

Where:

  • options: These are the flags or switches used with the command that modify its behavior. They are optional.
  • infile: This is the input file which will be converted to a hex dump. If it's omitted, xxd reads from the standard input.
  • outfile: This is the file where the output of the command will be stored. If it's omitted, xxd writes to the standard output.

Options in xxd Command in Linux

  1. -r, -revert: This option converts the hex dump back to binary.

    For example -

    Output:

    This command converts a hexadecimal string back into its plain text equivalent.

  2. -p, -ps, -postscript, -plain: Produces plain hexdump style output.

    For example -

    Output:

    The command translates the plain text 'Hello' into hexadecimal.

  3. -c, -cols : This option lets you specify the number of columns for the output format.

    For example -

    Output:

    This command uses the '-c' flag to specify that each byte should be on a new line.

Example Usages

  • Basic usage of xxd command.:

    Output:

    Explanation: The command takes the string 'Hello World!' as input and displays the hex representation along with the ASCII output.

  • Reverse operation with xxd command.:

    Output:

    Explanation: The command takes a hex string as input and reverses it back to ASCII.

Tips

  • Remember, if you do not specify an output file, the xxd command in Linux will print to the standard output (usually your terminal).

  • To view binary files in a human-readable format, use xxd. It's extremely useful for debugging binaries.

Advanced Use Cases of xxd Command in Linux

  • Displaying binary code of a file.:

    Output:

    Explanation: This command reads the content of the specified file and displays it in binary format.

  • Limit the number of bytes to display.:

    Output:

    Explanation: This command displays the first 100 bytes of the specified file.

  • Displaying the output in a single line.:

    Output:

    Explanation: This command displays the output in plain hexdump style with 200 columns, effectively displaying all output in a single line.

Conclusion

  • The 'xxd command in Linux' is a versatile tool used to create a hex dump of a given file.

  • This command is often used for debugging and can convert a hex dump back into binary.

  • Options like -r, -p, and -c, among others, allow you to fine-tune the command's output.

  • Advanced usage includes limiting byte display and displaying file content in binary.