-   Programming (
-   -   'find' command changes directory access time (

prollocks 10-05-2007 04:16 AM

'find' command changes directory access time
Hello all,

I'm writing a small script to traverse a set of directories and delete all files older than X days (including directories).

# find and delete all files/dirs older than 8 days
find . -mtime +8 -print0 | xargs -0 -r rm -rf

This should be simple and mostly works - however if there is a directory that is newer than the number of days I'm checking for its atime becomes the current date (files within that directory remain timestamped with their original dates).

Why does the 'find' command do this ? and is there a way around it?



cconstantine 10-05-2007 09:05 AM

That's the *file system* works. When a program reads the directory's inode to determine the directory contents, the file system updates the atime (access time) property on the inode. It doesn't matter if the program doing the access is 'ls' or 'find' or whatever.

You could make your script remove only *files* which are over the age limit as a first pass, then go back and prune directories which are empty.

gnashley 10-05-2007 10:56 AM

You could also remount the filesystem where the directories are using the noatime option while doing the operation.

prollocks 10-05-2007 11:57 AM

Ok -

I did some more testing and found that atime is only changed if a file is deleted from within a directory - newer files within that directory are left alone (dates untouched). So it seems directory atime only changes if something happens i.e deletion within that directory.

I may now check for empty dirs and delete them as an insurance, but wanted to keep things as simple as possible.



All times are GMT -5. The time now is 08:19 PM.