How can I remove a directory of many files fast?

For example, I backup my hard drive on an external drive, and when I need to release the space occupied by some very old backup on the external drive, I want to release the space as soon as possible. rm -r /very/old/backup takes very long to finish running.


marked as duplicate by αғsнιη, G-Man, Stephen Rauch, muru, taliezin Nov 6 '17 at 7:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Is there a solution faster than rm -r /very/old/backup in the link you gave? Or am I missing something? – Tim Nov 6 '17 at 4:48
  • 1
    Yeah, rsync there is super faster. – αғsнιη Nov 6 '17 at 4:50
  • rsync was the slowest of the three, rm being fastest and find second. – Jasen Nov 6 '17 at 9:24
  • the best answer may be to change the way you do backups, rsync has some really useful features that can save both space and time. – Jasen Nov 6 '17 at 9:26

Technically, this depends on the filesystem and the structure that's been set up for it. But in the normal case of UFS/EXT, the system has to visit each individual file to release space. In the case where you have many small files, it can take quite a while to walk the directory to release space.

Because your usage is as a backup, you might consider changing the layout before you need the space back. One option is to take the directory (potentially with many small files) and tar or zip it up. This will take some time, but can be done any time after the backup. When you have the archive file in place, delete the directory. At the end of the process, you have a single file on the filesystem, and can still pull any data you need from it.

When it's time to remove, you can rm a single file, which will go much more quickly.

Not the answer you're looking for? Browse other questions tagged or ask your own question.