find -exec cmd {} + vs | xargs

dogbane :

Which one is more efficient over a very large set of files and should be used?

find . -exec cmd {} +

or

find . | xargs cmd

(Assume that there are no funny characters in the filenames)

Tometzky :

Speed difference will be insignificant.

But you have to make sure that:

  1. Your script will not assume that no file will have space, tab, etc in file name; the first version is safe, the second is not.

  2. Your script will not treat a file starting with "-" as an option.

So your code should look like this:

find . -exec cmd -option1 -option2 -- {} +

or

find . -print0 | xargs -0 cmd -option1 -option2 --

The first version is shorter and easier to write as you can ignore 1, but the second version is more portable and safe, as "-exec cmd {} +" is a relatively new option in GNU findutils (since 2005, lots of running systems will not have it yet) and it was buggy recently. Also lots of people do not know this "-exec cmd {} +", as you can see from other answers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

find -exec + vs find | xargs: which one to choose?

find: -exec vs xargs (aka Why does "find | xargs basename" break?)

-exec to xargs conversion in find command

Is there any difference between -exec cmd {} and xargs -I?

What is the difference between find with -exec and xargs?

Why "xargs find | xargs mv" and "xargs find -exec mv" work but produce error printouts "No such file or directory"?

Are there limits to file length piped from find to xargs or using find -exec +

cmd2 `cmd1` vs cmd1 | xargs cmd2

Different behaviors between find -exec and piping through xargs

Blank lines when executing "grep | xargs" in a "find -exec"

Why does 'find -exec cmd {} +' need to end in '{} +'?

xargs: exec command with prompt

Using semicolon (;) vs plus (+) with exec in find

`{} {}some` vs`{} {,some}` in the `-exec` option of the `find` command

Does GNU find protect against finding a file multiple times if files are renamed with xargs or -exec?

Iterate over find command's output with exec or xargs & pass this as paramater to script

How to copy files from a folder using the find command with the -exec action, or with xargs, to another folder

How to use Xargs in TCL with Exec

xargs how to put result {} into $(cmd {})?

xargs how to put result {} into $(cmd {})?

Mongoose find vs exec. How to return values

find: "-exec rm {} \;" vs. "-delete" - why is the former widely recommended?

xargs -r0 vs xargs -0

Using xargs with find

Running find and xargs in background

Problems with find, xargs and egrep

Argument list too long for xargs/exec

Running `bash` using docker exec with xargs command

node.js & mongodb: await collection.find vs. collection.find.exec for async