2

I moved a bunch of emails using imap over to the server. When they loaded onto the sever the date modified is the same date for all the emails. So when I am using imap in mac mail it displays all of my emails as coming in on Sept. 30th.

The header of the emails contain the correct date so roundcube displays the correct date because I think it is pulling the information from the header.

Mac mail and other mail programs are pulling the information from the date created, modified and/or accessed. (I am not sure which one so I changed them all)

I manually was able to change the date modified, created, and accessed to match the header sent date, but it is a lot of work to do that for 2000 emails.

Do you know of a way I can change the modified and created date as a batch that will make them match the header date inside each email file?

Anyway to do it through cron?

I can use bash in Ubuntu or use windows either one will work.

The header in the file looks like this:

Return-Path: <jane@example.com>
From: <jane@example.com>
To: <joe@example.com>
Cc: "Mike" <mike@example.com>
Subject: Example Subject
Date: Mon, 29 Sep 2014 10:23:34 -0400
Message-ID: <763765530.23306569.1412000614673.JavaMail.root@example.net>
MIME-Version: 1.0

The date line by itself:

Date: Mon, 29 Sep 2014 10:23:34 -0400

adjc98
  • 37
  • 8

1 Answers1

4

First, cd to the directory where the files are. Then run:

for f in *
do
    touch -d "$(sed -n 's/^Date://p' "$f" | head -n1)" "$f"
done

The above uses sed to extract the "Date:" information from the file and then uses touch to assign that date to the file.

This was tested using GNU tools. Mac OSX tools sometimes differ.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • Actually it keeps saying on some of the files: touch: invalid date format ` Mon, 4 Feb 2013 15:48:19 -0400\n Mon, Feb 4, 2013 at 1:47 PM\n Mon, Feb 4, 2013 at 1:47 PM
    Subject: Matt's Email
    To: ='
    – adjc98 Oct 03 '14 at 23:21
  • @user862 That would mean that the file had more than one "Date:" header. To get around that, I updated the answer to select just the first. – John1024 Oct 03 '14 at 23:28
  • Ok thanks will try again with the new parameters. I was thinking it had multiple date fields in some of the emails and was trying to use q. I see you are using -n1 instead. Can you explain | head -n1? – adjc98 Oct 04 '14 at 00:23
  • `|` is the pipe symbol. It connects the stdout of the `sed` command to the `stdin` of the `head` command. `head` is a good thing to know about. It is one of the core unix utilities. It prints the beginning of the file. The option `-n1` tells it to print just the first line. There is a corresponding utility, `tail`, that prints the end of a file. (A solution using `q` with `sed` would also be possible.) – John1024 Oct 04 '14 at 00:42