Sachin H
Sachin H

Reputation: 31

bash delete older files

I have this unique requirement of finding 2 years older files and delete them. But not only files as well as corresponding empty directories. I have written most of the logic but only thing that is still pending is , when I delete particular file from a directory , How can I delete the corresponding directory when it is empty. As when I delete the particular file , the ctime/mtime would also accordingly get updated. How do I target those corresponding older directories and delete them? Any pointers will be helpful. Thanks in advance.

Upvotes: 2

Views: 393

Answers (1)

Luke Garrigan
Luke Garrigan

Reputation: 5011

I would do something like this:

find /path/to/files* -mtime +730 -delete

-mtime +730 finds files which are older than 730 days.

Please be careful with this kind of command though, be sure to write find /path/to/files* -mtime +730 beforehand and check that these are the files you want to delete!

Edit: Now you have deleted the files from the directories, -mtime +730 won't work. To delete all empty directories that you have recently altered:

find . -type d -mmin -60 -empty -delete

Upvotes: 3

Related Questions