Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3348274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:31:22+00:00 2026-05-18T01:31:22+00:00

Possible Duplicate: Delete all but the most recent X files in bash I have

  • 0

Possible Duplicate:
Delete all but the most recent X files in bash

I have a script to create incremental backups daily and I need to delete all backups but last 5.

For example, I have this folders:

drwxr-xr-x  4 root root 4096 Oct 29 01:10 2010-10-29
drwxr-xr-x  4 root root 4096 Oct 30 01:10 2010-10-30
drwxr-xr-x  4 root root 4096 Oct 31 01:10 2010-10-31
drwxr-xr-x  4 root root 4096 Nov  1 01:10 2010-11-01
drwxr-xr-x  4 root root 4096 Nov  2 01:10 2010-11-02
drwxr-xr-x  4 root root 4096 Nov  3 01:10 2010-11-03
drwxr-xr-x  4 root root 4096 Nov  4 01:10 2010-11-04
drwxr-xr-x  4 root root 4096 Nov  5 01:10 2010-11-05
drwxr-xr-x  4 root root 4096 Nov  6 01:10 2010-11-06
drwxr-xr-x  4 root root 4096 Nov  7 01:10 2010-11-07
drwxr-xr-x  4 root root 4096 Nov  8 01:10 2010-11-08

And I need to maintain only the last 5 directories and delete the others. After command execute, I need to have only this:

drwxr-xr-x  4 root root 4096 Nov  4 01:10 2010-11-04
drwxr-xr-x  4 root root 4096 Nov  5 01:10 2010-11-05
drwxr-xr-x  4 root root 4096 Nov  6 01:10 2010-11-06
drwxr-xr-x  4 root root 4096 Nov  7 01:10 2010-11-07
drwxr-xr-x  4 root root 4096 Nov  8 01:10 2010-11-08

I don’t need to delete previous to 5 days, I need to delete all except 5 last directories 🙂

Now I’m using:


find /backup/increment -maxdepth 1 -type d -mtime +5 -exec rm -rf {} \;

But I need to improved not based in time 🙂

EDIT: This is an example for a server that do backups all days, but I need an script that delete all folders previous to last 5 because my computer do backups at 00:10 at night, but not all nights the backup is done it, because my computer isn’t working all days, and I need to have always the last 5 backups 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:31:22+00:00Added an answer on May 18, 2026 at 1:31 am

    use the tail command to print lines starting with the n th line (Option -n +N):

    rm `ls -t | tail -n +6`
    

    ls -t outputs the current directory sorted by time. tail -n +6 takes al lines starting with the 6th line. Quoting with backticks feeds the result of the pipe into the rm command.

    OLD SOLUTION, not correct …

    use the head command, which prints the first n lines of some output:

    rm `ls -t1 | head -n 5`
    

    ls -t outputs the current directory sorted by time. head -n 5 takes the first five entries of the previous output. Quoting with backticks feeds the result of the pipe into the rm command.

    Please try out first before applying to live data 🙂 …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.