在Linux中根据[重命名]模式重命名许多文件

||                                                                                                                   关闭。这个问题是题外话。它当前不接受答案。                                                      
已邀请:
        我最喜欢的解决方案是我自己的重命名脚本。映射到您的问题的最简单示例是:
% rename \'s/_/-/g\' *
% rename \'s/(\\p{Lower})(\\p{Upper})/$1 $2/g\' *
尽管我确实讨厌文件名中的空格,尤其是垂直空格:
 % rename \'s/\\s//g\' *
 % rename \'s/\\v//g\' *
等等。它基于The 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工作,其中有些超级有用。     
        大多数基于Debian / Ubuntu的发行版提供了一个
rename
命令,该命令由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/A-Z/a-z/\' *
它使用perl,因此您可以使用perl表达式来匹配模式,实际上,我相信它的工作方式与tchrist的脚本非常相似。 用于批量文件重命名的另一套真正有用的工具是Oskar Liljeblad的namenameutils集合。源代码由自由软件基金会托管。另外,许多发行版(尤其是基于Debian / Ubuntu的发行版)都带有这些工具的
renameutils
软件包。 在这些发行版之一中,您可以使用以下软件进行安装:
$ sudo apt-get install renameutils
然后要重命名文件,只需运行以下命令:
$ qmv
它将弹出一个带有文件列表的文本编辑器,您可以使用编辑器的搜索和替换功能来操作它们。     
        我还没有测试它们,所以我在命令的前面放了“ 9”,这样您就可以在删除回显之前先尝试一下,然后再运行它们。 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
至于您的第三个问题,我敢肯定它也可以解决,但也许像@tchrist所提到的那样,比原始外壳单线更复杂的方法会有所帮助。     
        以上答案适用于Debian,Ubuntu等 对于RHEL和co:将from_pattern重命名为_pattern文件     
        我认为链接已断开,我无法在Webarchive中找到tchrist帖子中重命名脚本的页面,因此这是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/A-Z/a-z/ 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 $_;
}
    

要回复问题请先登录注册