Working Around “argument list too long” Errors
Problem
You get an “argument list too long” error while trying to do an operation involving shell wildcard expansion.
Solution
Use the xargs command, possibly in conjunction with find, to break up your argument list.
For simple cases, just use a for loop or find instead of ls:
$ ls /path/with/many/many/files/*e*-/bin/bash: /bin/ls: Argument list too long
# Short demo, surrounding ~ are for illustration only
$ for i in ./some_files/*e*; do echo "~$i~"; done
~./some_files/A file with (parens)~
~./some_files/A file with [brackets]~
~./some_files/File with embedded
newline~
~./some_files/file with = sign~
~./some_files/file with spaces~
~./some_files/file with |~
~./some_files/file with:~
~./some_files/file with;~
~./some_files/regular_file~
$ find ./some_files -name '*e*' -exec echo ~{}~ \;
~./some_files~
~./some_files/A file with [brackets]~
~./some_files/A file with (parens)~
~./some_files/regular_file~
~./some_files/file with spaces~
~./some_files/file with = sign~
~./some_files/File with embedded
newline~
~./some_files/file with;~
~./some_files/file with:~
~./some_files/file with |~
$ for i in /path/with/many/many/files/*e*; do echo "$i"; done
[This works, but the output is too long to list]
$ find /path/with/many/many/files/ -name '*e*'
[This works, but the output is too long to list]
The example above works correctly with the
echo command, but when you feed that "$i"
into other programs, especially other
shell constructs, $IFS
and other ...
Get bash Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.