-2

I Need to delete directories in a bash script that are older than 1 year based off of the structure of the folders. The folder structure is year/month/day for example backups/2016/nov/03 or backups/2017/jan/02. How can I loop through the folders and delete the ones that are older than a year?

This is as far as i've gotten

 TIMESTAMP=`date +%F_%H%M%S`
BACKUPBASEDIR=/data/pgsql/data/backup
HOSTNAME=`hostname`
YEAR=`date +%Y`
MONTH=`date +%b`
DAY=`date +%d`
LASTYEAR=$(( $YEAR - 1 ))
YEARINT=`printf '%d\n' "$YEAR"`
BACKUPDIR=$BACKUPBASEDIR/$LASTYEAR/$MONTH/$DAY

for f in $BACKUPBASEDIR/*
 do
  [ $(printf '%d\n' "$f") - $YEARINT -gt 1]; then
    echo "i'm not sure what to do at this point"
fi
done
exit

Edit:

$ ls -al
total 12
drwx------. 3 postgres postgres 4096 Oct 25  2016 .
drwx------. 5 postgres postgres 4096 Dec  1  2016 ..
drwx------. 2 postgres postgres 4096 Oct 25  2016 25

The directory 25 has a modified date of Oct 25 2016, but when I run

find /data/pgsql/data/backup/ -ctime +365 -type d

Nothing is found, even through the directory named 25 clearly hasn't been modified for more than a year.

koye
  • 1
  • 2
  • Have you tried anything so far? Show us your attempt at scripting this. http://idownvotedbecau.se/noattempt/ – mattias Nov 17 '17 at 20:14
  • Updated with what I have so far, honestly I'm new to bash so I don't really have much to show but hopefully i'm on the right path. @mattias – koye Nov 17 '17 at 20:21
  • Is the creation date of these directories intact? That is, if you do a `ls -l`, is the date correct? – mattias Nov 17 '17 at 20:23
  • @mattias From my understanding linux doesn't track the the birth/creation date of a file. Am I wrong? – koye Nov 17 '17 at 20:27
  • 1
    I would do the following: `find /data/pgsql/data/backup/ -ctime +365 -exec rm -rf {} \;` It will erase everything older than 365 days in there. I would recommend to first run `-exec echo rm -rf {}` too see if what is going to be erased is the you expect – ignivs Nov 17 '17 at 20:35
  • Right, but does the date of those folders, using ls -l add up to what you need? It would be way simpler, if so, to just recursively traverse that _backups_ dir and delete everything older than n days. Older referring to last modification date, https://stackoverflow.com/questions/3813541/is-there-a-way-to-know-the-creation-time-of-a-file-in-ubuntu – mattias Nov 17 '17 at 20:36
  • @mattias Yes, actually it looks like the numbers actually do match up. What would be the code to do that? Is it was ignivs posted? – koye Nov 17 '17 at 20:48
  • @koye Yes, and that will only print what it wants to delete. Once you're certain this is what you want to do, you change echo to whatever binary you want to use. _mv_, _rm_, _cp_ etc. – mattias Nov 17 '17 at 20:52
  • @mattias I updated my original post, when I run find /data/pgsql/data/backup/ -ctime +365 the directory isn't returned even though it hasn't been modified for a year. – koye Nov 17 '17 at 21:06
  • Did you do `find /data/pgsql/data/backup/ -ctime +365 -exec echo rm -rf {}`? – mattias Nov 17 '17 at 21:09
  • @ignivs When I run it with the `-exec echo rf ` i get this error `missing argument to -exec' ` – koye Nov 17 '17 at 21:10
  • @mattias that command gives me the error `missing argument to -exec'` – koye Nov 17 '17 at 21:12
  • Try `find /data/pgsql/data/backup/ -ctime +365 -exec echo rm -rf {} \;`, note the backslashsemicolon at the end. – mattias Nov 17 '17 at 21:19
  • @mattias Okay yeah it runs now but the directory still isn't printed. – koye Nov 17 '17 at 21:22
  • perhaps there is no directory matching the ctime you defined, try a shorter amount of days to see if starts matching (if you are willing to match directories particularly, you can add `-type d` to the find (so will miss files that may be stored in the root of the search path) – ignivs Nov 17 '17 at 21:31
  • @ignivs a shorter amount of days returns results, but I don't understand why because there are definitely folders that have modified dates that are at least 365 days old – koye Nov 17 '17 at 21:40
  • @koye my mistake, you should use **mtime** – ignivs Nov 18 '17 at 00:34

1 Answers1

0

All the mtime or atime suggestions ignore the fact that the original question was based off of the structure of the folders. If you want to use find, you should at least touch the directories to set the date right. But you might also use this:

#!/bin/bash
now=$(date -d now-1year +%s)
for d  in backups /*/*/* ; do
    if [ -type d  $d ] ; do
        year=$(echo $d|cut -d/ -f2)
        month=$(echo $d|cut -d/ -f3)
        day=$(echo $d|cut -d/ -f4)
        cond=$(date -d $day-$month-$year +%s)
        if [ $cond -lt $now ] ; then
            rm -rf $d
        fi
    fi
done
Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • This is really close to what I need, but what is the purpose of doing `now=date=now=date=$(date -d now-1year +%s)` instead of `now=now=date=$(date -d now-1year +%s)` – koye Nov 21 '17 at 15:31
  • Also this only deletes the lowest level directories and not the month or year directories – koye Nov 21 '17 at 19:10
  • Ah, you spotted my error; I removed the date= from my answer, which was basically a cut-and-paste problem. For the deletion of the month and year directories, you should test whether the directory is empty (`find` or `ls` or the likes) or just do an `rmdir` on all month and year directories ans throw away the error messages; `rmdir` will refuse to remove non-empty directories. – Ljm Dullaart Nov 22 '17 at 21:02