Difference between revisions of "Find notes"

From Noah.org
Jump to navigationJump to search
Line 1: Line 1:
 
[[Category:Engineering]]
 
[[Category:Engineering]]
 +
 +
== exec versus xargs ==
 
You see some people pipe find output into xargs, but other people start a command using -exec.
 
You see some people pipe find output into xargs, but other people start a command using -exec.
 
You wonder what the difference is. The difference is that xargs is faster. It will intelligently group
 
You wonder what the difference is. The difference is that xargs is faster. It will intelligently group
Line 10: Line 12:
  
 
You can always do it in a shell loop too:
 
You can always do it in a shell loop too:
 +
 +
<pre>
 
   for filename in *.png ; do convert $filename `basename $filename .png`.jpg; done
 
   for filename in *.png ; do convert $filename `basename $filename .png`.jpg; done
 +
</pre>
 +
 +
== List all extensions in the current directory ==
 +
 +
This came in handy when I was trying to find out exactly what mime-types I need to care about.
 +
 +
<pre>
 +
find . | grep -v .svn | xargs -L 1 basename | sed -e "s/.*\(\\.\\s*\)/\\1/" | sort | uniq
 +
</pre>

Revision as of 12:14, 15 June 2007


exec versus xargs

You see some people pipe find output into xargs, but other people start a command using -exec. You wonder what the difference is. The difference is that xargs is faster. It will intelligently group arguments and feed batches to the subcommand, so it doesn't have to start a new instance of the subcommand for every argument.

Generally I find -exec easier to use because you can easily repeat the found filename in the exec argument. It's easier for me to express exactly what I want to be executed. Of course, some people think the find syntax is wacky. The xargs command comes in handy in other places such as a stream not generated by find, but when using find I stick with -exec unless I have a good reason not to.

You can always do it in a shell loop too:

  for filename in *.png ; do convert $filename `basename $filename .png`.jpg; done

List all extensions in the current directory

This came in handy when I was trying to find out exactly what mime-types I need to care about.

find . | grep -v .svn | xargs -L 1 basename | sed -e "s/.*\(\\.\\s*\)/\\1/" | sort | uniq