This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Re:How about xargs?

Posted by: Anonymous Coward on July 11, 2006 08:18 AM

Yes, I have also found that <tt>xargs</tt> is very useful with <tt>find</tt>. The <tt>-type f</tt> restricts find's output to regular files; without that option, the names of all objects are printed, including directories, symlinks, etc. In your example, <tt>-type f</tt> is what you'd want, because <tt>mp3gain</tt> probably doesn't know what to do with directories or other types of objects. Or even worse, it might recursively act on all files within each directory given on its command line, which would cause files to be operated on multiple times! (Of course, in that case, you don't need <tt>find</tt> or <tt>xargs</tt>; just give <tt>mp3gain</tt> the directory name.)

Keep in mind that <tt>xargs</tt> has an idea of the maximum length of a command line, so it might run the command (i.e. <tt>mp3gain</tt>) multiple times. Sometimes that can cause different results than if the command is run only once.

Here is a cute trick when the command is <tt>grep</tt>: If you give that command multiple file names, it prepends each matched line with the file name, which is very useful. But if you're unlucky, it might happen that the last <tt>grep</tt> command is handed only one file name; you wouldn't see the file name for matched lines in that file. So what you can do is give the command as <tt>xargs grep pattern<nobr> <wbr></nobr>/dev/null</tt> which guarantees that <tt>grep</tt> will always be given at least two file names.

-- Larry Ruane

#

Return to CLI Magic: Searching with find