Shell script to count files, then remove oldest files

后端 未结 11 2248
眼角桃花
眼角桃花 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条回答
  •  猫巷女王i
    2021-01-30 05:41

    I like the answers from @Dennis Williamson and @Dale Hagglund. (+1 to each)

    Here's another way to do it using find (with the -newer test) that is similar to what you started with.

    This was done in bash on cygwin...

    if [[ $(ls /backups | wc -l) > 10 ]]
    then
      find /backups ! -newer $(ls -t | sed '11!d') -exec rm {} \;
    fi
    

提交回复
热议问题