Jerwin Roy Jerwin Roy - 1 year ago 130
Bash Question

Converting date format in bash

I have similar different file of the format

is it possible to rename using bash script to
for all files.
Kindly suggest some method to fix this.

Original file:


Expected output:


Answer Source

To perform only the name transformation, you can use awk:

echo 'backup_2016-30-10_12-00-00' |
  awk -F'[_-]' '{ print $1 "_" $3 "-" $4 "-" $2 "_" $5 ":" $6 ":" $7 }'

As fedorqui points out in a comment, awk's printf function may be tidier in this case:

echo 'backup_2016-30-10_12-00-00' |
  awk -F'[_-]' '{ printf "%s_%s-%s-%s_%s:%s:%s\n", $1,$3,$4,$2,$5,$6,$7 }'

That said, your specific Linux distro may come with a rename tool that allows you to do the same while performing actual file renaming.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download