Troubleshooting

Fix "Argument list too long" when deleting or moving many files

Delete, move or process hundreds of thousands of files without hitting 'Argument list too long', using find -delete, find -exec +, xargs and safe batching.

BeginnerLinux VPS3 min read

bash: /usr/bin/rm: Argument list too long appears when a wildcard like rm *.jpg expands to more file names than the kernel allows in one command. It is common in session, cache and mail folders with hundreds of thousands of files. The fix is to let a tool process the files in batches.

Before you start

  • SSH access and permission to change the files.
  • Double-check the directory: deleting in bulk cannot be undone. Run every command with -print or a count first.

Why it happens

The shell expands * into every matching file name and passes them all to the program. The kernel limits the total size of arguments and environment (getconf ARG_MAX shows it, often around 2 MB). Too many names exceed it, and the program never even starts.

Delete many files with find

Count first:

find /var/www/app/storage/framework/sessions -type f | wc -l

Delete:

find /var/www/app/storage/framework/sessions -type f -delete

Only files matching a pattern or older than a week:

find /home/user/cache -type f -name '*.tmp' -mtime +7 -delete

find handles files one by one internally, so there is no argument limit. Add -maxdepth 1 to avoid descending into subfolders.

Move or copy many files

Use -exec with +, which packs as many names as fit into each command call:

find /srv/incoming -maxdepth 1 -type f -name '*.jpg' -exec mv -t /srv/photos/ {} +

mv -t (target first) is what makes {} + work with mv and cp.

Use xargs

find /srv/logs -name '*.log' -print0 | xargs -0 gzip

-print0 and -0 handle spaces and special characters in names. Add -P 4 to xargs to run four processes in parallel for CPU-heavy work.

A shell loop for custom work

for f in /srv/incoming/*.csv; do /usr/local/bin/import.sh "$f"; done

The loop's wildcard is expanded by the shell itself, not passed to a single program, so it does not hit the limit (though it is slower for huge counts).

Delete a whole huge directory

If everything in the folder should go, deleting the folder is simplest:

rm -rf /path/to/cache-folder && mkdir /path/to/cache-folder

Restore the correct owner and permissions afterwards if an app writes there. For directories with millions of files, find ... -delete or rsync -a --delete empty/ target/ can be faster than rm -rf.

Stop it from happening again

  • Configure session garbage collection or move sessions to Redis.
  • Add a cron job that cleans old cache files: find /path/cache -type f -mtime +3 -delete.
  • Check inode usage with df -i; millions of small files can exhaust inodes before disk space.

Argument list FAQ

Can I just raise the limit?

ulimit -s affects it somewhat, but batching with find or xargs is the reliable fix.

Does this happen in cPanel File Manager?

File Manager may time out on folders with huge file counts; use SSH with the commands above, or ask support.

Is find -delete safe?

Yes, when the path and filters are right. Always run the same command with -print first.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.