Linux 遇到兩個 Binary 檔案,想要比較看看有什麼不一樣,要怎麼做呢?
Linux 如何做 Binary 檔案的 Diff
最基本想要知道有沒有不同,只要做 diff 即可。
- $ diff file1 file2 # 沒有結果就是相同,若不同會出現「二元碼檔 file1 與 file2 不同」
想要看更詳細的不同,我平常比較常用的是 xxd 和 hexdump
xxd
- $ xxd file1 > file1.hex
- $ xxd file2 > file2.hex
- $ diff file1.hex file2.hex
hexdump
- $ hexdump a > a.hex
- $ hexdump b > a.hex
- $ diff a.hex b.hex
- $ vimdiff a.hex b.hex
下述摘錄自此篇:diff - How do I compare binary files in Linux?
- $ diff <(xxd ./new-file.bin) <(xxd ./original-file.img) > diff-part.hex
- $ vimdiff <(xxd ./new-file.bin) <(xxd ./original-file.img)
- $ cmp -l file1.bin file2.bin | gawk '{printf "%08X %02X %02X\n", $1, strtonum(0$2), strtonum(0$3)}'