Linux - Save only recent 10 folders and delete the rest

后端 未结 6 1726
借酒劲吻你
借酒劲吻你 2021-02-04 14:14

I have a folder that contains versions of my application, each time I upload a new version a new sub-folder is created for it, the sub-folder name is the current timestamp, here

6条回答
  •  醉梦人生
    2021-02-04 14:40

    Your directory names are sorted in chronological order, which makes this easy. The list of directories in chronological order is just *, or [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] to be more precise. So you want to delete all but the last 10 of them.

    set [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/
    while [ $# -gt 10 ]; do
      rm -rf "$1"
      shift
    fi
    

    (While there are more than 10 directories left, delete the oldest one.)

提交回复
热议问题