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

  • SEARCH
  • Home
  • 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 8229713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:53:30+00:00 2026-06-07T16:53:30+00:00

I have a script which creates folders in a backup folder by the current

  • 0

I have a script which creates folders in a backup folder by the current date. This script is run once a day, every day via cron.

Is there a way to remove the folders older than 3 days via folder name? something like

date -3 ?

Script that works: Thank you to Jo So. This script creates a folder by date. Compresses the files for backup, sticks them in your backup directory and clears out backups older than 3 days 🙂

    #!/bin/bash

    cd /home/backups

    mkdir $(date +%Y-%m-%d)

    cd /opt/

    tar -pczf /home/backups/$(date +%Y-%m-%d)/opt.tar.gz code

    cd /var/

    tar -pczf /home/backups/$(date +%Y-%m-%d)/var.tar.gz work

cd /home/backups/
threedaysago=`date -d "3 days ago" +%Y%m%d`

for backup in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
do
    backupdate=`echo "$backup" | tr -d -`   # remove dashes

    if test "$backupdate" -lt "$threedaysago"
    then
        rm -rf "$backup"
    fi
done
  • 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-06-07T16:53:32+00:00Added an answer on June 7, 2026 at 4:53 pm
    threedaysago=`date -d "3 days ago" +%Y%m%d`
    
    for backup in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
    do
        backupdate=`echo "$backup" | tr -d -`   # remove dashes
    
        if test "$backupdate" -lt "$threedaysago"
        then
            rm -rf "$backup"
        fi
    done
    

    Work independently of mtime, and I can tell you that it will not break under particularly strange corner cases 😉

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

Sidebar

Related Questions

I have a script which creates a user-defined object like this: obj = new
I have a korn 88 shell script which creates a folder on the remote
I have an SQL script which creates a scheduled event: CREATE EVENT Daily_1200PM SCHEDULE
I have a PHP script which creates and listens on a port for connections
I'm trying to write a basic script which creates a UI. I have reduced
I have a script which I can run perfectly if I call it manually
I have a script which similar to this: foo.php class Foo { function Foo()
the procedure: i have a script which uploads images (via hmtl-form and php) for
I have a php script which takes a relative pathname via $_GET , reads
I have created this php script which displays the contents of a designated directory

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.