As a matter of fact, i detest having to learn more than one method to achieve a job when it comes to shell scripting. But most of the time, sysadmins should find their needs to be met in the best way.
Find has the -exec option to perform actions on the files that are found. It is a common way of deleting unnecessary files without xargs.
$ find . -name "*.tmp" -type f -exec rm -f {} \;
In the above example "{}" is safe to substitute for every file with a space in its name. But "rm" command is executed once for every single file that is found. If we think about tons of files to be removed then a lot of fork processes are likely to take place.
How about using xargs:
$ find . -name "*.tmp" -type f -print0 | xargs -0 -r rm -f
With xargs, "rm" will be executed once for all files, decreasing overhead of the fork. It would be safe to use "-print0" option for files with space. Xargs "-r" option is for not running if stdin is empty. Of course there is a limit for the argument list xargs can have at a time. Otherwise xargs will split the input and try to execute the command repeatedly. With "-s" flag this limit can be overriden.
Comments
Post a Comment