Xargs is great. If you want to know why use xargs over exec, have a read of this: xargs versus exec.
But, there is one problem. If the command that you are trying to execute on the results from find cannot take more than one file as param (basename for e.g, can take only one file), then use the -n 1 param with xargs (to process one file at a time) . In that case, xargs is probably going to be as quick as using exec.
find . -name "*.rar" | xargs -n 1 unrar e
find . -name "*.rar" | xargs -n 1 basename
And this will get the basename without using the basename function (I go it from this page).
find . -name "*.rar" -printf "%fn"
2 responses to “Unix find, xargs and basename.”
Your use of xargs can lead to nasty surprises because of the separator problem http://en.wikipedia.org/wiki/Xargs#The_separator_problem
GNU Parallel http://www.gnu.org/software/parallel/ does not have that problem. GNU Parallel also makes it possible to run small scripts. Try this:
ls *rar | parallel ‘mkdir {.}; cd {.}; unrar e ../{}’
Watch the intro video for GNU Parallel: http://www.youtube.com/watch?v=OpaiGYxkSuQ
Thanks for the reply Ole! I was trying to keep the example simple. There is obviously a fix for the separator problem when using find + xargs:
find . -name “*.rar” -print0 | xargs -0 -n 1 unrar e