Shell script to count files, then remove oldest files

后端 未结 11 2244
眼角桃花
眼角桃花 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:31

    Straightforward file counter:

    max=12
    n=0
    ls -1t *.dat |
    while read file; do
        n=$((n+1))
        if [[ $n -gt $max ]]; then
            rm -f "$file"
        fi
    done
    

提交回复
热议问题