Linux - Save only recent 10 folders and delete the rest

后端 未结 6 1733
借酒劲吻你
借酒劲吻你 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:48

    ls -lt | grep ^d | sed -e '1,10d' |  awk '{sub(/.* /, ""); print }' | xargs rm -rf 
    

    Explanation:

    • list all contents of current directory in chronological order (most recent files first)
    • filter out all the directories
    • ignore the 10 first lines / directories
    • use awk to extract the file names from the remaining 'ls -l' output

    • remove the files

提交回复
热议问题