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

  • they are binary files, so it can get hard. If it is a text file, it is possible to use little bit of grep or perl stuff to get back fileB. – Nikhil Mulley Jan 14 '12 at 12:34
  • As @Nikhil said, it would be possible with a regular file to treat them as sets and do set operations on them. – user13742 Jan 14 '12 at 12:41

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.

  • 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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.