Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Made {} right beside -I. Makes more sense visually to me. Fixed md5 to md5sum (md5 is for the mac)

...

Code Block
languagebash
ls | xargs -t -n1 md5md5sum

This is how it works,

  • -t will show you what xargs is about to execute before it executes it.
  • -n1 specifies that xarg work with he arguments passed by the directory to x arguments at a time, in this case one argument at a time.xargs will by default take the output of ls one line at a time and append it to the end of the command

Thanks to the -t the output will be shown on screen,

Code Block
languagebash
md5md5sum planetary.doc
MD5ab5970d50d67bcafe5c554387f76534e (bash) = ab5970d50d67bcafe5c554387f76534eplanetary.doc
md5md5sum Superman.jpg
MD5cdefa50d737dfcf8dc57886ea1a758c4 (cat) = cdefa50d737dfcf8dc57886ea1a758c4Superman.jpg

Substitution to Rename Files

Now let's get more advanced and use -I to allow substitution and explicitly set the location of what xargs receives. First we'll create a some temporary files,

...

Code Block
languagebash
ls | xargs -t -I {} mv {} {}.txt
mv file1 file1.txt
mv file2 file2.txt
mv file3 file3.txt

...

Code Block
languagebash
ls | xargs -t -IIvarX varX md5 varX
md5 file1.txt
MD5 (file1.txt) = d41d8cd98f00b204e9800998ecf8427e
md5 file2.txt
MD5 (file2.txt) = d41d8cd98f00b204e9800998ecf8427e
md5 file3.txt
MD5 (file3.txt) = d41d8cd98f00b204e9800998ecf8427e

...

Code Block
languagebash
ls | xargs -I {} echo "mv {} {}.txt"
mv file1 file1.txt
mv file2 file2.txt
mv file3 file3.txt

...