Passing multiple arguments to Linux commands
Many Linux (*nix) commands accept “-r” or similar arguments to recursively process the contents in a directory; a couple of examples are ls -R or gzip -r. There are some commands, however, that do not have such an option — in these cases, xargs is a great way to pass a listing of files in subdirectories.
For example, if you have a directory full of subdirectories with files and want to use md5sum or sha1sum to create a list of the hashes to verify file integrity (e.g., if you were to burn the directories and files on DVD), piping the contents to md5sum or sha1sum will simply create a hash of that string of list of files. Instead, use something like
find MYDIR -type f -print0 | xargs -0 sha1sum > SHA1SUM
The find command will list only files in the directory “MYDIR” as well as its subdirectories and send them to xargs with each file terminated by the null character. xargs will then take this list (delimited by the null character) and pass them to sha1sum, which then has its output redirected to a file “SHA1SUM”. Later, you can verify the integrity of the files by using sha1sum -c ./SHA1SUM.
Besides checksums, there are many other situations were xargs is useful. As a side note, the reason why I use sha1sum is because the VIA C7 processor has SHA1 support which makes calculating hashes very fast if you use this sha1sum replacement. Otherwise, use of md5sum is usually faster.
Comments are closed.