Deleting Many Files (Somewhat) Quickly

I had to delete a bunch of files across multiple directories. What works quickly?

The first option I found online was:

find . -type f -exec rm {} \;

There’s a faster way that deletes directly.

find . -type f -delete

That works well, unless there are a lot of files in each directory. Then, you might want to go into each directory and run the command. You don’t want to run too many at once, though.

There’s another way that turns out to be faster:

ls | xargs -L1 rm

Here’s the incantation to go into each directory and launch the command:

for i in * ; do pushd $i; ls | xargs -L1 rm & popd; done

I am not sure why it’s faster. Perhaps find loads the entire directory first, while ls just reads it piece by piece. In any event, it starts deleting sooner, and seems to run faster overall.

There is a HUGE caveat – do not bake that into a script.

You should not use the output of ls for anything, because you don’t know if ls is aliased – and it sometimes is.

Just be aware of the risks, and use the one-liner only when it’s safe – when the filenames are consistent.