Friday, May 3, 2024
Homepage · rm
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  44] [ 0]  / answers: 1 / hits: 78330  / 1 Year ago, mon, april 24, 2023, 7:05:24

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)?


More From » rm

 Answers
2

You could try unlinking the inode for the directory but that would leave you with a whole load of orphan files that fsck will flip out about.



rm is as good as it gets.






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:



rm -rf path/to/directory


rm will internally list the files and directories it's going to delete. And that's all in compiled C. It's those two reasons it's fastest.



This is very pointedly not the same thing as rm -rf path/to/directory/* which will expand at shell level and pass a load of arguments into rm. Then rm has to parse those and then recurse from each. That's much slower.



Just as a "benchmark" that compares find path/to/directory -exec {} ; is nonsense. That runs rm once per file it finds. So slow. Find can xargs-style build commands arguments with -exec rm {} + but that's just as slow as expansion. You can call -delete which uses an internal unlink call to the kernel (like rm does) but that'll only work for files at first.



So to repeat, unless you throw the disk into liquid hot magma, rm is king.






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 /dev/shm (a RAM disk) could be an idea.


[#39740] Tuesday, April 25, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
polcomposte

Total Points: 421
Total Questions: 92
Total Answers: 109

Location: Uzbekistan
Member since Mon, Jul 20, 2020
4 Years ago
;