通过插入和_字符重命名UNIX中的多个DateTime文件
我想重命名的目录中有许多文件,以便根据某个约定可以识别它们:
SURFACE_OBS:2019062200
SURFACE_OBS:2019062206
SURFACE_OBS:2019062212
SURFACE_OBS:2019062218
SURFACE_OBS:2019062300
。
等
SURFACE_OBS:2019-06-22_00
SURFACE_OBS:2019-06-22_06
SURFACE_OBS:2019-06-22_12
SURFACE_OBS:2019-06-22_18
SURFACE_OBS:2019-06-23_00
I have many files in a directory that I want to rename so that they are recognizable according to a certain convention:
SURFACE_OBS:2019062200
SURFACE_OBS:2019062206
SURFACE_OBS:2019062212
SURFACE_OBS:2019062218
SURFACE_OBS:2019062300
etc.
How can I rename them in UNIX to be as follows?
SURFACE_OBS:2019-06-22_00
SURFACE_OBS:2019-06-22_06
SURFACE_OBS:2019-06-22_12
SURFACE_OBS:2019-06-22_18
SURFACE_OBS:2019-06-23_00
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
MV
和参数扩展的bash shell循环可以做到:此循环拾取与模式匹配的每个文件:
*
- nothing[[:digit:]] [:digit:]] [:digit:]] [:digit:]] [:digit:]] [[:digit:]] [ :]]
[: digit 所需的位置。
我已经仔细选择了通配符,以便它试图匹配“输入”文件而不是更名的文件。如果您的实际文件名具有导致通配符失败的边缘案例,请根据需要调整模式(因此第二次将文件重命名)。
A bash shell loop using
mv
and parameter expansion could do it:This loop picks up every file that matches the pattern:
*
-- anything:
-- a colon[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]
-- 10 digits... and then renames it by inserting dashes and and underscore in the desired locations.
I've chosen the wildcard for the loop carefully so that it tries to match the "input" files and not the renamed files. Adjust the pattern as needed if your actual filenames have edge cases that cause the wildcard to fail (and thus rename the files a second time).