Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Table of Contents

Introduction

Is a very useful program to take a list and run commands against that list. xargs will take a list of arguments, loop through them and run a command against 1 or more arguments, one at a time.

Tip

If you like xargs you might want to check out GNU Parallel.

Basic Example

Here is a really straightforward non-destructive example of using xargs to calculate a MD5 hash on every file in the current directory,

...

Code Block
languagebash
md5sum planetary.doc
ab5970d50d67bcafe5c554387f76534e = planetary.doc
md5sum Superman.jpg
cdefa50d737dfcf8dc57886ea1a758c4 = Superman.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,

...

Note

One item I don't understand yet is why {} forces arguments to be iterated through one at a time. Also, how would we allow more than one argument? -n2 will not work.

Debugging xargs with echo

The echo command is useful to test and see what xargs will be looping through,

...

Notice that using echo I omit the -t but you will want to put the -t back when you are actually executing your command.

Dealing with Special Characters

When using xargs it will not work with special characters like apostrophe in file names. To get around this limitation use the find command's -print0 option in combination with -0,

Code Block
languagebash
find . -print0 | xargs -0 -I{} echo {}

Note using xarg's -0 "The GNU find -print0 option produces input suitable for this mode." it probably is because it is meant to be used together.

Useful Applications of xargs

Search - ...