bash – Filename conversion from epoch timestamp to date

Question:

Expensive;

I have a list of files (140k) with date in Unix format 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 for the converted files.

I imagine having two for where one opens the list of timestamp files and the other for which opens the converted files and then just change/move with a simple mv command.

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

Here is the example 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 matches the timestamp with the other already converted.

I've tried it in several ways, without success!

Can you help me?

Answer:

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<&-
Scroll to Top