Linux: Argument list too long (E2BIG) when deleting or copying files
ARG_MAX and MAX_ARG_STRLEN bound how many names execve accepts. Replace globs with find, batch with xargs, and avoid oversized arguments.

Introduction & Quick Summary
bash: /bin/rm: Argument list too longexecve failed because the argument list passed the kernel's limit. The shell expanded a glob into hundreds of thousands of file names, the total exceeded ARG_MAX, and the command never started. Nothing was deleted; the program was never executed.
The four variants people hit:
rm -rf *in a directory with too many entries. The glob is expanded by the shell beforermruns, so the limit applies to the whole expansion.cp *.log /backupormv *with the same problem, includingchmod 644 *andtar -cf x.tar *.- A single argument longer than 128 KiB. That is
MAX_ARG_STRLEN, a separate limit fromARG_MAX, and it fails even when the total is small. - A smaller limit than expected. The effective ceiling is also bounded by the stack size and by the wrapper that started the process, so a container can fail earlier than the host.
| Message | Failing limit | Typical cause | Fix |
|---|---|---|---|
Argument list too long | ARG_MAX total | Glob with too many files | find ... -delete or xargs |
Argument list too long with one big argument | MAX_ARG_STRLEN (128 KiB) | A single huge string | Pass a file, not a string |
Argument list too long only in Docker | Effective stack limit | ulimit -s inherited | Raise the limit or batch |
E2BIG in a script | Any of the above | subprocess with a list | Stream or batch the input |
Core Mechanics, Signatures & Reproducible Setup
Every process start copies the argument strings and the environment into the new process's stack. Linux enforces several limits at that point: the total size of arguments plus environment (ARG_MAX, commonly 2 MiB), a per-string limit (MAX_ARG_STRLEN, 131072 bytes), and a practical ceiling tied to the stack size limit. When any is exceeded, execve returns E2BIG, which the shell reports as Argument list too long.
A reproducible failure needs a directory with enough entries to blow the limit:
mkdir -p /tmp/many && cd /tmp/many
seq 1 300000 | xargs -I{} touch file-{}.log
getconf ARG_MAX # e.g. 2097152
ls | wc -l # 300000
rm *.log
# bash: /bin/rm: Argument list too long# the working replacement
find /tmp/many -maxdepth 1 -type f -name '*.log' -delete
find /tmp/many -maxdepth 1 -type f | wc -lfind passes at most one path per invocation, so no single execve approaches the limit. The alternative is to batch the names yourself.
Key Comparison Table
Each workaround pipes the names differently, and the differences matter for odd file names, speed, and safety.
| Approach | Expansion done by | Handles spaces and newlines | Invocations | Deletes the directory tree |
|---|---|---|---|---|
rm *.log | Shell glob before rm | Yes, but still hits E2BIG | One | No |
find -delete | find itself | Yes | None per file | With the right predicate |
find -exec rm {} + | find, batched to ARG_MAX | Yes | Few | No |
find -print0 piped to xargs -0 rm | xargs, split at ARG_MAX/-n | Yes, NUL-separated | Few | No |
find -print piped to xargs rm | xargs, whitespace split | No | Few | No |
rsync -a --delete empty/ target/ | rsync | Yes | One | Yes, mirror semantics |
The limits themselves are worth knowing when a fix works on one host and not another.
| Limit | Typical value | Set by | Affected by ulimit -s |
|---|---|---|---|
ARG_MAX | 2 MiB | Kernel, _SC_ARG_MAX | Yes, capped by stack/4 |
MAX_ARG_STRLEN | 128 KiB | Kernel constant | No |
| Environment size | Included in ARG_MAX | Parent process | Yes |
xargs default batch | About 128 KiB of names | xargs itself | No, unless -s is set |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
Delete by predicate, not by glob
find is the direct replacement for a glob deletion and is the only form that stays correct when the directory keeps growing during the run.
find /var/log/app -maxdepth 1 -type f -name '*.log' -mtime +7 -delete# equivalent with explicit batching, useful when a wrapper requires rm
find /var/log/app -maxdepth 1 -type f -name '*.log' -mtime +7 -exec rm -f {} +-delete implies -depth and refuses to remove non-empty directories, so a predicate mistake fails safe rather than wiping a tree. Keep -maxdepth unless recursion is intended.
Batch with xargs -0 for arbitrary names
When the command cannot be replaced by find, xargs splits the list into invocations that fit. NUL separation is what makes it safe for names containing spaces, quotes, or newlines.
find /data/raw -type f -name '*.csv' -print0 | xargs -0 -n 500 gzip -9# see the batches without running anything
find /data/raw -type f -name '*.csv' -print0 | xargs -0 -n 500 echo | wc -l-n 500 caps the count per invocation, and xargs -s 100000 caps the bytes. Without either, xargs uses the system limit and can still exceed a container's smaller effective limit.
cp, mv, tar and chmod in bulk
The same pattern applies, with the destination as the final argument. For mv, batching changes semantics only if the destination does not exist, so create it first.
mkdir -p /backup/2026-11
find /data/logs -maxdepth 1 -type f -name '*.log' -print0 \
| xargs -0 -n 200 cp -t /backup/2026-11find /srv/www -type f -print0 | xargs -0 -n 500 chmod 644
tar -cf archive.tar --files-from=<(find /data/project -type f)tar --files-from reads the list from a file instead of the command line, which sidesteps the limit entirely. The process substitution keeps the list out of the shell's argument vector.
A single oversized argument
MAX_ARG_STRLEN is 128 KiB per string, so a single JSON body or SQL statement passed as an argument fails even in a short command.
# fails: one argument larger than 128 KiB
psql -c "$(cat migration.sql)"# passes: the file is read by the program
psql -f migration.sql
curl --data-binary @payload.json https://example.com/apiThe fix is to pass a path or use stdin, which is also what the programs expect for large input.
Containers and smaller effective limits
A container inherits the stack limit of its entrypoint, and some orchestrators set ulimit -s low. The kernel caps ARG_MAX at a quarter of the stack in some configurations, so a job that works on a laptop fails in a pod.
ulimit -s
getconf ARG_MAX
docker run --rm --ulimit stack=67108864 alpine sh -c 'getconf ARG_MAX'# raise it for the current shell, then run the command
ulimit -s unlimited 2>/dev/null || ulimit -s 65536
getconf ARG_MAXRaising the limit is a workaround. Batching with find or xargs works under any limit and is the better default in scripts.
Scripting the same fix in Python and Node
A subprocess call with a list of paths hits the same wall, and the traceback names OSError: [Errno 7] Argument list too long, which is easy to mistake for a permissions error.
import subprocess
from pathlib import Path
files = [str(p) for p in Path("/data/raw").glob("*.csv")]
for start in range(0, len(files), 500):
subprocess.run(["gzip", "-9", *files[start:start + 500]], check=True)import { execFileSync } from "node:child_process";
import { readdirSync } from "node:fs";
const files = readdirSync("/data/raw").filter((f) => f.endsWith(".csv"));
for (let i = 0; i < files.length; i += 500) {
execFileSync("gzip", ["-9", ...files.slice(i, i + 500)]);
}Both versions batch explicitly, which also makes progress visible and failures attributable to a specific slice.
Common Compilation/Runtime Mistakes & Version Gotchas
Deleting the files in a loop over *. for f in *; do rm "$f"; done still expands the glob in the shell, so it fails at the same point as rm *.
Using ls | xargs rm. It breaks on spaces and newlines and silently skips names ls quotes differently. Use find -print0 with xargs -0.
Assuming -f fixes it. rm -f suppresses missing-file errors, not E2BIG. The program never runs, so its flags never apply.
Quoting the glob. rm "*.log" passes a literal *.log and fails with No such file or directory, which is a different error and often mistaken for success in a script.
Raising ulimit -s without measuring. ARG_MAX shrinks with the stack on some kernels, and a very large ulimit -s can be refused by the container runtime. Measure with getconf ARG_MAX after changing it.
Version notes. MAX_ARG_STRLEN has been 32 pages (128 KiB on 4 KiB pages) since Linux 2.6.23, so it does not move with distribution upgrades. Bash reports Argument list too long; dash and zsh print the same text from the same E2BIG. xargs -0 is in POSIX and in BusyBox, so it is safe in Alpine images, while -P for parallelism is a GNU extension. GNU cp and mv have -t for a destination first; the BSD versions on macOS do not, so scripts that batch with cp -t fail there.
Fix order: confirm the count with find | wc -l, replace the glob with a find predicate, and only then decide whether -delete, -exec {} +, or xargs -0 fits. When the file names need checking before deletion, the regex tester filters the listing without running rm, and the chmod reference explains the permission forms used in the bulk scripts above.
Official references: execve(2) and E2BIG and xargs(1).
Official references
primary documentationThe Linux documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Linux guides
All Linux guidesnginx 502 Bad Gateway: upstream connection refused
The log line names the address and errno, which is the diagnosis. Fix the host, the start-up order, the buffers, or the timeout that actually fired.
Elasticsearch: max virtual memory areas vm.max_map_count is too low
The bootstrap check reads a kernel setting that is not namespaced. Set it on the host, persist it with sysctl.d, and tune nodes instead of containers.
/usr/bin/env: bash with a carriage return: no such file or directory
A CRLF shebang names an interpreter that cannot exist. Normalise the file and stop the next one with .gitattributes.
ENOSPC from inotify: the watcher limit, not the disk
Three per-user limits, one misleading error. Find the consumer, set a permanent sysctl drop-in, and know when polling is the honest answer.
More in Linux
All Linux guidesThe guides filed next to this one in Linux, in library order.