Monday, August 9, 2010

Bash Scripts with a Variable - Show/Delete Files Older than x Days

Show files and directories more than 15 days old:
find /path/to/files/* -mtime +15

Delete files more than 15 days old:
find /path/to/files/* -mtime +15 -exec rm {} \;

These can, of course, be scripted (and cron'd, if you set the -mtime flag instead of using a variable). A script to show the files and directories older than x days would look like:

#!/bin/bash --
find /path/to/files/* -mtime $1


And to delete those same files:

#!/bin/bash --
find /path/to/files/* -mtime $1 -exec rm {} \;


Just remember to set the permissions of the script to allow execution, and pass the variable on the command line like so:
# MyScriptName +15

howtogeek.com has a detailed explanation of the command.

0 comments:

Post a Comment

 
Contact our honeypot department if you are desperate to get blacklisted.