1

I have a set of files with the following filename format <epochtimestamp>.topic-of-post.md, for example 1436310000.server-3-2-1.md.

I need to batch convert the timestamp part to human readable date in the format of YYYY-MM-DD, example 2014-10-14.token-revocation.md

How could I go about automating this process on a Windows system? I have Cygwin installed and some basic knowledge of bash scripting.

3 Answers3

1

With the Perl rename command, you can do

rename 'use Time::Piece; s/(^\d+)/my $t = localtime($1); $t->ymd/e' [0-9]*.md
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

If it matters, the epoch stamp you gave doesn't match the date you used.

$: date -d@1436310000 +'%Y-%m-%d'
2015-07-07

Loop through each file, slice out the epoch, convert to the new format, then rename it. Show us what you tried if it gives you trouble.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
0
   # Get each file under the current directory 
   for f in *.md; 
    do 
      # Get the date portion of the file name
      orig_date=$(echo "${f%%.*}"); 
      # convert it to a human readable format
      cnv_date="$(date -d@$orig_date +%Y-%m-%H)"; 
      # rename
      rename "${orig_date}.server-3-2-1" "${cnv_date}.token-revocation" $f;  
    done

This works for me on a GNU bash-4.2.26 and rename-2.23.2

If you don't have rename, you can use the mv command.

mv  "${orig_date}.server-3-2-1.md" "${cnv_date}.token-revocation.md"

or

mv "$f" "${cnv_date}.token-revocation.md"
iamauser
  • 11,119
  • 5
  • 34
  • 52
  • My rename version is `rename from util-linux 2.25.2`. My follow up question is how would I approach this without having to enter each file's name or "portion" after the date separated with a `.`? – Nedislav Denev Aug 23 '18 at 19:41