LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 07:03 AM   #1
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

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, 08:00 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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, 08:35 AM   #3
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

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, 10:16 AM   #4
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
Hi all,

Is it possible using TimeStamp!!! concept
 
Old 03-16-2011, 11:17 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
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, 01:12 PM   #6
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

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, 03:01 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
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 04:52 PM.
 
Old 03-16-2011, 08:03 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 02:00 AM   #9
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

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, 06:03 AM   #10
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

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, 08:56 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
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 09:18 AM.
 
  


Reply



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? linustalman Linux - General 2 12-14-2010 03:18 AM
is there any time period for taking 310-202 LinuxLover Solaris / OpenSolaris 0 10-11-2007 02:06 AM
FC5 slows down after period of time asidarin Linux - Server 19 06-25-2007 10:46 AM
Long period of time to install Peter_APIIT Fedora 4 03-25-2007 12:03 AM
Running a program for a specific period of time CaptainHowdy Linux - Software 2 08-22-2004 07:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:54 PM.

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
Open Source Consulting | Domain Registration