Shell script to count files, then remove oldest files

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

    Make sure your pwd is the correct directory to delete the files then(assuming only regular characters in the filename):

    ls -A1t | tail -n +11 | xargs rm
    

    keeps the newest 10 files. I use this with camera program 'motion' to keep the most recent frame grab files. Thanks to all proceeding answers because you showed me how to do it.

提交回复
热议问题