Epoch timestamp file name conversion to date

3

Expensive;

I have a list of files (140,000) dated in the format Unix epoch timestamp in the filename. I need to convert each file to match its actual date by changing its name. Example: 1475279740.15044_xxx.xxx.stats, where the epoch timestamp is 1475279740, converting gives 2016-09-30 (2016-09-30_xxx.xxx.stats).

I have the list of files with their names in timestamp and another file with the list of names already converted, both in txt . However, I need to change / move the file containing the timestamp to the converted files.

I imagine having two for where one opens the list of timestamp files and another for that opens the converted files and then would only change / move with a simple command mv .

To test, I created these two loops for , but only the second variable is changed in sequence, the first one stays static.

Here is the sample code:

 for x in $(cat timestamp.txt)
     do
         for y in $(cat timestamp-conv.txt)
       do

           echo $x convertido para $y

    done
 done

Expected code output:

  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-24
  1474566212 convertido para 2016-09-25
  1474566212 convertido para 2016-09-25

The two lists are identical, line by line between the two hits the timestamp with the other already converted.

I have tried in many ways, without success!

Can you help me?

    
asked by anonymous 19.10.2016 / 22:27

1 answer

1

I believe this script can do the job:

#!/bin/bash

exec 3< timestamp.txt

while read arq <&3; do
    epoch=$(echo $arq | awk '{ print $1 }' FS="_")
    filenameend=$(echo $arq | awk '{ print $2 }' FS="_")
    date=$(date --date="@$epoch" +%Y-%m-%d)
    mv ${arq} ${date}_${filenameend} && echo ${arq} convertido para ${date}_${filenameend}
done

exec 3<&-
    
20.10.2016 / 17:10