This question already has an answer here:

Need to delete all sub-directories which are a month old. But last day of the month data should be ignored for previous three months.

The data representation is as follows:

bkp_01102014  bkp_04102014  bkp_07102014  bkp_11102014  bkp_15112014  
bkp_24112014  bkp_27112014  bkp_30062014  bkp_31072  bkp_20092014

How to automate this using find or any other command?

marked as duplicate by Patrick, Anthon, jimmij, garethTheRed, Ramesh Dec 8 '14 at 16:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Is it possible to change the dir format into bkp_yyyymmdd? This would make the solution easier (looping through months given by cut -c7-|sort|tail -3 and finding the dir you want to keep with cut -d_ -f2 | sort| tail -1 ) – Walter A Dec 8 '14 at 14:03
  • This will help . let me check whether i can rename – Zama Ques Dec 8 '14 at 14:18
  • 1
    I assume the date format in the filenames is ddmmyyyy? There is a strange entry in the last line (4th entry). – Lucas Dec 8 '14 at 14:59

I think you do not need to rename the files. You can transform the the filenames on the fly (first sed), compare them to a date (awk) and transform the matching filenames back (second sed).

find parent/directory -maxdepth 1 -type d -name 'bkp_*'                | \
sed 's#parent/directory/bkp_\(..\)\(..\)\(....\)#\3\2\1#'              | \
awk -v date=$(date --date='3 months ago' +%Y%m%d) '$0<$date{print $0}' | \
sed 's#\(....\)\(..\)\(..\)#parent/directory/bkp_\3\2\1#'              | \
xargs rm -r

You should test this without the xargs rm part first!


Tried to write a Bash script. Please check if it can help you out.


NOW=`date +%s`

case $MM in
    01|03|05|07|08|10|12 )
    echo "31" ;;
    04|06|09|11 )
    echo "30" ;;
    02 )
    echo "28" ;;

for file in `ls -1 bkp*`
#   echo $file
    DATE=`echo $file | tr -cd [0-9]` # Extract Day of the Month from file name
    YY=`echo $DATE | cut -c5-8` # Extract Year from file name
    MM=`echo $DATE | cut -c3-4` # Extract Month of Year fro file name
    DD=`echo $DATE | cut -c1-2`
    FDATE=`echo ${YY}${MM}${DD}` # Re-arrange the date
    FDATE=`date +%s -d $FDATE` # Date in terms of seconds
#   echo "FILEDATE= $FDATE"
    DIFF=`echo "$NOW - $FDATE" | bc` # Difference between NOW and FILEDATE in Seconds
#   echo "DIFF FROM NOW= $DIFF"
    DIFFDAYS=`echo "$DIFF / 86400" | bc` # Difference in terms of DAYS
    DIFFMONTHS=`echo "$DIFFDAYS / 30" | bc` # Difference in terms of MONTHS
    LASTDAY=$(last_day $MM) # To determine whether it is a last day of month
#   echo $LASTDAY

if [ $DIFFMONTHS -ge 1 ]
    if [ $DD -eq $LASTDAY -a $DIFFMONTHS -ge 4 ]
        rm -rf $file
        rm -rf $file

Test Output:

Before executing the script:

$ ls
bkp_20082013  bkp_20082014  bkp_20112014
bkp_20071989  bkp_20092014  bkp_30112014
bkp_20072014  bkp_20102014  bkp_31102014

After executing the script:

bkp_20112014  bkp_30112014

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