Linux - Save only recent 10 folders and delete the rest

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

    ls -dt1 /path/to/folder/*/ | sed '11,$p' | rm -r 
    

    this assumes those are the only directories and no others are present in the working directory.

    • ls -dt1 will normally only print the newest directory however the /*/ will only match directories and print their full paths the 1 ensures one line per match/listing t sorts time with newest at the top.

    • sed takes the 11th line on down to the bottom and prints only those lines, which are then passed to rm.

    You can use xargs, but for testing you may wish to remove | rm -r to see if the directories are listed properly first.

提交回复
热议问题