Pete mentioned:
> 
> there's also something called xargs, which i never learned how to use.
> maybe someone on the list will explain how to use it.

xargs is pretty neat.  (Mike Simons taught me about it)

Instead of doing something like this:

  $ find . -name "*.html" -exec grep -i -l yahoo.com {} \;

(List (-l) all files named "[something].html" which contain the string
"yahoo.com", regardless of capitalization (-i))

Which would be the same as doing:

  $ grep -i -l yahoo.com ./foo.html
  $ grep -i -l yahoo.com ./bar.html
  $ grep -i -l yahoo.com ./baz.html
  $ grep -i -l yahoo.com ./index.html
  $ grep -i -l yahoo.com ./links.html
  $ grep -i -l yahoo.com ./users/bill/index.html
  $ grep -i -l yahoo.com ./users/pete/search.html
  ... etc.


... it can be more efficient to use xargs, like so:

  $ find . -name "*.html" | xargs grep -i -l yahoo.com


Notice here we're using "|" (to take the output of find, in this case,
just a list of filenames) to have the shell spawn the "grep" command,
instead of using "-exec" to have "find" run "grep" numerous times.


What xargs does is read from stdin (which in this case will be a list
of filenames :) ) and then take a considerable chunk of it (you can
specify how many with "-n max-args") and send it to the command you
specify.

In the above case, it will run simply this one command:

  $ grep -i -l yahoo.com ./foo.html ./bar.html ./baz.html ./index.html \
  ? ./links.html ./users/bill/index.html ./users/pete/search.html \
  ... etc.


This is useful if you don't like forking a gazillion times. :)



You can also use "locate" to pump filenames to xargs...


  locate .sh | xargs ls -l | cut -b 1-3,15-24,56-999


This will show all files with ".sh" in the filename (that 'locate' can show
you; ie, you can see them as whatever user you are, and they were in the
filesystem when "updatedb" was last run (typically, 4am today)).

Then I use "ls -l" to show some details about them, but rather than run
"ls -l" on each file (like you would with "find ... -exec ..."), we
use "xargs" to send a whole boatload of them to "ls".

Finally, for fun, I use "cut" to grab characters 1, 2, 3, 15-24, and 56-...
so we get the owner permissions, owner name, and filename of each file.

Fun fun!


-bill!

Reply via email to