2011-06-11 21 views
13

我正在尝试使用mv命令做三件事,但不确定它有可能吗?可能需要一个脚本。不知道如何写它。所有文件都在同一个文件夹中。根据模式在Linux中重命名大量文件

1)v9.zip结束应该只是的.zip(在V9移除)含有

2)文件的文件_应该是 -

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

+0

我不确定你会使用mv来做到这一点...也许看看脚本解决方案,给出条件的情况。 – quakkels 2011-06-11 15:13:48

+2

你的最后一点看起来不够明确。确定'ruNaway'是'ruN away'还是'ru Naway'的规则是什么? – 2011-06-11 15:24:23

+0

1和2应该使用'rename'变得微不足道。 3更多参与,因此需要一些脚本。你使用哪个shell,bash? – 2011-06-11 15:25:54

回答

8

我最喜欢的解决方案是my own rename script 。映射到你的问题,最简单的例子是这些:

% 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///模式,虽然通常会这样做。

other tools in that directory大多是对Unicode的工作,其中有一些超有用的。

+1

呃哇,我不知道我被授权拥有这种极端的权力。非常感谢你! – 2011-06-11 16:04:25

+2

training.perl.com/scripts/rename的链接已损坏。 – 2014-01-04 19:49:37

9

我没有测试过这些,所以我把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 

至于你的第三个问题,我敢肯定,它也可以做到,但也许更复杂的方法比原壳单行将帮助如@tchrist所述。

+1

+1,因为第一行帮助我解决了这个问题的简单版本。谢谢。 – TecBrat 2013-09-08 21:28:15

+1

这正是我所需要的。它让我能够理清命令,直到完美为止,然后删除回声和'火'! – ABrowne 2016-04-26 07:27:16

3

上述答案适用于Debian的,Ubuntu的等

对于RHEL和合作:重命名from_pattern to_pattern文件

20

有提供与大多数基于Debian/Ubuntu的发行版,一个rename命令,它是基于写入由罗宾·巴克拉里沃尔从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的脚本。

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

在这些发行版之一,你可以安装它:

$ sudo apt-get install renameutils 

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

$ qmv 

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

2

我认为这个链接已经坏了,我找不到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/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 [email protected] if [email protected]; 
    rename($was,$_) unless $was eq $_; 
}