Deleting many files - the easy way Print

  • 13

If you've ever gone in to a directory, for example a mail or log directory, and wanted to clear it of its contents, but faced an error like this:
/bin/rm: cannot execute [Argument list too long]

We have a solution for you. It's nice and simple, and easy to remember. Paste and run the command in the relevant directory to have it sniff out files in batches of 200 at a time, delete them, then search again until the directory is empty.
echo * | xargs -n 200 rm

You might also want to recursively delete all sub-directories / sub-folders too, in which case you'd add -rf (recursive, force) on the end, like so:
echo * | xargs -n 200 rm -rf

You can also adapt the command to look for particular extensions or strings, for example:
echo *.txt| xargs -n 200 rm

This will search through all files, list the first 200 files with .txt extension, delete them, and then keep searching until it's gone through the entire directory removing all files that have the .txt extension on them.

Was this answer helpful?

« Back