page

Jul 23, 2021

xargs command in linux ; xargs vs exec

 Linux and Unix xargs command tutorial with examples

https://shapeshed.com/unix-xargs/

 

xargs
 - command line for building an execution pipeline from standard input.

-  reads items from standard input as separated by blanks and executes a command once for each argument. 

echo 'one two three' | xargs mkdir
ls
one two three
 

xargs vs exec 

The find command supports the -exec option that allows arbitrary commands to be performed on found files. The following are equivalent.

find ./foo -type f -name "*.txt" -exec rm {} \; 

find ./foo -type f -name "*.txt" | xargs rm

So which one is faster? Let’s compare a folder with 1000 files in it.

time find . -type f -name "*.txt" -exec rm {} \; 0.35s user 0.11s system 99% cpu 0.467 total

 time find ./foo -type f -name "*.txt" | xargs rm 0.00s user 0.01s system 75% cpu 0.016 total

Clearly using xargs is far more efficient. In fact several benchmarks suggest using xargs over exec {} is six times more efficient.

 

No comments:

Post a Comment