简体   繁体   中英

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.

  • Admin

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM