BASH script to delete accessed files



BASH script to delete accessed files

BASH script to delete accessed files

More videos like this online at http://www.theurbanpenguin.com
I needed this script, or at least a variant of it for a project I was working on for automatic deployments of XenServer using PXE. The XenServers would be set to PXE boot and they would collect their configuration and install, bit of course on subsequent boots we would not want to run the install again. To achieve the result a script runs and deletes the configuration file for each server as it is read. To start we must understand the last access time for a file. This changes when a file is read and we can access it with:
stat -c %X filename
Now that we now how to retrieve a usable date format and run calculations on those dates we can move on with the script. The script will create a reference file at the start of the process. We read the access time for the reference file, and file accessed after that date and time then has been read and we can delete it. The script can be run it the background or from cron what ever matches your needs. For this demonstration script we will run it in the background and the script will look for log files that have been accessed and then they are deleted.