在Linux中根据模式重命名大量文件

我试图用mv命令做三件事,但不知道这是可能的吗? 可能需要一个脚本。 不知道该怎么写。 所有文件都在同一个文件夹中。

1)以v9.zip结尾的文件应该是.zip(v9被删除)

2)包含_的文件应该是 –

3)小写字母旁边的大写字母(或大写字母旁边的小写字母)的文件之间应该有一个空格。 所以MoveOverNow将被移动到现在和ruNaway将结束[az] [az]或[az] [AZ]成为[AZ] [az]和[az] [AZ]

Solutions Collecting From Web of "在Linux中根据模式重命名大量文件"

我最喜欢的解决方案是自己的重命名脚本 映射到您的问题最简单的例子是这些:

 % rename 's/_/-/g' * % rename 's/(\p{Lower})(\p{Upper})/$1 $2/g' * 

虽然我真的讨厌我的文件名空白,特别是垂直空白:

  % rename 's/\s//g' * % rename 's/\v//g' * 

等等。 它基于Larry Wall的脚本,但是扩展了选项,如下所示:

 usage: /home/tchrist/scripts/rename [-ifqI0vnml] [-F file] perlexpr [files] -i ask about clobbering existent files -f force clobbers without inquiring -q quietly skip clobbers without inquiring -I ask about all changes -0 read null-terminated filenames -v verbosely says what its doing -V verbosely says what its doing but with newlines between old and new filenames -n don't really do it -m to always rename -l to always symlink -F path read filelist to change from magic path(s) 

正如你所看到的,它不仅可以改变文件的名称,而且可以改变符号链接使用相同的模式。 你不必使用s///模式,虽然通常是这样。

该目录中的其他工具主要用于Unicode工作,其中有一些超级有用的工具。

有一个rename命令提供了大多数基于Debian / Ubuntu的发行版,它是由Robin Barker根据Larry Wall从1998年左右的原始代码(!)编写的。

以下是文档摘录:

  "rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input. For example, to rename all files matching "*.bak" to strip the extension, you might say rename 's/\.bak$//' *.bak To translate uppercase names to lower, you'd use rename 'y/AZ/az/' * 

它使用perl,所以你可以使用perl表达式来匹配模式,事实上我相信它很像tchrist的脚本。

另外一个非常有用的批量文件重命名工具是Oskar Liljeblad的renameutils集合 。 源代码由自由软件基金会托管。 另外,许多发行版(特别是基于Debian / Ubuntu的发行版)都有一个带有这些工具的renameutils软件包。

在其中一个发行版中,您可以安装它:

 $ sudo apt-get install renameutils 

然后重命名文件只需运行这个命令:

 $ qmv 

它会弹出一个文件列表的文本编辑器,你可以用你的编辑器的搜索和替换功能来操作它们。

我没有测试这些,所以我把echo放在命令的前面,所以你可以尝试它们,然后删除回声来运行它们。

1)

 for f in *v9.zip; do echo mv "${f}" "${f%v9.zip}.zip"; done 

2)

 for f in *_*; do echo mv "${f}" "${f//_/-}"; done 

至于你的第三个问题,我相信也可以这样做,但也许是一个更复杂的方法比原始的shell单行将帮助,如@ tchrist提到的。

以上答案适用于Debian,Ubuntu等

对于RHEL和co:将from_pattern重命名为_pattern文件

我认为这个链接被破坏了,我找不到tchrist文章中的重命名脚本的webarchive页面,所以这里是Perl中的另一个。

 #!/usr/bin/perl # -w switch is off bc HERE docs cause erroneous messages to be displayed under # Cygwin #From the Perl Cookbook, Ch. 9.9 # rename - Larry's filename fixer $help = <<EOF; Usage: rename expr [files] This script's first argument is Perl code that alters the filename (stored in \$_ ) to reflect how you want the file renamed. It can do this because it uses an eval to do the hard work. It also skips rename calls when the filename is untouched. This lets you simply use wildcards like rename EXPR * instead of making long lists of filenames. Here are five examples of calling the rename program from your shell: % rename 's/\.orig$//' *.orig % rename 'tr/AZ/az/ unless /^Make/' * % rename '$_ .= ".bad"' *.f % rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i' * % find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/' The first shell command removes a trailing ".orig" from each filename. The second converts uppercase to lowercase. Because a translation is used rather than the lc function, this conversion won't be locale- aware. To fix that, you'd have to write: % rename 'use locale; $_ = lc($_) unless /^Make/' * The third appends ".bad" to each Fortran file ending in ".f", something a lot of us have wanted to do for a long time. The fourth prompts the user for the change. Each file's name is printed to standard output and a response is read from standard input. If the user types something starting with a "y" or "Y", any "foo" in the filename is changed to "bar". The fifth uses find to locate files in /tmp that end with a tilde. It renames these so that instead of ending with a tilde, they start with a dot and a pound sign. In effect, this switches between two common conventions for backup files EOF $op = shift or die $help; chomp(@ARGV = <STDIN>) unless @ARGV; for (@ARGV) { $was = $_; eval $op; die $@ if $@; rename($was,$_) unless $was eq $_; }