Is there a maximum to bash file name expansion (globbing) and if so, what is it?
See globbing on tldp.org.
Let's say I want to run a command against a subset of files:
grep -e bar foo*
rm -f bar*
Is there a limit to how many files bash will expand to, and if so what is it?
I am not looking for alternative ways to perform those operations (e.g. by using find
).
Best Answer
There is no limit (other than available memory) to the number of files that may be expanded by a
bash
glob.However when those files are passed as arguments to a command that is executed (as opposed to a shell builtin or function), then you may run into a limit of the
execve()
system call on some systems. On most systems, that system call has a limit on the cumulative size of the arguments and environment passed to it, and on Linux also a separate limit on the size of a single arguments.For more details, see:
To work around that limit, you can use (assuming GNU
xargs
or compatible):Above, since
printf
is built-in (inbash
and most Bourne-like shells), we don't hit theexecve()
limit. Andxargs
will split the list of arguments into as manyrm
invocations as needed to avoid theexecve()
limitation.With
zsh
:With
ksh93
: