1

I have a list of 400 folders with the following pattern:

backup_01_01_2013/
backup_01_02_2013/
backup_01_03_2013/
...
backup_08_25_2014/

A new folder is created every day via a cron job.

I want to delete all folders EXCEPT:

  • Keep the 30 most recent folders
  • Keep the first of every month

How can I delete all the unnecessary folders using Bash in Linux?

tbenz9
  • 446
  • 2
  • 5
  • 18

2 Answers2

1

So, assuming they're all in the same directory.

I'll point out I tested portions of this script, but not all of it, so you'll probably want to do some modification/testing before you actually run it - lest you end up with all your directories removed.

# Find the date 30 days ago.
recent_dirs=`date -d "-30 days" +%d-%m-%Y`
rec_month=`echo "${recent_dirs}" | cut -d '-' -f2`
rec_day=`echo "${recent_dirs}" | cut -d '-' -f1`
rec_month=`echo "${recent_dirs}" | cut -d '-' -f3`

# Go to your "home" directory for the directories.
cd /path/to/home/directory

for i in *; do
    # Check to see if the element is a directory.
    if [ -d "${i}" ]; then
        echo "Processing directory - ${i} ... "
        # Determine the date information for the directory.
        cur_month=`cat "${i}" | cut -d '_' -f1`
        cur_day=`cat "${i}" | cut -d '_' -f2`

        # Keep all directories from the first of the month.
        if [ "${first_day}" -eq "01" ]; then continue; fi

        # Keep all directories from the current month and current year.
        if [ "${cur_month}" -eq "${rec_month}" ]; then
            if [ "${cur_year}" -eq "${rec_year}" ]; then continue; fi
        fi

        # Keep all directories from last month, but newer than today's DAY.  I'm not 
        #  a pro at bash arithmetic, so you might have to goof with the subshell 
        #  math to get it to work quite right.
        if [ "${cur_month} -eq $(( expr $rec_month - 1 )) ]; then
            if [ "${cur_day}" -gt "${rec_day}" ]; then continue; fi
        fi

        # So at this point, I think we've dealt with everything, except the case where
        #  the current month is January, and so you're trying to save December's 
        #  directories from last year.  I think this handles it.

        if [ "${rec_month}" -eq "01" ]; then
            if [ "${cur_month} -eq "12" ]; then
                if [ "${cur_year}" -eq $(( expr ${rec_year} - 1 )) ]; then continue; fi
            fi
        fi

        # If we haven't stopped processing the directory by now, it's time 
        #   remove our directory.
        rm -fr "${i}"
     else
        echo "Skipping non-directory "${i}"...
 do

exit 1

Something this won't do is deal with months with 31 days in it, so you may end up in many cases having saved 31 directories, instead of 30. I get the impression that you're trying to do a cleanup, and not a strict compliance routine, though...

ice13berg
  • 713
  • 8
  • 12
0

Quick & dirty - try 'ls -ltr' combined with tail -30 to get all but 30. run a for loop and rm -rf

shamik
  • 653
  • 1
  • 7
  • 13