user3040975
user3040975

Reputation: 655

find files older than X days in bash and delete

I have a directory with a few TB of files. I'd like to delete every file in it that is older than 14 days.

I thought I would use find . -mtime +13 -delete. To make sure the command works as expected I ran find . -mtime +13 -exec /bin/ls -lh '{}' \; | grep '<today>'. The latter should return nothing, since files that were created/modified today should not be found by find using -mtime +13. To my surprise, however, find just spew out a list of all the files modified/created today!

Upvotes: 47

Views: 90548

Answers (3)

BenVida
BenVida

Reputation: 2312

The simplest solution to this is in @navid's and @gniourf_gniourf's comments. Because it's buried in the comments, I'd like to bring it up to be more visible.

find your/folder -type f -mtime +13 -delete

This avoids any possible issues with spaces and whatnot in the filenames and it doesn't spin up another executable to do the deleting so it should be faster too.

I tried and tested this.

Upvotes: 15

stones333
stones333

Reputation: 8958

This works for me.

$ find ./folder_name/*  -type f -mtime +13 -print | xargs rm -rf

Upvotes: 11

Mindx
Mindx

Reputation: 689

find your/folder -type f -mtime +13 -exec rm {} \;

Upvotes: 67

Related Questions