How to delete thousands of files
Let's say we have a folder containing millions of small files, images, temporary files or the like that we want to delete to regain valuable resources. Using a simple rm -rf folder
may take a lot of time and use up 100% disk and CPU resources.
Linux provides 2 commands which can decrease resource usage (ionice and nice). However, using them with rm command does not seem to work correctly and still uses all resources in some cases.
We could use the rsync command to works around the problem.
You must first create an empty folder ...
mkdir empty
It is then possible to synchronize the 2 folders.
ionice -c 3 nice -19 rsync -a --delete empty/ folder/
This will then delete all the contents of the folder folder
.