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

Since creation time of a file is not saved in the inode, you will have to store it somewhere else. One way could be to maintain a list where the directory name and creation timestamp is stored in to keep track of it.
This one you could parse and calculate it's age to decide if it's time to delete it.
If allowed in your environment, you could code the timestamp into the directory name so you would not have to maintain an extra text file.

Date arithmetics are often being discussed about in the forum - try using the search function.
You can also check out this:
http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html

Hi zaxxon,

Thanks for your reply.

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

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

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

---------- Post updated 03-17-11 at 04:37 PM ---------- Previous update was 03-16-11 at 07:01 PM ----------

Hi all,

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

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

Then using cut command

cut -d"/" -f4 file

I separated the directory name which are created before 20 days alone like this,

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

I don't quite understand your question - specifically the part in red color. My hunch is that you want to email the information in your file. It might be a good idea to give an example of the email you want to send.

Otherwise, maybe you want to do something like this -

(str=""; while read x; do [ "$str" = "" ] && str="$x" || str="$str,$x"; done <input; echo $str) | mailx -s"these are the dir 20 days expired like that" admin@yoursite.com

where "input" is your input file containing all the information.

tyler_durden

Hi durden_tyler,

Thanks for your Reply

#!/bin/bash
cut="cut -d"/" -f4 anish"
for i in $"(find /anish -ctime -20 > anish)"
do $cut|mail root@localhost -s "These directories are 20 days old "
done

above script which i wrote for to find the directories are created before 20 days old and that deatails automatically send to admin throug mail. this is right way?? or any other way is available??

With Regards
Anish Kumar.V

You tell me.
Does it work?
If it does, then it is the right way.
If it doesn't, then it is the wrong way.

tyler_durden