Short answer: Argument list too long means your shell expanded a glob such as *.log into more bytes than the kernel accepts for one new program (error E2BIG from execve, about 2 MiB on a default Linux system). The command never starts. Let find do the matching instead, so no huge command line is ever built:
find . -maxdepth 1 -name '*.log' -delete # remove the matching files
find . -maxdepth 1 -name '*.log' -exec rm {} + # same, through rm in batches
printf '%s\0' ./*.log | xargs -0 rm # keep your glob, let xargs split itThe same applies to ls *, cp *.jpg dest/, mv * ../old/, chmod 644 * or grep foo *: the program does not matter, the size of the expanded list does. Below is the error reproduced, the limits behind it, every fix with timings, and the one case where none of these help.
The error, reproduced with 200,000 files
On our Debian 12 host (bash 5.2.15, dash as /bin/sh, kernel 6.1, ext4) we created 200,000 empty files named like app-000001.log in a scratch folder with a loop (a loop with a redirect, not a glob, so creating them could not hit the limit), then ran the usual commands:
$ bash -c 'rm *.log; echo "exit=$?"'
bash: line 1: /usr/bin/rm: Argument list too long
exit=126
$ bash -c 'ls *.log'
bash: line 1: /usr/bin/ls: Argument list too long
$ sh -c 'rm *.log; echo "exit=$?"' # dash
sh: 1: rm: Argument list too long
exit=126
$ bash -c 'cp *.log ../dst/'
bash: line 1: /usr/bin/cp: Argument list too long
$ bash ./clean.sh # a script holding: rm *.log
./clean.sh: line 1: /usr/bin/rm: Argument list too longIn an interactive bash the line reads bash: /usr/bin/rm: Argument list too long, in an SSH login shell it starts with -bash:. Systems without a merged /usr, or with /bin first in PATH, show bash: /bin/rm: Argument list too long or /bin/ls. Exit status 126 comes from bash and dash: they found the program but could not start it. Nothing was deleted, not even part of the list. Python reports the same kernel error as OSError: [Errno 7] Argument list too long, since errno 7 is E2BIG on Linux.
Why it happens: the execve limit
The shell, not rm, expands *.log. It builds an array with all 200,000 names and hands it to the kernel through the execve system call to start /usr/bin/rm. The section "Limits on size of arguments and environment" in execve(2) sets the rules, and E2BIG is the error when they are broken:
- Arguments and environment strings together may use at most a quarter of the soft stack limit (
ulimit -s), and never more than 3/4 of the kernel constant_STK_LIM(8 MiB), so 6 MiB at most. The floor is 32 pages (128 KiB). - A single string may be at most 32 pages (
MAX_ARG_STRLEN, 128 KiB with 4 KiB pages), whatever the stack limit is.
The numbers on the same host:
$ ulimit -s
8192
$ getconf ARG_MAX
2097152
$ xargs --show-limits < /dev/null
Your environment variables take up 2439 bytes
POSIX upper limit on argument length (this system): 2092665
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2090226
Size of command buffer we are actually using: 131072
Maximum parallelism (--max-procs must be no greater): 21474836478192 KiB of stack divided by four is the 2,097,152 bytes that getconf ARG_MAX prints. How many files fit depends on the name length. We searched for the largest number of our 14 character names that /bin/true still accepted with an almost empty environment: 91,172. That matches the kernel source (fs/exec.c): each argument costs its length, plus one byte for the terminating null, plus an 8 byte pointer on a 64 bit system (91,172 × 23 bytes is just under 2 MiB). Longer paths, such as /var/www/site/cache/* spelled out in full, fit fewer names.
Fixes, best first
All five were run on the same 200,000 files on our host. The times are wall clock for that one run, so read them as a rough comparison, not a benchmark:
| Fix | What runs | Time |
|---|---|---|
find . -maxdepth 1 -name '*.log' -delete | find deletes each match itself, no rm at all | 1.1 s |
printf '%s\0' ./*.log | xargs -0 rm | 26 rm runs of up to 7,709 names | 1.3 s |
find . -maxdepth 1 -name '*.log' -exec rm {} + | 3 rm runs of up to about 83,500 names | 1.6 s |
rsync -a --delete empty/ logs/ | rsync empties the whole folder | 1.4 s |
rm -rf logs | removes the folder and everything in it | 1.1 s |
for f in ./*.log; do rm "$f"; done | one rm per file | 8.2 s for 20,000 files |
1. find with -delete. The fastest and simplest when you only delete. -maxdepth 1 keeps find in the current folder; drop it to include subfolders. Run the same line without -delete first to see what matches, and keep -delete last: the GNU find manual warns that find evaluates the line as an expression, so -delete placed first deletes everything below the starting point. Add tests such as -mtime +30 to keep recent files. The find guide covers the tests.
2. find with -exec ... +. For any other command (mv, chmod, gzip), the + ending makes find pack as many names into each run as fit, like xargs. For moves, -exec mv -t ../old/ {} + puts the target first so the names can follow. Use \; instead of + only if the command accepts a single file.
3. printf into xargs. Use this when your selection is a glob that find cannot express easily, such as ./*.[0-9].gz. printf is a bash and dash builtin (type printf says so), so the expanded list stays inside the shell and never goes through execve. \0 separates the names and xargs -0 reads them that way, so spaces, quotes and newlines in file names are safe. xargs then starts rm as often as needed, staying under its 128 KiB command buffer. The xargs guide explains -0, -n and -P.
4. A loop. for f in ./*.log also expands inside the shell and works, but it starts one rm per file: 8.2 seconds for 20,000 files here, so around a minute and a half for 200,000. Fine for a few thousand files or when each file needs its own logic.
5. rsync with an empty folder. mkdir empty && rsync -a --delete empty/ logs/ leaves logs/ empty, including hidden files and subfolders, and also copies the permissions of empty/ onto logs/. In our run it was no faster than find, so it is only worth it where rsync is already your tool.
6. Remove the whole folder. If everything in it goes, rm -rf logs has no list to expand and is as fast as find. Recreate it with the owner and mode the application expects (ls -ld logs before you delete).
Why raising ulimit -s only partly helps
Raising the soft stack limit in the current shell raises the argument limit with it, up to the 6 MiB cap. On our host:
stack (ulimit -s) getconf ARG_MAX most of our files one command took
4096 1048576 45,582
8192 (default) 2097152 91,172
16384 4194304 182,353
65536 6291456 all 200,000
unlimited 6291456 all 200,000So ulimit -s 65536 followed by rm *.log worked for 200,000 short names, but the ceiling stays at 6 MiB of strings and pointers (273,533 of these names in our test), the single string limit of 128 KiB does not move at all, and the setting lasts only for that shell and its children. The soft limit cannot go above the hard limit (ulimit -Hs), and only root can raise that. With a million files you are back to find or xargs anyway, so use those from the start.
When one argument alone is too long
Sometimes there is no glob at all, and still every command fails. That is the 128 KiB limit on a single string. It hits a giant sed -e script, a JSON body passed to curl -d "$json", a bash -c "$script", or an exported environment variable, because each NAME=value pair is one string passed to every program you start:
s=$(head -c 131071 /dev/zero | tr '\0' a) # 131,071 characters
/bin/true "$s"; echo "exit=$?"
s="${s}a" # 131,072 characters
/bin/true "$s"; echo "exit=$?"
export BIG="$(head -c 200000 /dev/zero | tr '\0' x)"
ls > /dev/null
echo hi | cat
echo "echo still works"
unset BIG
ls > /dev/null; echo "exit=$?"$ bash ./bigarg.sh
exit=0
./bigarg.sh: line 4: /bin/true: Argument list too long
exit=126
./bigarg.sh: line 6: /usr/bin/ls: Argument list too long
./bigarg.sh: line 7: /usr/bin/cat: Argument list too long
echo still works
exit=0Builtins such as echo still work, external programs do not, and ulimit -s unlimited did not change it in our test. xargs cannot help either, because it splits lists of arguments, not one argument. The fix is to move the data out of the command line:
- Write the script or data to a file and pass the file:
sed -f script.sed,curl --data-binary @body.json,bash script.sh. - Or feed it on standard input:
printf '%s' "$json" | curl --data-binary @- URL. - Keep big values in shell variables without
export; a plain variable never leaves the shell. To find a bloated environment, runenv | awk '{ print length($0), substr($0, 1, 40) }' | sort -rn | head, and check ~/.bashrc for anexportthat grows on every login.
On a server: folders with millions of files
On a VPS or dedicated server this error usually turns up in a cache, session, upload or log folder an application has been filling for months, often in the middle of a clean up when the disk or the inodes are already short (df -i shows inodes). Listing the folder is fine as long as there is no glob: ls | wc -l or ls -f | head pass no file names to ls. Once the backlog is gone, schedule the clean up so it never builds again, for example a daily crontab line:
# 03:15 every day: delete cache files untouched for more than 7 days
15 3 * * * find /var/www/app/cache -type f -mtime +7 -deleteFor log files, let logrotate do it: rotate 14 keeps 14 old copies and maxage 30 removes rotated logs older than 30 days. Deleting a few million files still takes a while and a lot of disk I/O on a busy server, so run the first clean up at a quiet hour.
Frequently asked
What causes Argument list too long?
Your shell expands a glob such as *.log into one list of file names and passes it to the kernel to start the program. When the names and the environment together exceed the execve limit, about 2 MiB on a default Linux system (a quarter of the 8 MiB stack limit), the kernel returns E2BIG and the program never starts. The shell then prints Argument list too long and exits with status 126.
How do I delete files when rm * says Argument list too long?
Let find select the files instead of the shell: find . -maxdepth 1 -name '*.log' -delete. For other commands use find . -maxdepth 1 -name '*.log' -exec rm {} +, or printf '%s\0' ./*.log | xargs -0 rm, which keeps your glob but lets xargs split it into several runs. If the whole folder can go, rm -rf on the folder avoids the list entirely.
What is ARG_MAX on Linux and how do I check it?
Run getconf ARG_MAX; on a default system it prints 2097152, a quarter of the 8192 KiB stack limit shown by ulimit -s. xargs --show-limits < /dev/null shows how much of that your environment already uses. The kernel caps the total at 6 MiB however large the stack limit is, and a single argument or environment variable may not exceed 128 KiB.
Does raising ulimit -s fix Argument list too long?
Only partly. A higher soft stack limit raises the argument space up to a 6 MiB ceiling, which helped in our test with 200,000 short names, but the 128 KiB limit on a single string stays and the change applies only to that shell. For large folders, find -delete or xargs is the reliable fix.