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
   --arg-file=file
   -a file
            Read items from file instead of standard input.
            If you use this option, stdin remains unchanged when commands are run.
            Otherwise, stdin is redirected from /dev/null. 

   -0
   --null
            Expect filenames to be terminated by NULL instead of whitespace, and the quotes and
            backslash are not special (every character is taken literally).
            Disables the end of file string, which is treated like any other argument.
            Useful when input items might contain white space, quote marks, or backslashes.
            The GNU find -print0 option produces input suitable for this mode. 

   --delimiter=delim
   -d delim
            Input items are terminated by the specified character.
            Quotes and backslash are not special; every character in the input is taken literally. 
            Disables the end-of-file string, which is treated like any other argument.
            This can be used when the input consists of just newline-separated items,
            although it is almost always better to design your program to use --null where this
            is possible. The specified delimiter can be a single character, a C-style character
            escape such as \n, or an octal or hexadecimal escape code.
            Octal and hexadecimal escape codes are understood as for the printf command.
            Multibyte characters are not supported. 

   -E eof-str
            Set the end of file string to eof-str. If the end of file string occurs as a line of input,
            the rest of the input is ignored. If neither -E nor -e is used, no end of file string is used.
   --eof[=eof-str]
    -e[eof-str]
            This option is a synonym for the -E option. Use -E instead, because it is POSIX compliant
            while this option is not. If eof-str is omitted, there is no end of file string. 
            If neither -E nor -e is used, no end of file string is used. 

   --help
            Print a summary of the options to xargs and then exit.
   -I replace-str
            Replace occurrences of replace-str in the initial-arguments with names read from standard
            input. Also, unquoted blanks do not terminate input items; instead the separator is the
            newline character. Implies -x and -L 1.

   --replace[=replace-str]
    -i[replace-str]     This option is a synonym for -I replace-str if replace-str is specified, and
                        for -I{} otherwise. This option is deprecated; use -I instead.

   -L max-lines
            Use at most max-lines nonblank input lines per command line.
            Trailing blanks cause an input line to be logically continued on the next input line.
            Implies -x.

   --max-lines[=max-lines]
    -l[max-lines]
            Synonym for the -L option. Unlike -L, the max-lines argument is optional.
            If max-lines is not specified, it defaults to one. The -l option is deprecated since the
            POSIX standard specifies -L instead.

   --max-args=max-args
    -n max-args
            Use at most max-args arguments per command line. Fewer than max-args arguments will be used
            if the size (see the -s option) is exceeded, unless the -x option is given, in which case
            xargs will exit. 

   -p
   --interactive
            Prompt for confirmation before running each command line.
            Only run the command line if the response starts with 'y' or 'Y'. 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.
            Use the -n option with -P; otherwise chances are that only one exec will be done. 

   -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, including the command and
            initial-arguments and the terminating nulls at the ends of the argument strings.
            The largest allowed value is system-dependent, and is calculated as the argument length
            limit for exec, less the size of your environment, less 2048 bytes of headroom.
            If this value is more than 128KiB, 128Kib is used as the default value; otherwise, the
            default value is the maximum. 1KiB is 1024 bytes. 

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

   -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.

Exit Status

xargs exits with the following status:

0 if it succeeds (all invocations of command return exit status 0)
123 if any invocation of the command exited with status 1-125
124 if the command exited with status 255
125 if the command is killed by a signal
126 if the command cannot be run
127 if the command is not found 1 if some other error occurred.
Exit codes greater than 128 are used by the shell to indicate that a program died due to a fatal signal.

Examples

Find all the .mp3 files in the music folder and pass to the ls command, -print0 is required if any filenames contain whitespace.:

$ 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

Read a list of filenames from filelist.txt (one per line) and copy them to new_folder:

$ xargs -a filelist.txt cp -t new_folder

Move files in olddir to newdir, showing each command:

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

“The other night I ate at a real nice family restaurant. Every table had an argument going” ~ George Carlin

Related Linux commands

env - Display, set, or remove environment variables.
Julia Evans comic explaining the basics of xargs.


 
Copyright © 1999-2024 SS64.com
Some rights reserved