Home > OS >  How to use xargs with find?
How to use xargs with find?

Time:03-05

I have a large number of files on disk and trying to xargs with find to get faster output. find . -printf '%m %p\n'|sort -nr

If I write find . -printf '%m %p\n'|xargs -0 -P 0 sort -nr, it gives error argument line is too long. Removing -0 option gives other error.

CodePudding user response:

  • The parallelism commands such as xargs or GNU parallel are applicable only if the task can be divided into multiple independent jobs e.g. processing multiple files at once with the same command. It is not possible to use sort command with these parallelism commands.

  • Although sort has --parallel option, it may not work well for piped input. (Not fully evaluated.)

As side notes:

  • The mechanism of xargs is it reads items (filenames in most cases) from the standard input and generates individual commands by combining the argument list (command to be executed) with each item. Then you'll see the syntax .. | xargs .. sort is incorrect because each filename is passed to sort as an argument then sort tries to sort the contents of the file.

  • The -0 option to xargs tells xargs that input items are delimited by a null character instead of a newline. It is useful when the input filenames contain special characters including a newline character. In order to use this feature, you need to coherently handle the piped stream in that way: putting -print0 option to find and -z option to sort. Otherwise the items are wrongly concatenated and will cause argument line is too long error.

CodePudding user response:

Suggesting to use locate command instead of find command.

You might want to update files database with updatedb command.

Read more about locate command here.

  • Related