1

In my bash script I create a directory each month for backups as follows:

DAY=$(date -d "$D" '+%d')
MONTH=$(date -d "$D" '+%m')
YEAR=$(date -d "$D" '+%Y')

mkdir -p /var/app/backup/$DAY$MONTH$YEAR

For example I get:

 01012000
 01022000
 01032000
 .
 .
 01012014
 01022014
 01032014
 01042014
 .
 .

After years we want to remove the directories that are older than half of a year. For example - the directories from 01012000 until 01062000 should be removed at 1/1/2015.

What is the best approach (from bash script) in order to remove the directories that are older than half a year, regarding that we have the date in the directory's name?

  • 1
    do you mean 01012014 and 01062014 everything before 01062014? Or every beginning half year as far as it goes back? – jmunsch Dec 2 '14 at 4:42
2

You could convert the filename into something that can be compared directly (such as the Unix timestamp (number of seconds since the epoch), or to YYYYMMDD, which would be lexicographically sortable), and then check if it's older than six months.

For example, a script like (say, at /path/to/compare.sh):

#! /bin/bash

LAST=$(date -d '6 months ago' +%s)

for FILE
do
    NAME=$(basename $FILE)
    DATE=$(perl -pe 's/(\d{2})(\d{2})(\d{4})/$3$2$1/' <<<$NAME)
    if (( $(date -d "$DATE" '+%s') < $LAST ))
    then
        rm -r $FILE
    fi
done

And do:

/path/to/compare.sh /var/app/backup/*

Here I am converting to the number of seconds. I had to rearrange DDMMYYY to YYYYMMDD, since my date didn't accept the former as a valid date. The conversion to seconds is redundant because of that, but I am not sure why date is rejecting the former (perhaps a locale issue?).

1

I'm going to focus on just the removal part of your question. If you have the list of filenames like this:

$ cat data.txt
01012000
01022000
01032000
01012014
01022014
01032014
01042014

And you know that the cut off date for 6 months, is say "01022014". You can use sort & sed to determine which files need to be deleted, like so:

This will reverse sort the files:

$ sort -r data.txt 
01042014
01032014
01032000
01022014
01022000
01012014
01012000

This will drop off any of the newer files, leading up to our cut off date, "01022014".

$ sort -r data.txt | sed '1,/01032000/d'
01022014
01022000
01012014
01012000

NOTE: sed '1,/PATTERN/d' deletes (i.e. d) all the lines occurring from the first line (i.e. 1,) until our PATTERN is encountered.

Finally, the list can then be sent to xargs for deletion:

$ sort -r data.txt | sed '1,/01032000/d' | xargs rm
0

You could use ranged brace expansion:

Remove every half year:

echo /path/to/directories/01{01..06}{2000..2014}

OR remove everything except the last half year:

echo /path/to/directories/01{01..12}{2000..2013}
echo /path/to/directories/01{01..06}2014

Replace echo with rm -r and update the path.

  • yes but it should be automatic way ( as part of my bash script ) – maihabunash Dec 2 '14 at 6:22
  • What do you mean by automatic? This will remove each directory as you originally asked it. I am a little confused. – jmunsch Dec 2 '14 at 6:32

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.