ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.