Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 8 down vote accepted

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.

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.

share|improve this answer
You can't actually use the unlink system call on directories (you'll get an EISDIR error), so that first option isn't possible. – James Henstridge Mar 22 '12 at 3:01
Would mv to /tmp be faster? It seems that mv takes a lot of time too. – Mohammad Moghimi Mar 22 '12 at 4:35
@MohammadMoghimi: mving between different filesystems/partitions means a cp followed by a rm. – enzotib Mar 22 '12 at 6:52
@enzotib However, if /tmp is on the same filesystem, I wonder if mv and restart would be quicker? I'm not sure if /tmp is cleared using rm anyway. – Sparhawk Nov 15 '13 at 0:23

Sometimes, find $DIR_TO_DELETE -type f -delete is faster than rm -rf.

You may also want to try out mkdir /tmp/empty && rsync -r --delete /tmp/empty/ $DIR_TO_DELETE.

Finally, if you need to delete the content of a whole partition, the fastest will probably be umount, mkfs and re- mount.

share|improve this answer

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 :).

share|improve this answer

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...

share|improve this answer
The directory that I wanted to delete was on our computing cluster and the files were outputs of doing some experiments with many many images. All of a sudden I found out that I'm over quota so I had to either copy them to my pc (copying many files over network is super slow), or simply delete them (which also turned out to be slow). The thing that I did at the end was that I had to create a job (=rm -rf that_dir) and submitted to the cluster to do the deletion which was kind of stupid. – Mohammad Moghimi Mar 22 '12 at 6:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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