Find the answer to your Linux question:
Results 1 to 3 of 3
All, I have searched for a solution to this and tried numerous things, but I am limited on my abilities with grep/awk/shell scripting etc and spent too many hours already. ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    5

    Smile converting epoch in a file name to human readable date/time

    All,

    I have searched for a solution to this and tried numerous things, but I am limited on my abilities with grep/awk/shell scripting etc and spent too many hours already. Maybe someone smarter than I can easily help me with a quick one-liner, perl/python script, etc...?

    I have log files that everyday are downloaded from my webserver in the format:
    Code:
    samplesite.com.xxxxxxxxxxx.gz
    xxxxxxxxxx is a 10 digit epoch time.

    I am trying to figure out a way in batch to:

    1. find all of exisiting files containing the pattern (after the first run it will only be one a day)
    2. Isolate the epoch string
    3. convert the epoch string to human readable date/time
    4. rename the original file as samplesite.com.mmddYYYY.gz

    Thanks so much!

  2. #2
    Just Joined! fabtk's Avatar
    Join Date
    May 2011
    Location
    Switzerland
    Posts
    6
    Hi sinusoid

    About step 1: Are you downloading these backup files manually or should this also happen automatically?

    For step 2, 3 and 4 I would do something like this ...

    Code:
    #!/bin/bash
    
    for i in *.gz
    do
      # getting length of timestamp (if 10 = timestamp - if not, already converted file)
      TSLENGHT=$(echo $i | cut -d'.' -f3 | awk '{print length}')
      if [ $TSLENGHT == 10 ]; then
        # cutting timestamp out of filename
        FILETIMESTAMP=$(echo $i | cut -d'.' -f3)
        # getting correct date format
        FILEDATE=$(date -d "1970-01-01 $FILETIMESTAMP sec" "+%d%m%Y")
        # renaming
        mv $i "samplesite.com.$FILEDATE.gz"
      fi
    done
    Greetings from Switzerland

    FabTK

    //edit: tested and works
    Last edited by fabtk; 05-06-2011 at 09:44 AM.

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Posts
    5
    FabTK --- this is FANTASTIC!

    Thank you SO much!

Posting Permissions

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