1

I didn't got the script from imapsync to rename maildir filenames to work. :-/

So what I need is:

I have a mail folder with thousands of mails. After importing those emails to my new server, the filename of the emails got the creation date as a Unix timestamp in the filename, but the creation date flag of the file is the correct receive date from the email.

ls -l for one file looks like this:

-rw-r--r-- 1 popuser popuser  1350432 2013-03-16 07:22 1363563215.M562903P29332V0000000000000802I0000000000AEA46B_527.my-domain.org,S=1350432:2,S

So what the script has to do is: 1) read the creation date/time of the file (I found the command

stat -c %y filename

does this)

2) convert the date/time from 1) to a Unix timestamp

date -d "2013-03-17 11:19:01.000000000 +0100" "+%s"

3) delete the first 10 digits (wrong timestamp) of the filename and us the the timestamp from 2) instead

4) do this for all files in a specific directory

I'm a newby in Linux scripts, can anyone help me with this script?

Thank you!

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
kapale
  • 535
  • 1
  • 7
  • 15

1 Answers1

2

Try doing this with rename :

$ rename -n 's/^\d+/(stat($_))[9]/e' [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*

from the shell prompt. It's very useful, you can put some code like I does in a substitution for stat with the e modifier.

You can remove the -n (dry-run mode switch) when your tests become valids.

warning There are other tools with the same name which may or may not be able to do this, so be careful.

If you run the following command (linux)

$ file $(readlink -f $(type -p rename))

and you have a result like

.../rename: Perl script, ASCII text executable

and not containing:

ELF

then this seems to be the right tool =)

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

$ sudo update-alternatives --set rename /path/to/rename

(replace /path/to/rename to the path of your perl's rename command.


If you don't have this command, search your package manager to install it or do it manually


Last but not least, this tool was originally written by Larry Wall, the Perl's dad.


Edit

As stated here, if you have the following error :

Argument list too long

Then use find like this :

find -type f -name '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*' -print0|
    xargs -0 -n1 rename -n 's/^\d+/(stat($_))[9]/e' 

(try it without -n1, that should works too)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • thx for this great answer! It worked with a test directory where I have 500 files. I've only one issue left, the directory has 13 Mio. files... it looks like that's too much for this :-( is there a way to solve this issue easily? Error Message: -bash: /usr/bin/rename: Argument list too long – kapale Mar 18 '13 at 11:27
  • I don't know what you mean by 'too much for this', have you tried it ? Do you have any error ? Is your PC get inflamed ? Tell us. – Gilles Quénot Mar 18 '13 at 11:31
  • Yes I have tried it, as I wrote in my last post. The error message from my ubuntu 10.04 machine was: "-bash: /usr/bin/rename: Argument list too long" with less files in the directory it worked. – kapale Mar 18 '13 at 12:39