I know there was a post on here at one point about how to get file properties such as create time and such, but I've searched high and low without success.
What I need to do is create a script that runs daily that will expire or remove files that are older than 1 month, or possibly a number of days, within a directory. I think I understand how to make the script run daily using the crontab.. but any help regarding how to check dates and remove the files accordingly would be greatly appreciated.
The find command with the mtime or ctime flags will do this for you:
find /search/path -mtime +30 -exec rm -i {} \;
I put the -i flag here to prompt you for each file removed (linux defaults to this as I remember) - probably a better approach would be to copy them all to another directory and then only delete them once you are sure you want to get rid of them all.
I'd like to offer my thoughts on ctime verses mtime. Sorry, but this will be a little verbose.
First, if you change the contents of a file you change the mtime of a file. Since this is a change to the inode, ctime is updated as well. The mtime is bit like the date on a letter and ctime is a bit like the postmark on the envelope.
You can set mtime to anything you want via the utime() system call or the touch command. Doing so sets the ctime and you cannot reset ctime.
If you restored last year's payroll records from tape, you might want to set the mtime back to the end of last year. But the ctime will reliably still indicate when the last change to the file occurred. This is how your backup program will know that it must back up the file. The ctime is really used by backup program. But an application program that prints out a payroll listing would use mtime.
Mmm, let's picture this: you change exactly 1 character in an ASCII text file, thus not changing the file size. Does this change the ctime as well? I always thought not?
Yes, even a one character write to a file without changing the file size will indeed update ctime. You can verify this with fewer keystrokes than you spent asking the question:
print -n a > x
ls -lc x
# wait a minute
print -n b > x
ls -lc x
Yep, I already tried it myself, but the - excellent - info you gave can be useful to other people as well. I am not a developer so I rarely wander outside the '1m' man world. Thanks for the quote anyway!