Need Help to Implement a Shell Script for Deleting Directories Older then 122 Days
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
One problem you will have is that a directory is not modified when files within the directory are modified. And deleting a file will also update the directory modification time.
Another is that subdirectories will isolate newly created files from being reflected in any dates of old directories...
So you can only delete directories that are empty... and older than a given time.
and you do not need to write a script, because the command find itself can handle it (just look at the man page and the examples about deleting old files/empty dirs)
The problem is that even find is not likely to work for what is being requested...
The time delay to delete directories is AT LEAST twice the time delay to delete files... And since it is a recursive activity, it tends to accumulate rapidly with the depth of the directory tree.
I don't think find records timestamps before it scans a directory (scanning will update access times...) as that starts to require a relatively unbounded stack. I admit, stack usage isn't that critical anymore, but still, it is something to think about.
if you do not want to explain things but implement the original requirement: find is completely capable doing it. OP did not tell anything about the content of those directories, so probably there will be no any conflict with the files inside.
(since it was a backup directory probably all the files inside are older...)
I wonder where the specification of 122 days came to be ... Or is this an assignment?
If directory names represent the date, then providing the naming rules are accurate and consistent you should be able to determine based on current date/time what 122 days back is and then perform this deletion, a script would be just computing the correct file name point.
Please make some sort of script effort, post what you have, where you're stuck. And if it is homework, then it is OK to say that, but yes the right thing to do is work at it, and ask questions, not for the forum members to suggest an exact script for you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.