Script to delete the older files automatically in linux

Script to delete the older files automatically in linux

In this post we explain how to schedule a job in linux  to delete older  files automatically in Linux or Unix. Here , we are creating  a script to delete the  30 days old database audit files.

This activity has two steps

  1. Create the script to delete the older files
  2. Schedule the script run automatically using ‘cron’

script to delete the older files

  • create a directory /wysheid/scripts
$ mkdir /wysheid/scripts
  • create a file audit_cleanup.sh

$ vi audit_cleanup.sh

  • Add the following contents to the script
$!/bin/bash
export AUDIT=/wysheid/oracle/adump  # The patch of the AUDIT_DUMP_DEST
find $AUDIT -type f -mtime +30 -exec rm {} \;

Here -type f ,will help to find only the files and exclude the directories

-mtype ,helps to find the files which are modified , +30 indicates 30 days older

-exec rm {} –> deletes the files.

  • Change the privilege of the file to execute mode
$ chmod 755 audit_cleanup.sh

Create a crontab to execute the script daily 2:00 am server time

As  oracle  user

  • Edit the crontab
$ crontab -e
  • Add the following command to schedule the job to execute every day at 2:00 am
0 2 * * * /bin/sh /wysheid/scripts/audit_cleanup.sh

Happy Learning ..

Leave a comment

Your email address will not be published. Required fields are marked *