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


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


3 Answers 3


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.

  • 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, 2018 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, 2018 at 14:02

You can use the following script as a hint:

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"
  • Yes there was error extra operand ‘@1507285570’
    – DaviD
    Jan 10, 2018 at 12:27
  • @MukeshSaiKumar +1 You're welcome!
    – dessert
    Jan 10, 2018 at 12:40

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.