I have a folder that has many files and "rm -rf" takes a lot of time to complete. Is there any faster way to remove a directory and it's contents (subdirs, etc)?
You could try unlinking the inode for the directory but that would leave you with a whole load of orphan files that
A few people are mentioning edge cases where some things are faster than others. But let's make sure we're comparing the best versions of the same things.
If you want to delete a directory and everything in it, I'm suggesting you:
This is very pointedly not the same thing as
Just as a "benchmark" that compares
So to repeat, unless you throw the disk into liquid hot magma,
On a related note, different filesystems delete things at different rates because of how they're structured. If you're doing this on a regular basis you might want to store these files in a partition formatted in XFS which tends to handle deletions pretty fast.
Or use a faster disk. If you have tons of RAM, using
You may also want to try out
Finally, if you need to delete the content of a whole partition, the fastest will probably be
If you don't need the free space the quickest way is delay the deletion and do that in the background:
Then have a crontab that does it in the background, at a quiet time, with a low I/O proiority:
Update: I found a neat trick to run multiple rm in parallel - this will help if you have a large disk array:
I think the issue is that there is no perfect way to remove a very large directory and its entire set of contents with out a true indexed filing system that understands unlinking and doesn't mean it thinks it has missing files ala FSCK. There has to be a trust.
For instance I have zoneminder running for a golf range. I constructed a linux raid of 1.5 TB to handle the immense amount of data she captures a day ( 12 camera feeds ) how she ran on 120 GB drive is beyond me. Long story short the folder for all the captured data is about 1.4 TB of her storage. Lots to purge
Having to reinstall ZM and purge the 1.4 TB old library is no fun because it can take 1 - 2 days to delete the old images.
A true indexed FS allows the drop of directory and knows that data under it is dead and the zero'ing out the data is a waste of our time and PC resources. It should be an option to zero out deleted data. RM just takes to long in the real world on ext4.
Answer: Recursively unlinking all files would be faster marginally but you would still have to set aside a time to run FSCK.
Create a script running a recursive "FOR" command that can "unlink" all files under your folders then just rm or rmdir all folders to clean it up. Manually run FSCK to zero out the rest of the data when its convenient. Kinda lazy didnt write it out sorry :).
Albeit not useful if you want to purge an existing directory, I'll mention that a possible strategy if you know you will have a directory with a lof of files which you will need to purge regularly is to put the directory on its own filesystem (e.g., partition). Then when you need to purge it, unmount it, run a
It would be quite interesting to give us the reason you need it to go faster... (I am not kidding !)
i.e. One reason could be 'you need to free the filled directory for further usage' then if so renaming an recreating the empty directory would be very fast another reason could be 'you locked your terminal during the deleting time' if so you could do the deletion in background while still working... and so on...The more we know the best we could help...