Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Do these three commands do the same thing?

  • A command that uses grep.

    grep "a" -r .
    
  • A command that uses find.

    find .  -exec grep "a" {} \;
    
  • A command that uses a grep on a find through xargs.

    find . | xargs grep "a"
    
share|improve this question
1  
Welcome to Super User, we expect users to do some homework before asking their questions. Have you looked at the man pages of grep, find and xargs? Have you tried the commands to see if their results were identical? Have you tried to note the differences between them? Why should we do this for you if it's simple enough for you to do this yourself? There is not really an actual problem presented here so there is nothing that we can solve... –  Tom Wijsman Aug 18 '11 at 15:36
    
kim sounds like a girl's name, maybe cut her some slack? ;-) anyhow, first thing h/she should do is try them, e.g. on cygwin which h/she can put on their windows machine. It's a fairly interesting question though even if h/she hasn't tried it yet. These are commands that many here aren't that familiar with, and might learn something, me included! –  barlop Aug 18 '11 at 19:26
    
@barlop - We should definitly not treat people different based on their gender. That said, I agree with the rest of your comment. Especially the last sentence. –  Nifle Aug 25 '11 at 9:59

3 Answers 3

up vote 2 down vote accepted

They aren't the same and each one have problems.

  • The first one is non portable as it uses a Gnu extension. Moreover, options are expected to be before the pattern, not after it.
  • The second one doesn't show the file name, as already stated.
  • The third one will fail if file names have embedded spaces or similar.

I would use instead:

find .  -type f -exec grep a /dev/null {} +

It is portable, it ignores non regular files, it won't clash with weird file names and will always show file names when the pattern is found.

share|improve this answer
    
that is interesting, how do you know/look up that -r is a GNU extension? where is the pure grep no GNU extensions? –  barlop Aug 18 '11 at 22:54
1  
The Unix utilities standard is defined by IEEE & POSIX: pubs.opengroup.org/onlinepubs/009695399/mindex.html. Its grep manual page states the standard options: pubs.opengroup.org/onlinepubs/009695399/utilities/grep.html –  jlliagre Aug 19 '11 at 4:39

Is this a homework question?

Did you try them?

They do slightly different things.

For example, if you feed grep a single filename it does not echo the filename on output. This can be frustrating when using grep with find.

share|improve this answer
    
"if you feed grep a single filename it does not echo the filename on output. This can be frustrating when using grep with find" grep -H will though. –  barlop Aug 18 '11 at 22:52
1  
@barlop: that's a good tip. Unfortunately not all Unixes have a version of grep that supports the -H option :-( –  RedGrittyBrick Aug 18 '11 at 23:18
1  
Yes, -H is also a Gnu extension. –  jlliagre Aug 19 '11 at 4:41

You should better define "do the same thing". The first one runs one command, the second runs one find and fork+exec's grep once per file found, and the third runs a minimum of three commands - or more if too many files are found to fit onto one command line. As far as CPU/memory impact goes, they most distinctly do not do the same thing - the difference between 1, three, and "a whole bunch" of processes is significant.

From a filesystem perspective, the filesystem is traversed, each file is stat'd, and then opened, fully read, and closed. So they all do the same thing from that perspective, and the filesystem doesn't notice a difference (aside from perhaps slower traversal in the second instance due to the overhead from forking a bazillion processes).

The output genreated to the screen differs, and that difference can be determined empirically buy just running the commands with a few different possible directory structures (one obvious way is that which redgrittybrick mentioned).

They consume different amounts of time to type, due to the number of differenc characters and possibilities for syntax errors.

And so on. There's lots of ways things can "differ". :)

share|improve this answer

Your Answer

 
discard

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.