如何重命名以 Unix 时间戳开头的多个文件 - imapsync 问题 [英] How do I rename multiple files beginning with a Unix timestamp - imapsync issue
问题描述
我没有从 imapsync
获得脚本来重命名 maildir 文件名以使其正常工作.:-/
所以我需要的是:
我有一个包含数千封邮件的邮件文件夹.将这些电子邮件导入我的新服务器后,电子邮件的文件名将创建日期作为文件名中的 Unix
时间戳记,但文件的创建日期标志是来自电子邮件的正确接收日期.
ls -l 对于一个文件如下所示:
-rw-r--r-- 1 popuser popuser 1350432 2013-03-16 07:22 1363563215.M562903P29332V0000000000000802I0000000000AEA46B_527.my-domain.org,S=1350432:
所以脚本要做的是:1)读取文件的创建日期/时间(我找到了命令
stat -c %y 文件名
这样做)
2) 将日期/时间从 1) 转换为 Unix 时间戳
date -d "2013-03-17 11:19:01.000000000 +0100" "+%s"
3) 删除文件名的前 10 位数字(错误的时间戳),取而代之的是 2) 中的时间戳
4) 对特定目录中的所有文件执行此操作
我是 Linux
脚本的新手,谁能帮我解决这个脚本?
谢谢!
尝试使用 还有其他同名的工具可能会也可能不会这样做,所以要小心.
如果你运行以下命令(linux
)
$ 文件 $(readlink -f $(type -p rename))
你会得到类似
的结果.../rename:Perl 脚本,ASCII 文本可执行文件
且不包含:
ELF
那么这似乎是正确的工具 =)
如果没有,将其设为 Debian
和 Ubuntu
等衍生产品的默认设置(通常已经如此):
$ sudo update-alternatives --set rename/path/to/rename
(将 /path/to/rename
替换为 perl 的 rename
命令的路径.
如果你没有这个命令,搜索你的包管理器来安装它或 手动操作
<小时>最后但同样重要的是,这个工具最初是由 Perl 的父亲 Larry Wall 编写的.
<小时>编辑
如上所述 这里,如果您遇到以下错误:
<块引用>参数列表太长
然后像这样使用 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 重命名 -n 's/^d+/(stat($_))[9]/e'
(不带-n1
试试,应该也可以)
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!
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 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.
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)
这篇关于如何重命名以 Unix 时间戳开头的多个文件 - imapsync 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!