Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readLinux
#Linux#Shell#Performance#Troubleshooting
An overflowing argument stack spilling past a cyan execve gate, amber e2big plate and batched xargs tray

Introduction & Quick Summary

text
bash: /bin/rm: Argument list too long

execve 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 before rm runs, so the limit applies to the whole expansion.
  • cp *.log /backup or mv * with the same problem, including chmod 644 * and tar -cf x.tar *.
  • A single argument longer than 128 KiB. That is MAX_ARG_STRLEN, a separate limit from ARG_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.
MessageFailing limitTypical causeFix
Argument list too longARG_MAX totalGlob with too many filesfind ... -delete or xargs
Argument list too long with one big argumentMAX_ARG_STRLEN (128 KiB)A single huge stringPass a file, not a string
Argument list too long only in DockerEffective stack limitulimit -s inheritedRaise the limit or batch
E2BIG in a scriptAny of the abovesubprocess with a listStream 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:

bash
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
bash
# the working replacement find /tmp/many -maxdepth 1 -type f -name '*.log' -delete find /tmp/many -maxdepth 1 -type f | wc -l

find 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.

ApproachExpansion done byHandles spaces and newlinesInvocationsDeletes the directory tree
rm *.logShell glob before rmYes, but still hits E2BIGOneNo
find -deletefind itselfYesNone per fileWith the right predicate
find -exec rm {} +find, batched to ARG_MAXYesFewNo
find -print0 piped to xargs -0 rmxargs, split at ARG_MAX/-nYes, NUL-separatedFewNo
find -print piped to xargs rmxargs, whitespace splitNoFewNo
rsync -a --delete empty/ target/rsyncYesOneYes, mirror semantics

The limits themselves are worth knowing when a fix works on one host and not another.

LimitTypical valueSet byAffected by ulimit -s
ARG_MAX2 MiBKernel, _SC_ARG_MAXYes, capped by stack/4
MAX_ARG_STRLEN128 KiBKernel constantNo
Environment sizeIncluded in ARG_MAXParent processYes
xargs default batchAbout 128 KiB of namesxargs itselfNo, 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.

bash
find /var/log/app -maxdepth 1 -type f -name '*.log' -mtime +7 -delete
bash
# 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.

bash
find /data/raw -type f -name '*.csv' -print0 | xargs -0 -n 500 gzip -9
bash
# 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.

bash
mkdir -p /backup/2026-11 find /data/logs -maxdepth 1 -type f -name '*.log' -print0 \ | xargs -0 -n 200 cp -t /backup/2026-11
bash
find /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.

bash
# 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/api

The 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'
bash
# raise it for the current shell, then run the command ulimit -s unlimited 2>/dev/null || ulimit -s 65536 getconf ARG_MAX

Raising 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.

python
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)
js
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 documentation

The Linux documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Linux guides

More in Linux

All Linux guides

The guides filed next to this one in Linux, in library order.