LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Calculate Time Period in Scripting (https://www.linuxquestions.org/questions/linux-newbie-8/calculate-time-period-in-scripting-868900/)

anishkumarv 03-16-2011 07:03 AM

Calculate Time Period in Scripting
 
Hi all,

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.


With Regards
Anish Kumar.V
Edit/Delete Message

rtmistler 03-16-2011 08:00 AM

I haven't used it in a while, but look into logrotate:

http://linux.die.net/man/8/logrotate

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.

anishkumarv 03-16-2011 08:35 AM

Hi rtmistler,

Thanks for your reply.

Code:

mkdir anish | ls -l /root/>> file1 && grep anish file1

using this code i can able to save the directory creation time , like this


Quote:

drwxr-xr-x 2 root root 4096 Mar 16 18:48 anish
this directory has to be deleted automatically after three months, is it possible with the help of this (Mar 16 18:48) date/time to calculate.


With Regards
Anish Kumar.V

anishkumarv 03-16-2011 10:16 AM

Hi all,

Is it possible using TimeStamp!!! concept

theNbomr 03-16-2011 11:17 AM

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.

anishkumarv 03-16-2011 01:12 PM

Hi theNbomr,

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.....


With Regards
Anish Kumar.V

theNbomr 03-16-2011 03:01 PM

Something like this, perhaps.
Code:

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.

--- rod.

chrism01 03-16-2011 08:03 PM

You can also look at running a script (possibly using find) from within cron, as this (cron scheduling) uses calendar mths.

anishkumarv 03-17-2011 02:00 AM

HI chrism01,

Can you post any example, i am quite confused with find command, just now only started to reading the man page of find.


With Regards
Anish Kumar.V

anishkumarv 03-17-2011 06:03 AM

Hi all,


using this command i stored the details in one file which are created before 20 days.

Code:

find /root/testing/ -ctime -20 > file

Then using cut command

Code:

cut -d"/" -f4 file
I separated the directory name which are created before 20 days alone like this,
Quote:

anish
dinesh
linux
now i want to send mail to admin these are the dir 20 days expired like that...how we can do that in scripting please guide me to do....


With Regards
Anish Kumar.V

theNbomr 03-17-2011 08:56 AM

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:
Code:

echo $(basename /home/user/directory/file.name)
file.name

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
--- rod.


All times are GMT -5. The time now is 04:52 PM.