Argument list too long
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 *orchmod *on a folder with too many entries. - Passing a large list of filenames to
cp,mv, orgrep. - 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" -deleteThe -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 rmThe -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 rmThe -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"
doneA 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-deleteIf the glob still fails, use find ... -exec mv to move files out, then delete them in bulk.
6. Check your ARG_MAX
getconf ARG_MAXOn modern Linux this is typically 2,097,152 bytes. You can’t easily change it without recompiling the kernel, so use batching tools instead.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro