Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined! pendal's Avatar
    Join Date
    Jan 2008
    Posts
    15

    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

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    Code:
    echo "$filename" | cut -d- -f2
    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" | sed -e 's/\.tgz$//' | cut -d- --output-delimiter="\n" -f1-3
    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.

    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

  4. #4
    Linux 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
    }'

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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:
    Code:
    120249001    # his example
    1202792625   # "now" (approximately)
    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.

    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.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192

    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.

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by wje_lf View Post
    Sorry, ghostdog74, I didn't see your post before I finished mine, or I would have acknowledged it.
    hey wje, why apologize? this is forum, remember? anyone can post (legit posts of course).
    btw, think you forgot to input redirect (or pipe?) the "filename" to the while loop.

  8. #8
    Just Joined! pendal's Avatar
    Join Date
    Jan 2008
    Posts
    15
    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

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Quoth the esteemed ghostdog74 (cool name, by the way):
    btw, think you forgot to input redirect (or pipe?) the "filename" to the while loop.
    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.

    In this case, my reasoning was this:
    1. I had no idea what exactly pendal would be piping the filenames from, so I didn't want to clutter the script.
    2. I wanted the script to be a simple demonstration of concept, so he could enter filenames and play with it and see what happened.
    3. I remembered his comment:
      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. :)

    Edit: 512 posts! Power of 2! w00t!
    --
    Bill

    Old age and treachery will overcome youth and skill.

  10. #10
    Just Joined! pendal's Avatar
    Join Date
    Jan 2008
    Posts
    15
    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.
    I completely agree and thanks for remember I didn't expect anyone to do ALL the work for me.

    Just wanted some guidance.

    Thanks again,

    -P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...