Home Linux
 Bash Syntax

xargs

Execute a command, passing constructed argument list(s). The arguments are typically a long list of filenames (generated by ls or find) that are passed to xargs via a pipe.

Syntax
      xargs [options] [command]

Options
   -0
   --null
            Expect filenames to be terminated by NULL instead of whitespace.
            Do not treat quotes or backslashes specially.

   -e[string]
   -E[string]
   --eof[=string]
            Set EOF to _ or, if specified, to string.

   --help
            Print a summary of the options to xargs and then exit.

   -i[string]
   -I[string]
   --replace[=string]
            Edit all occurrences of , or string, to the names read in
            on standard input. Unquoted blanks are not considered argument terminators.
            Implies -x and -l 1.

   -l[lines]
   -L[lines]
   --max-lines[=lines]
            Allow no more than 1, or lines, nonblank input lines on the command line.
            Implies -x.

   -n args
   --max-args=args
            Allow no more than args arguments on the command line.
            May be overridden by -s.

   -p
   --interactive
            Prompt for confirmation before running each command line. Implies -t.

   -P max
   --max-procs=max
            Allow no more than max processes to run at once.
            The default is 1. A maximum of 0 allows as many as possible to run at once.

   -r
   --no-run-if-empty
            Do not run command if standard input contains only blanks.

   -s max
   --max-chars=max
            Allow no more than max characters per command line.

   -t
   --verbose
            Print the command line (on standard error) before executing.

   -x
   --exit
            If the maximum size (as specified by -s) is exceeded, exit.

   --version
            Print the version number of xargs and then exit.

xargs can execute the command supplying some initial arguments directly, and reading the remaining arguments from standard input (or piped input).

xargs passes arguments to command in several bundles, this allows command to process more arguments than it could normally handle at once.

Arguments in the standard input must be separated by unquoted blank characters, or unescaped blank characters or newline characters.
Characters can be quoted by enclosing them in "double-quotes" (non-double-quote and non-newline chars only).
Characters can be quoted by enclosing them in 'apostrophes' (non-apostrophe and non-newline chars only).
Any unquoted character can be escaped by preceding it with a backslash.

e.g. file1 file2 "file three" 'file four' file\ five

If command is omitted then the equivalent of /bin/echo is used.

If all invocations of command return exit status 0 then xargs will return 0, an error of 127=command not found.

Examples

Find all the .mp3 files in the music folder and pass to the ls command

   find ./music -name "*.mp3" -print0 | xargs -0 ls

Find all files in the work folder, pass to grep and search for profit:

   find ./work -print | xargs grep "profit" 

Find and delete files which have been modified in the last 30 minutes:

  find ./work -mmin -30 | xargs -0 rm

Delete all files from the work directory:

  find ./work -print0 | xargs -0 rm

(Use this when rm on a large directory gives: Argument list too long)

Run diff on file pairs (e.g., f1.a and f1.b, f2.a and f2.b ...):

   echo $* | xargs -n2 diff

The previous line would be invoked as a shell script, specifying filenames as arguments.

Display file, one word per line (same as deroff -w):

   cat file | xargs -n1 

Move files in olddir to newdir, showing each command:

   ls olddir | xargs -i -t mv olddir/ newdir/

Related Linux Bash commands:

env - Display, set, or remove environment variables



Back to the Top

Simon Sheppard
SS64.com