Linux Tip of the Day - Power of XARGS

January 18, 2008 by Adrian
Filed under: Linux 

 

Don’t over look the power of xargs. By default in some flavors of unix, the command line is limited to roughly 20K bytes.

Some directory lists can well exceed that. Secondly, performance using

% find /tmp -name foo -exec rm \;

will fork the rm command for every file it finds. If this were to return 1000s of files you get rm forked 1000s of times.

But,

% find /tmp -name foo |xargs rm

while getting the same effect, rm is forked with a long list of files, and thus system overhead is reduced, and the command runs faster, and system impact is reduced.

This is also important if you have list of files produced by

% find {blah} > listfile

Once you edit your list to remove those files you really want to keep, then

% cat listfile| xargs rm

Will remove all the files in the file. While
% rm $(cat listfile) will

Fail if listfile is greater than ~20K

Comments

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!