brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: xargs can show how long one command line may be on your system

Xargs
~/tips/tip-xargs-show-limits

xargs --show-limits < /dev/null

Prints the system's limit on command length and the buffer size xargs really uses.

“Argument list too long” is the error xargs exists to prevent. If you’re curious how long a command can get on your system, xargs can show the numbers.

xargs --show-limits < /dev/null

The < /dev/null gives xargs empty input, so it prints the limits and exits without running anything. In a container the output includes:

POSIX upper limit on argument length (this system): 2094894
Maximum length of command we could actually use: 2094684
Size of command buffer we are actually using: 131072

The first line is what the kernel allows, around 2 MB here. The last one is interesting: xargs itself uses a 128 KiB buffer, much less than the maximum. So with a long list, it starts a new command every 128 KiB, even though one command could hold far more. That’s on purpose, and I doubt it matters often.

When it does matter, for a slow command where each start costs time, you can raise the buffer with -s, up to that maximum.

The numbers depend on the environment too. Environment variables share the same space as arguments, so a shell with a lot of exported variables leaves less room. And --show-limits is GNU only; elsewhere, getconf ARG_MAX gives the kernel limit.

Further reading: xargs(1) manual page, GNU findutils manual: Invoking xargs

More Xargs tips printf '%s\n' a b c d e f | xargs -n1 -P3 --process-slot-var=SLOT sh -c 'echo "slot $SLOT got $0"; sleep 0.2' grep -l 'TODO' *.py | xargs -o vim find . -name '*.orig' -print0 | xargs -0 -p rm find . -name '*.gz' -print0 | xargs -0 -n 1 gzip -t printf 'a b\nc d\n' | xargs -L 1 echo line: All Xargs tips (12) All topics

Link