Skip to content
Argument list too long

Argument list too long

DodaTech 3 min read

The Argument list too long error means a glob expanded beyond the shell’s buffer limit. Fix it with find -exec, xargs, or a for loop instead of rm *.

What It Means

Linux limits the total size of command-line arguments and environment variables passed to a new process, defined by ARG_MAX (usually 2MB on modern systems). When you use a wildcard like *, the shell expands it into individual arguments. If the total exceeds ARG_MAX, the kernel rejects the call and prints the error.

Why It Happens

  • Running rm * in a directory with tens of thousands of files.
  • Using ls * or chmod * on a folder with too many entries.
  • Passing a large list of filenames to cp, mv, or grep.
  • Scripts that construct command lines dynamically without batching.
  • A directory became a dumping ground for log files, exports, or uploads.

How to Fix It

1. Use find with -exec or -delete

Instead of rm *.log:

find . -name "*.log" -exec rm {} \;
find . -name "*.log" -delete

The -exec flag runs rm once per file (or in batches with +). The -delete flag is the fastest for simple deletion.

2. Batch with xargs

find . -name "*.log" -print0 | xargs -0 rm

The -print0 / -0 pair handles filenames with spaces or special characters. xargs automatically splits input into batches within the argument limit.

3. Use xargs with explicit batch size

find . -name "*.log" -print0 | xargs -0 -n 100 rm

The -n 100 flag runs rm with at most 100 arguments per invocation. Adjust the batch size based on your file count.

4. Use a shell for loop

for file in *.log; do
  rm "$file"
done

A for loop processes one file per iteration, keeping each command invocation small. This is slower but works with any number of files.

5. Move files to a subdirectory, then delete

mkdir /tmp/batch-delete && mv *.log /tmp/batch-delete/ && rm -rf /tmp/batch-delete

If the glob still fails, use find ... -exec mv to move files out, then delete them in bulk.

6. Check your ARG_MAX

getconf ARG_MAX

On modern Linux this is typically 2,097,152 bytes. You can’t easily change it without recompiling the kernel, so use batching tools instead.

Does ARG_MAX affect all commands?
Yes. Every execve() call enforces the limit. It includes both argument strings and environment variables. Even echo * fails if the directory has enough files — the shell expands the glob before echo ever runs.
What's the difference between find -exec {} \; and {} +?
With \;, rm runs once per file — safe but slow. With +, find groups files into batches (like xargs) and runs fewer process invocations. Use + for performance, \; when you need per-file control.
How many files cause this error?
It depends on filename lengths. Short names (like file1.log) need roughly 50,000+ files to hit 2MB. Long paths (200+ chars) can trigger it with only 10,000 files. The limit is total byte size of all arguments, not file count.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro