使用 sed 批量重命名文件 [英] Using sed to mass rename files

查看:45
本文介绍了使用 sed 批量重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

更改这些文件名:

  • F00001-0708-RG-biasliuyda
  • F00001-0708-CS-akgdlaul
  • F00001-0708-VF-hioulgigl

到这些文件名:

  • F0001-0708-RG-biasliuyda
  • F0001-0708-CS-akgdlaul
  • F0001-0708-VF-hioulgigl

外壳代码

测试:

ls F00001-0708-*|sed 's/(.).(.*)/mv & 12/'

执行:

ls F00001-0708-*|sed 's/(.).(.*)/mv & 12/' | sh

我的问题

我不明白 sed 代码.我明白什么是替代命令

I don't understand the sed code. I understand what the substitution command

$ sed 's/something/mv'

的意思.而且我对正则表达式有些了解.但我不了解这里发生了什么:

means. And I understand regular expressions somewhat. But I don't understand what's happening here:

(.).(.*)

或在这里:

& 12/

前者,对我来说,看起来就像是:一个字符,后跟单个字符,后跟任意长度的 a 序列单个字符"——但肯定不止于此.至于后半部分:

The former, to me, just looks like it means: "a single character, followed by a single character, followed by any length sequence of a single character"--but surely there's more to it than that. As far as the latter part:

& 12/

我不知道.

推荐答案

首先,我应该说最简单的方法是使用预命名或重命名命令.

First, I should say that the easiest way to do this is to use the prename or rename commands.

在 Ubuntu、OSX(Homebrew 包 rename、MacPorts 包 p5-file-rename)或其他具有 perl 重命名(prename)的系统上:

On Ubuntu, OSX (Homebrew package rename, MacPorts package p5-file-rename), or other systems with perl rename (prename):

rename s/0000/000/ F0000*

或在使用 util-linux-ng 重命名的系统上,例如 RHEL:

or on systems with rename from util-linux-ng, such as RHEL:

rename 0000 000 F0000*

这比等效的 sed 命令更容易理解.

That's a lot more understandable than the equivalent sed command.

但是对于理解 sed 命令,sed 联机帮助页很有帮助.如果你运行 man sed 并搜索 &(使用/命令进行搜索),你会发现它是 s/foo/bar/replacements 中的一个特殊字符.

But as for understanding the sed command, the sed manpage is helpful. If you run man sed and search for & (using the / command to search), you'll find it's a special character in s/foo/bar/ replacements.

  s/regexp/replacement/
         Attempt  to match regexp against the pattern space.  If success‐
         ful,  replace  that  portion  matched  with  replacement.    The
         replacement may contain the special character & to refer to that
         portion of the pattern space  which  matched,  and  the  special
         escapes  1  through  9  to refer to the corresponding matching
         sub-expressions in the regexp.

因此,(.) 匹配第一个字符,可以被1 引用.然后 . 匹配下一个字符,它总是 0.然后 (.*) 匹配文件名的其余部分,可以被 2 引用.

Therefore, (.) matches the first character, which can be referenced by 1. Then . matches the next character, which is always 0. Then (.*) matches the rest of the filename, which can be referenced by 2.

替换字符串使用 &(原始的filename) 和 12 是文件名的每个部分,除了第二个部分字符,这是一个 0.

The replacement string puts it all together using & (the original filename) and 12 which is every part of the filename except the 2nd character, which was a 0.

恕我直言,这是一种非常神秘的方式来做到这一点.如果对于某些原因重命名命令不可用,您想使用sed 进行重命名(或者您可能正在做一些太复杂的事情重命名?),在你的正则表达式中更明确会更更具可读性.也许是这样的:

This is a pretty cryptic way to do this, IMHO. If for some reason the rename command was not available and you wanted to use sed to do the rename (or perhaps you were doing something too complex for rename?), being more explicit in your regex would make it much more readable. Perhaps something like:

ls F00001-0708-*|sed 's/F0000(.*)/mv & F0001/' | sh

能够看到实际发生的变化s/search/replacement/使它更具可读性.它也不会保留如果你不小心运行它,从你的文件名中吸取字符两次什么的.

Being able to see what's actually changing in the s/search/replacement/ makes it much more readable. Also it won't keep sucking characters out of your filename if you accidentally run it twice or something.

这篇关于使用 sed 批量重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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