Command-line to reverse byte order/change endianess

You could use objcopy:

$ objcopy -I binary -O binary --reverse-bytes=num inputfile.bin outputfile.bin

where num is either 2 or 4.


Resorted to Perl in the end. Used a one-liner which I found at PERL One Liners:

tail -c 8 file | perl -0777e 'print scalar reverse <>' | od -t d8

The 0777 separator char was a bit puzzling to me, but this page at debian admin seems to suggest that it is a placeholder for 'no record separator', triggering a complete reverse byte-per byte.

Other suggestions are welcome.

EDIT: Found another command in a comment to tac.c, which I downloaded from GNU coreutils:

Copy each FILE, or the standard input if none are given or when a FILE name of "-" is encountered, to the standard output with the order of the records reversed. The records are separated by instances of a string, or a newline if none is given. By default, the separator string is attached to the end of the record that it follows in the file.

Options: -b, --before The separator is attached to the beginning of the record that it precedes in the file. -r, --regex The separator is a regular expression. -s, --separator=separator Use SEPARATOR as the record separator.

To reverse a file byte by byte, use (in bash, ksh, or sh): tac -r -s '.\| ' file


Used dd, Luke!

dd if=sourcefile of=resultfile conv=swab