Skip to content

Fix “rm: Argument list too long” in Linux (5 Ways)

Why rm, ls, cp and mv fail with "Argument list too long" on large directories, and five tested ways to delete any number of files fast with find, xargs and more.

Covers
bash, find, rm
On this page

You try to clean up a directory full of log files, cache files or mail spool entries, and instead of deleting anything, the shell stops with:

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

Depending on your shell and system, the same error can read /bin/rm: Argument list too long, zsh: argument list too long: rm or sudo: unable to execute /bin/rm: Argument list too long. Nothing was deleted, and the fix is the same in every case: stop passing the file names on the command line. This guide explains why the error happens and shows the fastest safe ways to delete (or move, copy and list) any number of files.

Quick Fix

To delete every .log file in the current directory, no matter how many there are:

find . -maxdepth 1 -type f -name "*.log" -delete

Run it with -print instead of -delete first to see what would be removed. The rest of this article explains this command, the alternatives and the mistakes to avoid.

Why “Argument List Too Long” Happens

The error does not come from rm. It comes from the kernel, before rm even starts:

  1. When you type rm *.log, your shell expands *.log into the full list of matching file names, for example rm app-1.log app-2.log … app-250000.log.
  2. The shell then asks the kernel to start rm with that list through the execve() system call.
  3. The kernel limits the total size of the arguments plus the environment variables passed to a new program. If the list is bigger, execve() fails with E2BIG, which the shell prints as “Argument list too long”.

So the limit is about the total length in bytes of all file names, not a fixed number of files. Long file names hit it sooner. You can see the limit on your system with:

getconf ARG_MAX
2097152

That is 2 MB, the usual value on Linux (a quarter of the default 8 MB stack size). xargs --show-limits shows how much of it is left after your environment variables. In our test, 250,000 files named like app-session-00000001.log were enough to trigger the error.

This is also why the error appears with any command, not just rm: ls *.log, cp *.log /backup/, mv *.log old/, grep error *.log and sudo rm *.log all fail the same way, because the shell builds the same giant list for each of them.

find reads the directory itself and deletes each match with a system call, without starting rm at all, so there is no argument list to overflow:

find /var/log/myapp -maxdepth 1 -type f -name "*.log" -delete

What each option does:

  • -maxdepth 1 stays in this directory, like rm *.log would. Without it, find also deletes matching files in every subdirectory.
  • -type f matches regular files only, never directories or symlink targets.
  • -name "*.log" is the pattern. Quote it, or the shell expands it before find sees it and you get the same error again.
  • -delete removes each match. Always put it last: find evaluates options in order, so find . -delete -name "*.log" deletes everything.

Do a dry run first by replacing -delete with -print, or count the matches:

find /var/log/myapp -maxdepth 1 -type f -name "*.log" | wc -l

find can also select by age or size, which is what you usually want for log and cache cleanups. For example, to delete .log files older than 30 days:

find /var/log/myapp -maxdepth 1 -type f -name "*.log" -mtime +30 -delete

If you got the error with sudo, run the whole find command with sudo:

sudo find /var/spool/myapp -maxdepth 1 -type f -name "*.tmp" -delete

Method 2: find with -exec … +

If you want rm itself to do the deleting, for example to add -v for verbose output, let find run it in batches:

find . -maxdepth 1 -type f -name "*.log" -exec rm -f {} +

The + at the end tells find to pack as many names as fit under the limit into each rm call, so it runs rm only a handful of times. Do not use \; instead of +: -exec rm {} \; starts one rm process per file, which is hundreds of times slower on large directories.

Method 3: find with xargs

xargs does the same batching and is useful when the file list comes from somewhere else, or when you want to run deletions in parallel:

find . -maxdepth 1 -type f -name "*.log" -print0 | xargs -0 rm -f

Always pair -print0 with xargs -0. They separate names with a null byte, so file names that contain spaces, quotes or even newlines are handled correctly. Plain find … | xargs rm breaks on those names and can delete the wrong file.

On a fast SSD you can split the work across CPU cores with -P, which runs several rm processes at once (here 4, with 1,000 names each):

find . -maxdepth 1 -type f -name "*.log" -print0 | xargs -0 -n 1000 -P 4 rm -f

On spinning disks or network storage this rarely helps, because the disk, not the CPU, is the bottleneck.

Method 4: Keep the Glob, Skip the Argument List

If you prefer your shell’s glob patterns (for example app-2026-0[1-6]*.log), feed the names to xargs through printf. printf is a shell builtin, so the expanded list never goes through execve() and the limit doesn’t apply:

printf "%s\0" *.log | xargs -0 rm -f

A plain shell loop also works, because the loop is handled inside the shell:

for f in *.log; do rm -f -- "$f"; done

But the loop starts one rm per file and is very slow on large directories. Use it only for a few thousand files, or when you need to do something else with each file. The -- stops rm from treating a file name that starts with - as an option.

Method 5: Delete the Whole Directory

If you want to remove everything in a directory, don’t use a glob at all. Delete the directory and recreate it:

rm -rf /var/cache/myapp/sessions
mkdir /var/cache/myapp/sessions

rm -rf on a directory walks it internally and never hits the argument limit. Note that the new directory gets default ownership and permissions; restore them if the application needs specific ones (check them first with stat). To empty a directory while keeping it, use find /var/cache/myapp/sessions -mindepth 1 -delete instead.

Which Method Is Fastest?

We created 250,000 empty .log files in one directory on an Ubuntu server with an SSD and timed each method:

MethodTimeNotes
find … -delete2.5 sFastest; no extra processes
find … -print0 | xargs -0 rm -f2.5 sSame speed; can run in parallel
printf "%s\0" *.log | xargs -0 rm -f2.7 sKeeps shell glob patterns
find … -exec rm -f {} +3.1 sBatches names into a few rm calls
for f in *.log; do rm … ; done6 min 20 sOne rm process per file
rm -f *.log—Argument list too long

The batching methods are all within a second of each other. Anything that starts one process per file (a loop, or find -exec … \;) is over 100 times slower.

Moving, Copying and Listing Many Files

The same approach fixes the error for other commands. GNU mv and cp have a -t option that takes the destination first, so the file names can be appended in batches:

find . -maxdepth 1 -type f -name "*.log" -exec mv -t /backup/logs/ {} +
find . -maxdepth 1 -type f -name "*.log" -exec cp -t /backup/logs/ {} +

To count or list files without a glob, let ls or find read the directory directly. ls -f skips sorting, which makes it much faster on huge directories:

ls -f | wc -l
find . -maxdepth 1 -type f -name "*.log" | head

To search inside many files, use grep‘s own recursion instead of a glob:

grep -rl "ERROR" --include="*.log" .

What Does Not Work

  • Raising the stack limit. On Linux the argument space grows with ulimit -s, but the kernel caps it (at about 6 MB on current kernels), so this only helps for lists slightly over the limit. In our test ulimit -s 65536; rm -f *.log still failed.
  • Adding sudo, or switching shells. bash, zsh and sh all build the same list, and sudo passes it on to rm in the same way.
  • Unquoted patterns in find. find . -name *.log lets the shell expand the glob first, which fails the same way, or silently matches only one file.

Prevent It from Happening Again

Directories with hundreds of thousands of files usually mean something isn’t being cleaned up. Fix the cause:

  • Rotate and expire application logs with logrotate.
  • Schedule a cleanup with cron or a systemd timer, for example a nightly find /var/cache/myapp -type f -mtime +7 -delete.
  • For PHP session files, check that the session garbage collector runs (session.gc_probability and session.gc_maxlifetime).

Learn More

For more on the tools used here, see the find command with practical examples, the xargs command, how to limit directory depth with find and how to parallelize file operations with find and xargs.

Conclusion

“Argument list too long” means the shell expanded a glob into more file names than the kernel will pass to a program. Don’t pass the names on the command line: find … -delete is the fastest and safest fix, find … -exec rm {} + and find -print0 | xargs -0 rm are good alternatives, and rm -rf on the directory works when you want everything gone. Always do a dry run with -print first, keep -maxdepth 1 if you don’t want to touch subdirectories, and quote your patterns.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top