0

I am receiving emails, the filename format is this (It is Unix format for date + .report-server)

1509106656.32538_1.report-server

Now I have several emails. I want to format all file names with new date time format like this.

dd-mm-yyyy.report-server
Zanna
  • 70,465
DaviD
  • 25

3 Answers3

1

How about this:

for i in *.report-server; do echo mv "$i" "$(date -d@${i%%.*} +%d-%m-%Y.${i##*.})"; done

This loops over the .report-server files in the current directory and for every file prints a line like

mv 1509106656.32538_1.report-server 27-10-2017.report-server

where you can check the file name. If it does what you want, simply remove echo from the command and run it to perform the renaming.

Be careful! If you end up having multiple files with the same date mv will (by default) silently overwrite the first ones. If you want to preserve the (presumably unique) .32538_1 part, change ${i##*.} to ${i#*.} in the command. If you want mv to ask before overwriting files, add the -i option.

dessert
  • 39,982
  • Its working thanks. i change mv to cp . And yes i noticed that but i am receiving email per day, it will work fine. – DaviD Jan 10 '18 at 12:26
  • @DaviD If you want to have the files sorted correctly I recommend using yyyy-mm-dd format instead – replace %d-%m-%Y with %F for that. – dessert Jan 10 '18 at 14:02
1

You can use the following script as a hint:

#!/bin/bash
for i in *.report-server; do
    timestamp=$(echo $i | cut -d. -f1)
    output_date=$(date +'%d-%m-%Y' -d @$timestamp)
    mv "$i" "${output_date}.report-server"
1

Here's a way to do it using perl-based rename or prename (essentially a duplicate of How can I batch convert folder names from one date format to another, with different format specifiers):

$ rename -n -- '
  BEGIN{use Time::Piece};
  s/(\d{10})\.\d{5}_1/Time::Piece->strptime($1, "%s")->strftime("%d-%m-%Y")/e
' *.report-server
rename(1509106656.32538_1.report-server, 27-10-2017.report-server)

Remove the -n (or change to -v if you want verbose output) when you are happy that it is doing what you want.

steeldriver
  • 136,215
  • 21
  • 243
  • 336