Results 1 to 10 of 10
ok,
so i have an application that creates files labeled as follows
filename-timestamp-arc.tgz
or in literal terms
pers-120249001-arc.tgz
I need to make a bash script that:
1. loops through the ...
- 02-11-2008 #1
doing math on unix timestamp
ok,
so i have an application that creates files labeled as follows
filename-timestamp-arc.tgz
or in literal terms
pers-120249001-arc.tgz
I need to make a bash script that:
1. loops through the files
2. explodes the file name and extracts the timestamp
3. checks if the timestamp is more than X days old
4. if the timestamp is more than X days old, the file gets deleted.
I can pretty much handle the bulk of the work myself.
Where I am getting stuck is on the actual command of exploding the file name
and also on getting the current timestamp minus 5 days.
I've looked around at the date command but i can't seem to find anywhere that explains how do the math on a date.
in PHP i would do a stringtodate command and then get today's date - 5 days, I assume there is a similar function but I can't seem to find it.
Thanks in advance
-P
- 02-11-2008 #2
I am totally, totally confused.
When you say "explode the filename", do you mean extract the numerical part from the rest of the filename?
And if so, what kind of date is "120249001"? What's the year, what's the month, what's the day, is there a time?--
Bill
Old age and treachery will overcome youth and skill.
- 02-12-2008 #3
By explode the filename, I assume that you mean splitting it up into its parts. If all you need is the timestamp, you could do:
This tells cut to use '-' as a delimiter between fields, and give you back the second field. Alternatively, if you actually want all three parts, you could do:Code:echo "$filename" | cut -d- -f2
This will remove the ".tgz" from the end and then split the filename the same way as before, printing each part out on its own line.Code:echo "$filename" | sed -e 's/\.tgz$//' | cut -d- --output-delimiter="\n" -f1-3
wje's question in regards to the timestamp is valid. If this is seconds since the Epoch, you can just do plain arithmetic on it.DISTRO=Arch
Registered Linux User #388732
- 02-12-2008 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
assume 2nd field really is the epoch in seconds
Code:echo "pers-120249001-arc.tgz" | awk 'BEGIN{FS="-" current=systime() day=5 } { if ( ( current-$2 ) > convertsec(day) ) { print "found " $0 " more than 5 days" } } function convertsec(day) { return 86400*day }'
- 02-12-2008 #5
Of course!
I don't know why I didn't recognize it before, but the number in pendal's example looks very much like "now" as the number of seconds since the Epoch, but divided by 10:
so if we multiply his example by 10 and subtract it from the moment I show above, we get 3 days 12 hours 3 minutes 35 seconds.Code:120249001 # his example 1202792625 # "now" (approximately)
So, Cabhan, maybe he is using raw seconds and just copied it wrong.
pendal? Is this true?
If so, you can use Cabhan's advice for isolating the number.
Assuming my guess is correct, you might find this script some help (but be sure to put in all ten digits of the time):
Code:#!/bin/bash now=$(date '+%s') echo $now acceptable_days=5 while read filename do file_time=$(echo "$filename" | cut -d- -f2) if [ $(( $now - $file_time >= $acceptable_days*24*60*60 )) = 1 ] then echo 'delete this one' else echo 'keep this one' fi done--
Bill
Old age and treachery will overcome youth and skill.
- 02-12-2008 #6
oops
Sorry, ghostdog74, I didn't see your post before I finished mine, or I would have acknowledged it.
--
Bill
Old age and treachery will overcome youth and skill.
- 02-12-2008 #7Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 02-12-2008 #8
My apologies of creating so much confusion. The timestamp in my filename is the unix epoch time stamp. I may have copied it wrong.
wje_lf got it right. the script he put together will work perfectly.
Man! I have to say I am really thankful for this forum. I have never gotten this much help on a mistyped post.
THIS PLACE IS GREAT!!!!
-P
- 02-12-2008 #9
Quoth the esteemed ghostdog74 (cool name, by the way):
Each of us has a different style for helping people who come here. Well, actually, each of us probably has several different styles, depending on the situation.btw, think you forgot to input redirect (or pipe?) the "filename" to the while loop.
In this case, my reasoning was this:
- I had no idea what exactly pendal would be piping the filenames from, so I didn't want to clutter the script.
- I wanted the script to be a simple demonstration of concept, so he could enter filenames and play with it and see what happened.
- I remembered his comment:
so I figured I'd just answer his basic question and then step out of the way.I can pretty much handle the bulk of the work myself.
This doesn't mean that adding in some piping machinery would have been wrong. It just wasn't the direction my horse led me at the time. :)
Edit: 512 posts! Power of 2! w00t!--
Bill
Old age and treachery will overcome youth and skill.
- 02-12-2008 #10I completely agree and thanks for remember I didn't expect anyone to do ALL the work for me.3. I remembered his comment:
Quote:
I can pretty much handle the bulk of the work myself.
so I figured I'd just answer his basic question and then step out of the way.
This doesn't mean that adding in some piping machinery would have been wrong. It just wasn't the direction my horse led me at the time.
Just wanted some guidance.
Thanks again,
-P


Reply With Quote
