'How to use xargs with find?
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.
Solution 1:[1]
The parallelism commands such as
xargsorGNU parallelare 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 usesortcommand with these parallelism commands.Although
sorthas--paralleloption, it may not work well for piped input. (Not fully evaluated.)
As side notes:
The mechanism of
xargsis 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 .. sortis incorrect because each filename is passed tosortas an argument thensorttries to sort the contents of the file.The
-0option toxargstells 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-print0option tofindand-zoption tosort. Otherwise the items are wrongly concatenated and will causeargument line is too longerror.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tshiono |
| Solution 2 | Dudi Boy |
