Shell script to count files, then remove oldest files

后端 未结 11 2222
眼角桃花
眼角桃花 2021-01-30 05:29

I am new to shell scripting, so I need some help here. I have a directory that fills up with backups. If I have more than 10 backup files, I would like to remove the oldest fi

11条回答
  •  余生分开走
    2021-01-30 05:46

    find is the common tool for this kind of task :

    find ./my_dir -mtime +10 -type f -delete
    

    EXPLANATIONS

    • ./my_dir your directory (replace with your own)
    • -mtime +10 older than 10 days
    • -type f only files
    • -delete no surprise. Remove it to test your find filter before executing the whole command

    And take care that ./my_dir exists to avoid bad surprises !

提交回复
热议问题