Command to delete all but the most recent three folders

3

On my application server, I'd like to keep a history of my releases. However, I do not want to keep all of them, because we rarely have to do rollback for very old releases.

So I'd like to know which command to delete all but the latest three releases.

Thank you!

    
asked by anonymous 03.09.2015 / 16:32

1 answer

4

Use the

ls -t diretorio/sub_diretorio | tail -n +4 | xargs rm -r
  • ls -t lists files sorted by date, most recent first
  • tail -n +4 displays from the 4th element of the list, ignoring the first 3
  • xargs passes list items to the command rm -r which recursively removes files / folders
03.09.2015 / 18:45