1

I have a directory with over 13 milion files and I want to remove some of them based on mtime, how do I do it efficiently? It's on ext4.

1 Answer 1

6

You probably won't get more efficient than:

find /dir -type f -mtime +6 -delete

To delete files older than 7 (yes, 7, not 6) days.

In any case, you'll need to do one lstat() system call on each file to determine its last modification time. Deleting a file (unlink()ing it from a directory) is expensive as it needs to edit the contents of the directory, update the directory's modification time, update the file's link count in the inode and update block/extent allocation information.

Using -delete (a GNU extension) is efficient as then the unlink() is done by find directly without having to invoke a separate rm utility. It implies -depth, but I don't expect that would make much difference in terms of performance.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .