Tux

...making Linux just a little more fun!

Two-cent Tip: efficient use of "-exec" in find

Mulyadi Santosa [mulyadi.santosa at gmail.com]


Sun, 31 Jan 2010 16:39:55 +0700

Most CLI aficionados use this kind of construct when executing a command over "find" results:

$ find ~ -type f -exec du -k {} \;

Nothing is wrong with that, except that "du" is repeatedly called with single argument (that is the absolute path and the file name itself). Fortunately, there is a way to cut down the number of execution:

$ find ~ -type f -exec du -k {} +

Replacing ";" with "+" would make "find" to work like xargs does. Thus du -k will be examining the size of several files at once per iteration.

PS: Thanks to tuxradar.com for the knowledge!

-- 
regards,
 
Mulyadi Santosa
Freelance Linux trainer and consultant
 
blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Sun, 31 Jan 2010 13:15:02 +0000

On Sun, Jan 31, 2010 at 04:39:55PM +0700, Mulyadi Santosa wrote:

> Most CLI aficionados use this kind of construct when executing a
> command over "find" results:
> 
> $ find ~ -type f -exec du -k {} \;
> 
> Nothing is wrong with that, except that "du" is repeatedly called with
> single argument (that is the absolute path and the file name itself).
> Fortunately, there is a way to cut down the number of execution:
> 
> $ find ~ -type f -exec du -k {} +

https://linuxgazette.net/111/tag/3.html

-- Thomas Adam

-- 
"It was the cruelest game I've ever played and it's played inside my head."
-- "Hush The Warmth", Gorky's Zygotic Mynci.


Top    Back