Skip to content
Latchkey

Fix "argument list too long" With xargs

xargs avoids "argument list too long" by splitting items across multiple command runs that each fit the OS limit.

When a glob expands to too many files, the shell rejects the command. xargs sidesteps this by feeding the files in safely sized batches.

What it does

The kernel caps the combined size of a command line and its environment (ARG_MAX). A glob like rm *.log that expands past that cap fails before the program even starts. xargs reads the same list on stdin and runs the command repeatedly, packing each invocation just under the limit so it always succeeds.

Common usage

Terminal
# fails: too many files for one command line
rm build/*.o            # /bin/rm: Argument list too long
# works: xargs batches them
find build -name '*.o' -print0 | xargs -0 rm
# cap each command line at 64 KiB explicitly
find . -type f -print0 | xargs -0 -s 65536 ls -l

Options

FlagWhat it does
(default)xargs sizes each command line under ARG_MAX automatically
-s <bytes>Cap each command line at this many bytes
-n <max>Also cap by item count per run
-0 (with -print0)Stream files via stdin instead of glob expansion
(getconf ARG_MAX)Show the system argument-length limit

In CI

Replace any "cmd dir/*" that might match thousands of files with "find dir ... -print0 | xargs -0 cmd". The error often appears only as a repo or cache grows, so a step that passed for months suddenly fails on a busy branch. Piping through xargs makes it size-independent.

Common errors in CI

The shell-level error is "/bin/rm: Argument list too long" (or "/usr/bin/<cmd>: Argument list too long"). xargs itself can still report "xargs: argument line too long" if a single item exceeds the limit, for example one absurdly long path, or under -I where each line is capped; raise -s or shorten the item.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →