xargs
xargs
is a command-line utility used to build and execute command lines from standard input. It is commonly used to pass a large number of arguments to a command that does not accept them directly.
Basic Syntax
Commonly Used Options
-0
: Input items are terminated by a null character instead of whitespace.-a file
: Read items fromfile
instead of standard input.-d delim
: Input items are terminated by the specified character.-E eof-str
: Set the end-of-file string toeof-str
.-I replace-str
: Replace occurrences ofreplace-str
in the initial arguments with input items.-L max-lines
: Use at mostmax-lines
non-blank input lines per command line.-n max-args
: Use at mostmax-args
arguments per command line.-P max-procs
: Run up tomax-procs
processes at a time.-p
: Prompt the user before running each command line.-r
: Do not run the command if the input is empty.-s max-chars
: Use at mostmax-chars
characters per command line.-t
: Print the command line on standard error before executing it.
Examples
Basic Usage
Pass a list of files to the rm
command:
Using -n
Option
Run the command with at most 2 arguments at a time:
Using -I
Option
Replace occurrences of {}
with the input items:
Using -P
Option (1)
Run up to 4 processes at a time:
Using -0
Option
Handle input items separated by null characters (useful with find -print0
):
Using -a
Option
Read items from a file:
Using -d
Option
Specify a custom delimiter:
Using -L
Option
Use at most 1 non-blank input line per command line:
Using -p
Option (2)
Prompt the user before running each command line:
Using -t
Option
Print the command line before executing it:
Conclusion
xargs
is a versatile tool for building and executing command lines from standard input. Understanding its options allows for efficient and powerful command-line operations.