I have two folder for Full and Incremental folder which has separate backup folder in them based on date in folder name like for example:

cd /backup/Full                     
Jul 12 17:52 01-07-2017
Jul 12 18:32 08-07-2017
Jul 15 18:20 15-07-2017
cd /backup/Incremental
Jul 13 12:01 02-07-2017
Jul 13 12:09 03-07-2017
Jul 13 12:14 04-07-2017
Jul 13 12:22 05-07-2017
Jul 13 12:36 06-07-2017
Jul 13 12:47 07-07-2017
Jul 13 12:49 09-07-2017

I want to run script which will calculate the 90 days older folder based on date as name of the folder like 01-07-2017 and delete it (Strictly not to consider folder creation date). I need to run 2 script one for Full and second for Incremental using same condition.

  • You can look into logrotate, which is built for this purpose.
    – l0b0
    Jul 17, 2017 at 10:33
  • Any specific script based on logrotate command. please help? @l0b0 Jul 17, 2017 at 10:46

3 Answers 3


find . -type d -mtime +90 -exec rm {}\;

  • find files(f) in the current directory(.) which is older than 90 days, execute the remove command(rm).

. can be replaced with the directory path/

  • mtime parameter look for last modified date of the directory and delete it accordingly however I want to delete the folder based on date name folder :) Jul 17, 2017 at 14:29

I suggest to make bash script for:

  1. get names of folders to array in format yyyymmdd as int
  2. calculate currentdate-90 in format yyyymmdd as int
  3. list variables lowest than currentdate-90 change back to format dd-mm-yyyy
  4. run rm for listed dirs.

But maybe someone do that better way


An implementation of @Krzysztof Stasiak description could be like this.

First make some test directories:

cd /backup
mkdir Full/20170{1,7}02
mkdir Incremental/2017010{1..9}
mkdir Incremental/2017070{1..9}

And the script:

#! /bin/bash
cd /backup
for P in Full Incremental; do
    cd $P
    for D in *; do
        [ $(date -d '-90 days' +%Y%m%d) -gt $D ] && rm -rf $D
    cd ..

To test, you can put an echo before rm to see what is going to be done but without doing it.

Here we just cd to the main directory and start a for-loop for the two diretories:

cd /backup
for P in Full Incremental; do

then cd into the "periode" directory:

cd $P

then do a forloop over all directories:

for D in *; do

if today minus 90 days is greater-than -gt the directory $D:

[ $(date -d '-90 days' +%Y%m%d) -gt $D ]

then delete the directory:

&& rm -rf $D

You can also write it like:

if [ $(date -d '-90 days' +%Y%m%d) -gt $D ]; then
  rm -rf $D

Consider using a standard backup software instead. It could be Bacula.

  • I have a doubt on your script :( Jul 17, 2017 at 14:56
  • What is the question?
    – hschou
    Jul 17, 2017 at 16:10
  • I am looking for two separate script being run for Full and Incremental directories so script could be like defining 'date1' variable which will calculate the date which will be 90 days back folder and then will use "rm -rf /backup/Incremental/(dollar)date1" it will delete the folder. Jul 18, 2017 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.