August 3 2017

Spring cleanup of archive files!

 

During a project, we casually gathered waaaay too many archive files.  These were intentionally stored, however, in development and testing environments, they became a nuisance, and took up too much space.  Occasionally the Ops team would send out an email saying that disk space was full, and it was time to do some cleanup.

I created a fairly simple script (see below) that searches some directories for specific files/file types, and lists them out, or optionally deletes them.  After creating the script for my specific requirement, I went back and added options for more re-usability.  In our scenario, we were storing some temp files under a '/mnt/edi/tmp' directory, and archives under a 'staging' directory.

Using the 'find' command under Linux, we were able to locate files that exceeded a certain age, and then delete them.

 

**You will need to modify the script to point to your specific directories!

 

Here are some common usages:

Help

Take a look at the command options.

:~/scripts$ ./cleanup.sh -h
************************************
Cleanup command line options:
  -d   : actually delete the files.
  -l   : list the files for deletion.
  -p   : does not prompt for deletion.
  -f   : forces the deletion.
  -t n : where n is number of days old. Defaults to 7
  -h   : displays these instructions.
************************************

List

List out the files that would be deleted.

 :~/scripts$ ./cleanup.sh -l
Thu Aug  3 09:27:17 MDT 2017 Looking for files older than 7 days.
checking /mnt/edi/tmp directory.                                   4  files found for destruction
checking /mnt/edi/Eligibility/*/staging directories.               0  files found for destruction

Files to delete...
/mnt/edi/tmp/FS0A9B8D1115D7F4987590000038F07287.payload
/mnt/edi/tmp/FS0A9B8D1115D7B03AB080000038F06582.payload
/mnt/edi/tmp/FS0A9B8D1115D7F4988260000038F07296.payload
/mnt/edi/tmp/FS0A9B8D1115D7F4988150000038F07293.payload

Delete

Actually, delete the files (add the -p command to not prompt).

:~/scripts$ ./cleanup.sh -dp
Thu Aug  3 09:29:00 MDT 2017 Looking for files older than 7 days.
checking /mnt/edi/tmp directory.                                   10  files found for destruction
checking /mnt/edi/Eligibility/*/staging directories.               0  files found for destruction

*******************************
         Deleting files.
*******************************

 

Cleanup.sh bash script

Please, please test this thoroughly before applying it to your environment!

#!/bin/bash

prompt="-i"
mtime="7"
while getopts "dlhpft:" opt;
do
        case $opt in
        d) doDelete="true"
        ;;
        l) listfiles="true"
        ;;
        h) listhelp="true"
        ;;
        p) prompt=""
        ;;
        f) force="-f"
        ;;
		t) mtime="$OPTARG"
		;;
        esac
done

if [ "$listhelp" == "true" ];
then
        echo "************************************"
        echo "Cleanup command line options:"
        echo "  -d   : actually delete the files."
        echo "  -l   : list the files for deletion."
        echo "  -p   : does not prompt for deletion."
        echo "  -f   : forces the deletion."
        echo "  -t n : where n is number of days old. Defaults to 7"
        echo "  -h   : displays these instructions."
        echo "************************************"
        
        exit
fi

echo "$(date) Looking for files older than" $mtime "days." 
echo "checking /mnt/edi/tmp directory.                                  " `find /mnt/edi/tmp  -name *.payload -mtime +$mtime | wc -l` " files found for destruction"
echo "checking /mnt/edi/Eligibility/*/staging directories.              " `find /mnt/edi/Eligibility/*/staging -name "*.*" -mtime +$mtime | wc -l` " files found for destruction"

if [ "$listfiles" == "true" ];
then
        echo ""
        echo "Files to delete..."
        find /mnt/edi/tmp -name *.payload -mtime +$mtime
        find /mnt/edi/Eligibility/*/staging -name "*.*" -mtime +$mtime
fi

if [ "$doDelete" == "true" ];
then
        echo ""
        echo "*******************************"
        echo "         Deleting files."
        echo "*******************************"
        find /mnt/edi/tmp -name *.payload -mtime +$mtime -exec rm $prompt $force {} \;
        find /mnt/edi/Eligibility/*/staging -name "*.*" -mtime +$mtime -exec rm $prompt $force {} \;
fi

 

Bonus Option: Automatically Schedule this using Cron!

Execute 'crontab -e' and add an entry to run this automatically.  This sample entry runs the script every day at noon, deleting, non-prompting, and listing out the files while logging all actions to a cleanup.log file!

0 12 * * * /home/oracle/scripts/cleanup.sh -dpl >> /home/oracle/scripts/cleanup.log 2>&1

 

Also, check out how to Automatically Rotate Managed Server Log Files!

About the Author

Bio

Kevin has over 9 years experience in enterprise scale implementations. Kevin is very experienced in architecting, modeling and developing BPM processes, particularly those requiring advanced ADF UI screens, as well as setting up the infrastructure for the BPM solutions, which have integrated with multiple external systems.

Join the Conversation

Enter your first name. It will only be used to display with your comment.
Enter your email. This will be used to validate you as a real user but will NOT be displayed with the comment.
By submitting this form, you accept the Mollom privacy policy.