Remove old files

In my redhat 5 sysem , there are many files are generated to a directory, I would like to do the housekeeping as following , move the files elder than 90 days to a specific directory , then remove the the files elder than 180 days from this specific directory ,

I have a script to do it .

find /path -mtime +90 -exec mv {} /path2 {} \;
find /path3 -mtime +90 -exec mv {} /path4 {} \;
find /path5 -mtime +90 -exec mv {} /path6 {} \;
find /path7 -mtime +180 -exec rm {}  {} \;
find /path8 -mtime +180 -exec rm {}  {} \;
find /path9 -mtime +180 -exec rm {}  {} \;

it works to do the housekeeping , however , it is not easy to maint. the script , as there are many path ( over 70 ) , therefore , there are 140 lines in the script , can advise how to write a professional script to do it ? thanks

I think the best & efficient approach to handle this task is to create a cleanup rule file. A file with rules defined to maintain each directory.

To give you an idea, here I am having a rule file with following data:-

  • Directory - Directory to maintain
  • Days - Days to keep the files
  • Move - Move Flag (If set to 1 then execute mv in find command)
  • Remove - Remove Flag (If set to 1 then execute rm in find command)
  • Destination - Destination directory to move if Move Flag is set.
# --------------------------------------------------------------------- #
# Directory |   Days    |       Move    |       Remove  | Destination   #
# --------------------------------------------------------------------- #
path1   |   90          |       1       |       0       |   path2
path2   |   90          |       1       |       0       |   path4
path3   |   90          |       1       |       0       |   path5
path4   |   90          |       1       |       0       |   path6
path5   |   90          |       1       |       0       |   path7
path6   |   90          |       1       |       0       |   path8
path7   |   180         |       0       |       1       |   NA
path7   |   180         |       0       |       1       |   NA
path9   |   180         |       0       |       1       |   NA
# --------------------------------------------------------------------- #

Now you just have to write a script to read data from this pipe delimited file and run find command for each rule. It will be easy and simple if you want to modify or add a new rule because you don't have to change the code but just simply change the rule file. I hope this helps.

Use this script:

#!/bin/ksh     ## Note: #!/bin/bash also works
while read SRC DEST
do
   find $SRC -mtime +90 -exec mv {} ${DEST}/{} \;
   find ${DEST} -mtime +180 -exec -rm {} \;
done < /path/to/mv90.txt

Next create one file:
mv90.txt with each line having 2 directory names - source dir and dest dir

path1  path2

To maintain just edit the file changing or removing directories as needed.