Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 0 down vote accepted

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

share|improve this answer
echo -e "\x00\x11\x22\x33" > file; echo -e "\x00\x11" > snip; grep -a snip file doesn't seem to work – warl0ck 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 posting your answer, you agree to the privacy policy and terms of service.

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