Linux – Best Way to Remove 4 Million Small Text Files from a Folder

filesfindrm

Due to a script error (file_put_contents($text, $filename, FILE_APPEND) instead of file_put_contents($filename, $text, FILE_APPEND) I now have 4 million small text files in a folder.

I'd rather not nuke the whole folder, as a handful of useful files are in there.

All of the unwanted files have "jpg" in the middle of the filename, none of the wanted files do.

What's the quickest way to delete these? I can't even do a ls without it hanging my console at the moment – which makes identifying the names of the files I want to keep hard, otherwise I'd just move them out of the folder and then delete the whole folder.

Is find . -name "*jpg*" -delete the best option, or is there a better/quicker way?

Best Answer

With 4 million files, rm /location/of/many/files/*jpg* will probably fail with Argument list too long error.

Use find:

find /location/of/many/files/ -maxdepth 1 -type f -name '*jpg*' -delete

or if -delete is not available:

find /location/of/many/files/ -maxdepth 1 -type f -name '*jpg*' -exec rm {} +
Related Question