LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise
User Name
Password
Linux - Enterprise This forum is for all items relating to using Linux in the Enterprise.

Notices


Reply
  Search this Thread
Old 10-27-2004, 04:00 PM   #1
Bubba235
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Rep: Reputation: 0
Newbie scripting question, dates


I am trying to automate backup scripts and I am able to create date specifc directories by using the string:
mkdir /hotbackup/$(date +%m%d%Y)
This creates a directory named /hotbackup/10272004 which is exactly what I want. The issue I am having is I can't figure out how to create/remove a directory from a previous day. I have tried
rm /hotbackup/$(date +%m%d%Y)-1
rm /hotbackup/$(date-1 +%m%d%Y)
rm /hotbackup/$(date +%m%d-1%Y)
and they all fail.
Any help would be greatly appreciated.
Thank you.
 
Old 10-27-2004, 04:06 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
You could use:
today=`date +%d`
yesterday=$(($today-1))

Note that you will also need to take into account the 1st of each month, especially in January.

Personally I like to use the unix time (%s) which is the number of seconds since 1970, this way you only need to know the number of seconds in a day etc and not wory about the archaic - near imposible to compute - gregorian calendar.
 
Old 10-27-2004, 05:09 PM   #3
chort
Senior Member
 
Registered: Jul 2003
Location: Silicon Valley, USA
Distribution: OpenBSD 4.6, OS X 10.6.2, CentOS 4 & 5
Posts: 3,660

Rep: Reputation: 76
If you don't do the time since epoch, you could use printf and a lot of manual calendar definition. I did this once in PERL and the results were good, but it takes a lot of thought with different number of days per month, roll over from one month to the next, one year to the next, daylight time, etc, etc... It seems simple until you start thinking about it...

On second thought, that time since epoch idea isn't looking so bad
 
Old 10-27-2004, 05:10 PM   #4
Bubba235
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for the replies. I was hoping for a quick 'Do this -1' but it looks like it's going to be more difficult that that.
 
Old 10-27-2004, 05:23 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
If you are open to the idea of using perl then there is a "parsedate" function which is pretty good at taking a human readable date, changing it into the seconds since the epoch which can then be modified and turned back into a time using the localtime function.
 
Old 10-28-2004, 03:51 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Normally, you use date in the file name so you can look at it and know when it was created. If you want to do something to that file based on it's creation date or other options, the find tool has several options ( man find ) like mtime , atime , ctime .

Here is an example where the backups and logs which are older than 3 months are deleted.

Edit: Guess I should make the err messages go to a separate file....
2> /mnt/backup/${filename}err.log

Code:
#!/bin/bash
filename=`date '+%m%d%y'`
/bin/tar -cvzf /mnt/backup/${filename}.tar.gz . \
> /mnt/backup/${filename}.log \
2> /mnt/backup/${filename}err.log
#Delete old files with the following command
find /mnt/backup -type f -name '*.gz' , -name '*.log' , \
-mtime +90 -exec rm {} \;

Last edited by homey; 10-28-2004 at 04:40 PM.
 
Old 10-28-2004, 04:42 PM   #7
Bubba235
LQ Newbie
 
Registered: Oct 2004
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you, I think you solved the problem I was having, great job! I appreciate it.
 
Old 10-28-2004, 05:51 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
You're welcome!

Oh, I always charge extra if you appreciate it.

and just in case you change your mind

I charge double if you don't appreciate it.
 
Old 11-05-2004, 12:06 AM   #9
subhasis_ray
Member
 
Registered: Jul 2001
Location: india
Distribution: RedHat 7.1,7.2,7.3, 8.0,9.0,Fedora,EL2.1,EL3.0
Posts: 103

Rep: Reputation: 16
Just one more way to do it.....

use the perl module Date::Manip, and create a script that takes in the number of days u want to go back, calculate the date and return it to u. All u need to do then is to call this script and pass it the number of days to go back...... your job is done

-Subhasis
 
  


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
Newbie Bash Scripting Question jsaxton86 Programming 2 11-26-2005 07:58 PM
Newbie Bash scripting question Caidence Programming 2 08-03-2003 04:53 AM
Newbie bash scripting question... cmfarley19 Linux - Newbie 2 05-03-2003 08:26 AM
Newbie Scripting, please help granny Programming 6 04-24-2003 11:46 PM
A Newbie,s Scripting Question newpenguin Linux - Newbie 7 10-07-2002 12:52 PM

LinuxQuestions.org > Forums > Enterprise Linux Forums > Linux - Enterprise

All times are GMT -5. The time now is 12:13 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
Open Source Consulting | Domain Registration