I have a file (call it A, for reference) that may be a fragment of some other extant file on my system. I can't use cmp because I don't know how many bytes may be missing from the start of A (or, at least, I can't use it without brute forcing through the -i flag). Is there a way for me to discover whether A is already existent on my system (using GNU tools, or any other linux program)? Or will I have to botch together a c++ program to do the job? Note: efficiency is desirable since the files that A has to be compared with may be numerous.


Not knowing how big the chunk (A) is ... have you considered grep'ing for its content using -a?

  • echo -e "\x00\x11\x22\x33" > file; echo -e "\x00\x11" > snip; grep -a snip file doesn't seem to work – daisy Oct 15 '12 at 23:59
  • No, I hadn't considered grep! That'll do. – Zorawar Oct 16 '12 at 0:09
  • @warl0ck: grep needs a pattern, so try grep -a "$(cat snip)" file. But, shouldn't there be a problem with echo's newlines? Your example should fail regardless of this correction, shouldn't it? On my system it succeeds... – Zorawar Oct 16 '12 at 0:11
  • In fact, why use grep -a at all? Can't I just use grep? Taking warl0ck's example, echo -e "\x00\x11\x22\x33" > file; echo -e "\x00\x11Surplus Gibberish" > snip; grep -a "$(cat snip)" file succeeds. Is that expected behaviour? If I remove the -a flag, then the command fails, as I'd want it to. – Zorawar Oct 16 '12 at 0:25
  • I'd be surprised if that worked. Neither of snip or file is wholly included in the other in your example; I wouldn't expect a match, no matter what method of invocation. – tink Oct 16 '12 at 0:38

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.