Unix find, xargs and basename.

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.”

  1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *