As I know the commands like
find <dir> -type f -exec rm {} \;
are not the best variant to remove large amount of files (total files, including subfolder). It works good if you have small amount of files, but if you have 10+ mlns files in subfolders, it can hang a server.
Does anyone know any specific linux commands to solve this problem?
Here’s an example bash script:
This script starts by setting its own process priority and diskIO priority to very low values, to ensure other running processes are as unaffected as possible.
Then it makes sure that it is the ONLY such process running.
The core of the script is really up to your preference. You can use
rm -rif you are sure that the whole dir can be deleted indesciminately (option 2), or you can usefindfor more specific file deletion (option 1, possibly using command line options “$2” and onw. for convenience).In the implementation above, Option 1 (
find) first outputs everything to a tempfile, so that thermfunction is only called once instead of after each file found byfind. When the number of files is indeed huge, this can amount to a significant time saving. On the downside, the size of the tempfile may become an issue, but this is only likely if you’re deleting literally billions of files, plus, because the diskIO has such low priority, using a tempfile followed by a singlermmay in total be slower than using thefind (...) -exec rm {} \;option. As always, you should experiment a bit to see what best fits your needs.