3

I have 2 Binary Files FileA and FileC

Its is such that FileC = FileA + FileB using the cat utility

How do I subtract FileA from FileC to get FileB ?

PS: I am using Ubuntu Oneiric

Gautam
  • 2,335
  • 5
  • 18
  • 18

2 Answers2

3

Assuling you have stat on your plateform to get the size of FileA, you could do something like:

dd if=./FileC of=./FileB bs=1 skip=$(stat -c %s ./FileA)

which should work on any type of file.

Mat
  • 52,586
  • This works assuming that content of FileA was put or concatenated into FileC before FileB. But works for me, because its me who will put FileA before FileB into FileC. +1 :-) – Nikhil Mulley Jan 14 '12 at 13:15
1

You need to know where to cut. For binary files, this generally means knowing the size of FileA or FileB.

You can find the size of FileA with ls -l. If you need to write a portable script, you can extract the size with ls -lgo FileA | awk '{print $3; exit}' (or, for non-POSIX-compliant versions of ls that don't have the -g and -o options, ls -l FileA | awk '{print $5; exit}'). On non-embedded Linux, a simpler way to obtain the size is stat -c %s FileA.

Once you have the size, you can use tail to extract the second part of the file:

tail -c +$((sizeA + 1)) <FileC

If you want to break a file into equal chunks, use the split command.