Command to delete all folders except the three most recent

Asked

Viewed 877 times

3

On my application server, I would like to keep a release history of it. However, I don’t want to keep them all, because we rarely have to do rollback for very old releases.

Therefore, I would like to know the command to delete all releases, except the three most recent.

Thank you!

1 answer

4


Use the command

ls -t diretorio/sub_diretorio | tail -n +4 | xargs rm -r
  • ls -t list the files sorted by date, latest first
  • tail -n +4 displays from the 4th element of the list, ignoring the first 3
  • xargs passes the list items to the command rm -r that removes files/folders recursively

Browser other questions tagged

You are not signed in. Login or sign up in order to post.