You're cleaning up a folder full of old session files, cache fragments or email backups, you type rm *, and bash shoots back -bash: /bin/rm: Argument list too long. Nothing gets deleted. We see this a lot on VPS and cPanel accounts where a single directory has quietly grown to hundreds of thousands of files. The good news: your server isn't broken, and the fix takes about a minute once you know why it happens.

Symptom: what the error looks like

It's not just rm. Any command that receives a huge expanded wildcard can hit it:

$ rm /home/user/tmp/sess_*
-bash: /bin/rm: Argument list too long

$ mv *.jpg /backup/images/
-bash: /bin/mv: Argument list too long

$ ls cache/*.html | wc -l
-bash: /bin/ls: Argument list too long

You'll also see it inside cron jobs and deploy scripts, where it's easier to miss because the output goes to a log or an email nobody reads. A classic one: a nightly cleanup cron that "works" for months, then silently stops deleting anything once the folder crosses a certain size, and the disk slowly fills up.

Cause: it's the shell, not the file system

When you type rm *.log, rm never sees the asterisk. Bash expands the wildcard first, builds the full list of matching filenames, and then hands that whole list to the kernel's execve() call as arguments. The kernel caps how many bytes of arguments plus environment variables a new process can receive. That cap is called ARG_MAX.

You can check it on your own server:

getconf ARG_MAX
# typically 2097152 (2 MB) on modern Linux

On current kernels the real limit is tied to a quarter of the stack size (ulimit -s), and there's also a separate per-argument limit of around 128 KB. Either way, 200,000 filenames like sess_8f3a9c... easily blow past it. The kernel returns E2BIG, and bash prints "Argument list too long."

Two things are worth knowing here:

  • It doesn't matter how big the files are. It's about how many names there are and how long they are, including the path prefix.
  • Bash builtins like echo and printf don't go through execve(), so echo * works fine even when ls * fails. That's actually useful, as you'll see below.

Fix: delete or move files without hitting ARG_MAX

Before you delete anything, double-check where you are and what matches. Run pwd, then count first:

find /home/user/tmp -maxdepth 1 -type f -name 'sess_*' | wc -l

Note the quotes around 'sess_*'. They stop bash from expanding the pattern, so find receives it as-is and does the matching itself.

Option 1: find with -delete (the one we recommend)

find /home/user/tmp -maxdepth 1 -type f -name 'sess_*' -delete

This is the fastest and safest choice. find walks the directory and unlinks each match itself, so there's no giant argument list at all. Keep -maxdepth 1 unless you really want to recurse into subfolders, and always put -delete last. If you put it before -name, find deletes everything it walks past.

Want to delete only old files? Add an age filter:

# only files older than 7 days
find /home/user/tmp -maxdepth 1 -type f -name 'sess_*' -mtime +7 -delete

Option 2: find with xargs (for mv, cp, chmod, etc.)

-delete only removes files. For any other command, pipe the names to xargs, which splits them into batches that fit under the limit:

find . -maxdepth 1 -type f -name '*.jpg' -print0 | xargs -0 mv -t /backup/images/

-print0 and -0 separate names with a null byte, so filenames with spaces, quotes or newlines don't break things. mv -t puts the destination first, which is what lets xargs add filenames at the end.

Prefer something without xargs? find can batch for you:

find . -maxdepth 1 -type f -name '*.jpg' -exec mv -t /backup/images/ {} +

The + at the end (instead of \;) groups many files into each call, so it's nearly as fast as xargs.

Option 3: a shell loop (slow, but obvious)

for f in sess_*; do rm -- "$f"; done

This works because the wildcard is expanded inside bash and never passed to execve() in one go. It runs rm once per file, so on 500,000 files it's painfully slow. Fine for a few thousand, not for a real cleanup.

Option 4: delete the whole directory

If everything in the folder is junk, removing the folder is quickest:

rm -rf /home/user/tmp/cache && mkdir /home/user/tmp/cache

rm -rf on a directory walks it internally, so there's no argument expansion. Just remember to recreate the folder with the right owner and permissions afterwards (chown user:user, usually 755), or whatever app writes there will start throwing errors.

Quick comparison

MethodSpeed on 500k filesBest for
find ... -deleteFastestDeleting by name, age or type
find ... -print0 | xargs -0Fastmv, cp, chmod, chown, gzip
find ... -exec cmd {} +FastSame as xargs, one less tool
for f in *; do ...Very slowSmall folders, one-off scripts
rm -rf dirFastEmptying an entire directory

On shared cPanel hosting

If you don't have SSH, cPanel's File Manager will usually time out or freeze on a folder this large. Enable SSH access (or use cPanel → Terminal if your plan includes it) and run the find command from there. No terminal at all? Add a one-time cron job under cPanel → Cron Jobs:

find /home/USERNAME/tmp -maxdepth 1 -type f -name 'sess_*' -mtime +2 -delete

Let it run once, check that the file count dropped, then remove the cron entry (or keep it as a scheduled cleanup, which leads us to prevention).

Common gotchas

  • Raising the limit isn't the answer. You can push it up with ulimit -s unlimited, but you're just moving the wall. Fix the command instead.
  • Your cleanup script is the problem. If a PHP or bash cron runs something like exec("rm -f /path/cache/*"), it'll fail the same way. Swap it for a find -delete call.
  • Inodes are probably full as well. A folder big enough to trigger this error is often why your account shows "inode limit exceeded." Check with df -i on a VPS, or the inode counter in cPanel's sidebar.
  • Deleting a lot of files takes time. Removing a million files on a busy disk can take several minutes and push up I/O wait. On a production VPS, run it with ionice -c3 nice -n19 find ... so your site stays responsive.

Prevention: stop folders from getting this big

  • PHP sessions: Make sure session garbage collection actually runs. On Debian/Ubuntu it's handled by a cron job (/etc/cron.d/php) instead of PHP itself. If you've set a custom session.save_path, that cron won't clean it, so add your own find -mtime job.
  • Cache plugins: WordPress page-cache plugins can pile up millions of files under wp-content/cache/. Set a sensible cache lifetime and enable automatic purging.
  • Mail: Huge cur/ or new/ folders in ~/mail/ usually point to a catch-all address or a cron sending output to a mailbox. Turn off the catch-all, or add >/dev/null 2>&1 to noisy cron jobs.
  • Logs and temp files: Use logrotate for logs, and a scheduled find /path -type f -mtime +7 -delete for app temp directories.
  • Monitor file counts: A weekly find /home -xdev -type d -size +10M lists directories whose directory entry itself has grown huge, which is a reliable sign of a folder with way too many files in it.

If you're on a SkyServer plan and you're not sure which folder is eating your inodes, or you're nervous about running -delete on a live site, open a ticket with the path and we'll help you clean it up safely.

Frequently Asked Questions

Is "Argument list too long" a sign my disk is full?

No. It's a limit on how much text the shell can pass to a command, not a storage problem. That said, folders big enough to trigger it often use up your inode quota too, so it's worth running df -i while you're at it.

Can I just increase ARG_MAX?

Kind of. On Linux the limit scales with the stack size, so ulimit -s can raise it for your session. It's a band-aid, though. The folder will keep growing, and find -delete or xargs work no matter how many files there are.

Why does "echo *" work but "ls *" fails?

echo is built into bash, so it never calls execve() and never hits the kernel limit. ls is a separate program, so the expanded list has to be passed to the kernel, which rejects it.

Is find -delete safe to run on a live website?

Yes, as long as your filters are tight. Always do a dry run first by swapping -delete for -print | head, use -maxdepth 1, and add -mtime so you don't delete files that are in use right now.

I only have cPanel and no SSH. What can I do?

Use cPanel's Terminal if your plan includes it. Otherwise, add a one-time cron job with the find ... -delete command, let it run, and then remove it. File Manager usually can't cope with folders this big.