Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
now i am writting one bash script. in that my requirement is
i need to create one directory and that the directory details to be stored in one file Ex. date/time and all in one file.
after that i need to delete the folder automatically exactly after 3months.
between these time period in 2month itself i need to send one mail to admin "regarding this still one month only more to delete the folder" . is it possible to do like that date calculation in script.
You don't have to use it for the /var/log tree you can customize your profile to look at other files and delete those after they reach a certain size or age. Takes a bit of fooling with, but I used it to rotate and deprecate logs from an application I wrote so as to not overflow disk size but also keep the logs and offload them once they were swapped out.
The find command is very good at finding files and directories of a specified age. I would try to use it as the basis for keeping track of files matching various age thresholds. Keeping track of file and directory age information is redundant, since the filesystem already does this, and is automatically up-to-date.
--- rod.
is it possible using "FIND" command to find the age of the directories in a particular path, if more that 2 months of the folder age means its automatically triggered mail to ADMIN, like that.....
oldFiles=$(find /some/start/directory -ctime +60)
if [ "$oldFiles" != "" ]; then
echo $oldFiles | mail -s "Deleting files from /some/start/directory" ADMIN@your.site
fi
This assumes one month is 30 days. If that isn't good enough, you can play some games with the date command to find dates more precisely.
If you use '-ctime -20' in the argument to find, you will find files that are less than 20 days old (creation time; do you really mean modification time?). From your original post, it sounded like you wanted to find files that are older than a certain age. To do that, you would want to change the leading minus sign in the numeric argument to a plus sign.
Also, the use of cut to trim the leading directory name from files relies on the case that the files found are exactly 4 levels deep from the root. Better would be to use the built-in basename function. Example:
In your original post, you said you were looking for directories beyond 3 months old. To find directories with find, you would use the -type d option. I think it makes more sense to deal with files & directories, and this seems to be what you have adopted.
My example showed how to send the list of files to a user by e-mail:
Code:
echo $oldFiles | mail -s "Deleting files from /some/start/directory" ADMIN@your.site
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.