xargs

build and execute cmd line[s]

Mac OS Sequoia is distributed with
xargs having fewer options

xargs [cmd [args]] STDIN-args
[-i[replace-str]] [--replace[=replace-str]]
[-l[max-lines]] [--max-lines[=max-lines]]
[-s max-chars] [--max-chars=max-chars]
[-n max-args] [--max-args=max-args]
[-r ] [--no-run-if-empty]
[-e[eof-str]] [--eof[=eof-str]]
[-p ] [--interactive]
[-t ] [--verbose]
[-0] [--null]
[-P max-procs] [--max-procs=max-procs ]
[--help] [--version]
[-x] [--exit]

  1. reads STDIN-args (frequently from find) from standard input
  2. constructs a command with common-args followed by STDIN-args
  3. executes the constructed command
STDIN-args are expected to be delimited by whitespace (not protected with apostrophes, quotes or a backslash).
Blank lines in standard input are ignored.
Default command is echo

--repl[=repl-str]
-[repl-str]
String in command to be replaced. Default {}. The same as find -exec command {} \;
STDIN-args replace occurences of repl-str in the common-args.
Without this option, STDIN-args are appended to the command's arguments.
Without this option, STDIN-args are appended to the command's arguments.
Unquoted blanks do not terminate arguments.
Implies --exit and --max-lines=1.
--max-args=max-args
-n max-args
At most max-args arguments are used per command line.
--max-args=1 generates a seperate command for each argument.

> ls -1 b* | xargs echo +
+b1 b2 b3
> ls -1 b* | xargs -max-args=1 echo +
+b1
+b2
+b3
Fewer than max-args arguments will be used if max-chars is exceeded, unless --exit is given, in which case xargs exits.

-J repl-str

BSD (darwin too)

first occurrence of repl-str in STDIN-args is replaced.
not defaulted to {} as in linux.
-I replstr

BSD (darwin too)

one or more occurrences of replstr are replaced in command with the entire line of input resulting in a maximun of 255 bytes.
by concatenating as much of the argument containing replstr as possible.
The 255 byte limit does not apply to arguments to command which do not contain replstr.
No replacement will be done on command itself.
Implies -x. (Perhaps using -n 1 will be useful
-R rep#

BSD (darwin too)

Number of replacments for -I. Default: 5 Will not affect how many arguments will be read from input (-n), or
the size of the command(s) xargs will generate (-s).
repl-str must occur as a distinct argument, not within a quoted string.

Example: copy the list of files and directories which start with an uppercase letter in the current directory to destdir:

/bin/ls -1d [A-Z]* | xargs -J % cp % destdir

--verbose
-t
Before executing the constructed command, display it on STDERR
--interactive
-p
Prompt before executing each command.
Implies --verbose.
--null
-0
STDIN-args (perhaps piped from another process like find) are terminated by a null allowing the processing of filenames with embedded special characters like spaces, quotes or backslashes.
find -print0 is compatible with --null.

", \ etc are not special (all characters are taken literally).
Disables the eof-str.

--delimiter=delim
-d delim

Input items are terminated by delim.
When input items separated by newlines as with ls --null may be a better option if delimeter is a null.).
Quotes and backslash are not special; i.e. they are taken literally.
Disables the eof-str string, which is treated like any other argument.
Multibyte characters are not supported.
-E eof-str
-e ƥ[eof-str]
--eof[=eof-str]
Processing input ends when eof-str is encountered.
If eof-str is null ( as it might be in a script) it is ignored .
 public_html >ls  |xargs --eof=500.cgi grep robots {}
--arg-file=file
-a file

Read items from file not stdin.
stdin is unchanged when commands are run. Otherwise, stdin is redirected from /dev/null.
--max-chars=max-chars
-s max-chars
Limit command line to max-chars including the command and common arguments and the terminating nulls at the ends of the argument strings. The default is 20k characters.
--max-lines[=max-lines]
-l[max-lines]
Use at most max-lines nonblank input lines per command line, default :1
Trailing blanks cause an input line to be logically continued on the next input line!
Implies --exit.
--no-run-if-empty
-r
If STDIN-arg does not contain strings, do not run command.
Default: Command is run once even if there is no input.
--exit
-x
Exit if max-chars is exceeded.
Use --max-args with --interactive; otherwise chances are that only one exec will be done.
--max-procs=max-procs
-P max-procs
Run up to max-procs processes at a time; default 1.
If max-procs is 0(bad), xargs will run as many processes as possible at a time.
This will cause the system_process_table to fill up resulting in some process (not necessarily your's) partially completing, some being skipped and general mayhem.

If a process sub-shells a dozen times and xargs starts 100, that's 1200! See ulimit

--help Output a summary of options and exit.
xargs --help
Usage: xargs [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]
       [-E eof-str] [-e[eof-str]]  [--eof[=eof-str]]
       [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]
       [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]
       [-n max-args] [--max-args=max-args]
       [-s max-chars] [--max-chars=max-chars]
       [-P max-procs]  [--max-procs=max-procs] [--show-limits]
       [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]
       [--version] [--help] [cmd [initial-arguments]]

--version
xargs (GNU findutils) 4.4.2
Mac OS is distributed with 
    usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr] 
    [-L number] [-n number [-x]] [-P maxprocs] [-s size] [utility [argument …]]

At Midphase i.,e, Real-Wolrld-Systems.com : xargs (GNU findutils) 4.5.11
Raspberry Pi 10 (buster) xargs (GNU findutils) 4.6.0.225-235f Raspberry Pi 12 (bookworm) xargs (GNU findutils) 4.9.0

Examples:

Remove ALL vi backup files ( i.e. those with trailing ~) in all subdirectories (not just the current working directory):

/usr/bin/find . -iname "*~" | xargs rm 
List all html files that contain js with most recently updated displayed last:
grep --files-with-matches js *.html | xargs ls -lrt --max-args=1 {}

exit status

0 success
123 cmd exited with status in range of 1 - 125
124 cmd exited with status 255
125 cmd killed by signal (?)
126 cmd cannot be run because (?)
127 cmd is not found
1 some other error occurred.

Caution

When using find to create STDIN-args on a directory with a active file creates/deletes,
xargs may attempt to operate on non-existant (just deleted) files or miss files just created.

See find, locate, locatedb, updatedb.

Report bugs to <bug-findutils@gnu.org>.