LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-16-2011, 08:03 AM   #1
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
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
 
Old 03-16-2011, 09:00 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
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.
 
Old 03-16-2011, 09:35 AM   #3
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
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
 
Old 03-16-2011, 11:16 AM   #4
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
Hi all,

Is it possible using TimeStamp!!! concept
 
Old 03-16-2011, 12:17 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 03-16-2011, 02:12 PM   #6
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
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
 
Old 03-16-2011, 04:01 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 03-16-2011 at 05:52 PM.
 
Old 03-16-2011, 09:03 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You can also look at running a script (possibly using find) from within cron, as this (cron scheduling) uses calendar mths.
 
Old 03-17-2011, 03:00 AM   #9
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
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
 
Old 03-17-2011, 07:03 AM   #10
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
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
 
Old 03-17-2011, 09:56 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 03-17-2011 at 10:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How do I calculate the hours usage per day over a certain period? LinusStallman Linux - General 2 12-14-2010 04:18 AM
is there any time period for taking 310-202 LinuxLover Solaris / OpenSolaris 0 10-11-2007 03:06 AM
FC5 slows down after period of time asidarin Linux - Server 19 06-25-2007 11:46 AM
Long period of time to install Peter_APIIT Fedora 4 03-25-2007 01:03 AM
Running a program for a specific period of time CaptainHowdy Linux - Software 2 08-22-2004 08:02 PM


All times are GMT -5. The time now is 08:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration