Fastest way to delete a non empty directory in Linux

4

1

I use the command rm -rf directory but i came across the following article : Article That suggests that rsynch to an empty folder is the fastest way? rsync -a –delete empty/ a Why is this the case ? Is there another faster method to delete a nonEmpty folder ?

Another interesting article about this Article2

firephil

Posted 2013-10-23T11:25:35.573

Reputation: 257

Answers

2

I have found that the fastest and most memory-efficient solution is to use this command in the directory full with millions of files:

ls -f1 | xargs rm

Explanation

Because ls -f1 will not sort the directory content, it will start the output right away. The rm command will take just one argument: the actual file name coming from the first command. It was the only solution to delete 3.000.000 files from a Magento webshop's session directory. The server ran on a virtual machine with just 2Gb of RAM - it was no other possibility to delete the files.

Frantique

Posted 2013-10-23T11:25:35.573

Reputation: 136

Doesn't rm -rf delete files one-by-one too? – Hi-Angel – 2018-09-28T07:10:05.763

3

Based on a very quick glimpse at GNU Coreutils 8.21 and rsync 3.0.1 sources plus taking a look what they do with the strace, the rm from GNU Coreutils seems to call malloc() quite a bit more than rsync does when performing the same thing. I'm doubt this explains the speed difference completely, though, but it's part of the explanation most likely.

Sami Laine

Posted 2013-10-23T11:25:35.573

Reputation: 1 002