如何重命名以Unix时间戳开头的多个文件-imapsync问题 [英] How do I rename multiple files beginning with a Unix timestamp - imapsync issue

查看:100
本文介绍了如何重命名以Unix时间戳开头的多个文件-imapsync问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有从imapsync中获得脚本来重命名maildir文件名才能正常工作. :-/

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

所以我需要的是:

我有一个包含数千封邮件的邮件文件夹.将这些电子邮件导入到我的新服务器后,电子邮件的文件名在文件名中以Unix时间戳作为创建日期,但是文件的创建日期标志是来自电子邮件的正确接收日期.

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看起来像这样:

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

因此脚本要做的是: 1)读取文件的创建日期/时间(我找到了命令

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

这样做)

2)将日期/时间从1)转换为Unix时间戳

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

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

3)删除文件名的前10位(错误的时间戳),然后从2开始使用我们的时间戳

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

4)对特定目录中的所有文件执行此操作

4) do this for all files in a specific directory

我是Linux脚本的新手,有人可以帮助我使用此脚本吗?

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

谢谢!

推荐答案

尝试使用 rename :

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]*

shell提示符下输入

.这非常有用,您可以放置​​一些代码,例如我用e修饰符代替了 stat .

当测试变为有效时,可以删除-n(空运行模式开关).

from the shell prompt. It's very useful, you can put some perl 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.

有些其他具有相同名称的工具可能无法执行此操作,因此请小心.

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

如果运行以下命令(linux)

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

,结果为

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

且不包含:

ELF

那么这似乎是正确的工具=)

then this seems to be the right tool =)

如果没有,则将其设置为Debian和类似Ubuntu的派生类的默认设置(通常已经是这种情况):

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

(将/path/to/rename替换为perl's 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

最后但并非最不重要的是,该工具最初是由Perl的父亲拉里·沃尔(Larry Wall)编写的.

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

如所述

As stated here, if you have the following error :

参数列表太长

Argument list too long

然后像这样使用find:

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' 

(在没有-n1的情况下尝试,这也应该有效)

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

这篇关于如何重命名以Unix时间戳开头的多个文件-imapsync问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆