Merge pull request #2150 from geekpi/master

translated
This commit is contained in:
geekpi 2014-12-26 21:29:26 +08:00
commit 88933273fa
2 changed files with 83 additions and 84 deletions

View File

@ -1,84 +0,0 @@
Translating-----geekpi
Linux FAQs with Answers--How to rename multiple files on Linux
================================================================================
> **Question**: I know I can rename a file using mv command. But what if I want to change the name of many files? It will be tedius to invoke mv command for every such file. Is there a more convenient way to rename multiple files at once?
In Linux, when you want to change a file name, mv command gets the job done. However, mv cannot rename multiple files using wildcard. There are ways to deal with multiple files by using a combination of sed, awk or find in conjunction with [xargs][1]. However, these CLIs are rather cumbersome and not user-friendly, and can be error-prone if you are not careful. You don't want to undo incorrect name change for 1,000 files.
When it comes to renaming multiple files, the rename utility is probably the easiest, the safest, and the most powerful command-line tool. The rename command is actually a Perl script, and comes pre-installed on all modern Linux distributions.
Here is the basic syntax of rename command.
rename [-v -n -f] <pcre> <files>
<pcre> is a Perl-compatible regular expression (PCRE) which represents file(s) to rename and how. This regular expression is in the form of 's/old-name/new-name/'.
The '-v' option shows the details of file name changes (e.g., XXX renamed as YYY).
The '-n' option tells rename to show how the files would be renamed without actually changing the names. This option is useful when you want to simulate filename change without touching files.
The '-f' option force overwriting existing files.
In the following, let's see several rename command examples.
### Change File Extensions ###
Suppose you have many image files with .jpeg extension. You want to change their file names to *.jpg. The following command converts *.jpeg files to *.jpg.
$ rename 's/\.jpeg$/\.jpg/' *.jpeg
### Convert Uppercase to Lowercase and Vice-Versa ###
In case you want to change text case in filenames, you can use the following commands.
To rename all files to lower-case:
# rename 'y/A-Z/a-z/' *
To rename all files to upper-case:
# rename 'y/a-z/A-Z/' *
![](https://farm9.staticflickr.com/8655/16054304245_bcf9d23b59_b.jpg)
### Change File Name Patterns ###
Now let's consider more complex regular expressions which involve subpatterns. In PCRE, a subpattern captured within round brackets can be referenced by a number preceded by a dollar sign (e.g., $1, $2).
For example, the following command will rename 'img_NNNN.jpeg' to 'dan_NNNN.jpg'.
# rename -v 's/img_(\d{4})\.jpeg$/dan_$1\.jpg/' *.jpeg
----------
img_5417.jpeg renamed as dan_5417.jpg
img_5418.jpeg renamed as dan_5418.jpg
img_5419.jpeg renamed as dan_5419.jpg
img_5420.jpeg renamed as dan_5420.jpg
img_5421.jpeg renamed as dan_5421.jpg
The next command will rename 'img_000NNNN.jpeg' to 'dan_NNNN.jpg'.
# rename -v 's/img_\d{3}(\d{4})\.jpeg$/dan_$1\.jpg/' *jpeg
----------
img_0005417.jpeg renamed as dan_5417.jpg
img_0005418.jpeg renamed as dan_5418.jpg
img_0005419.jpeg renamed as dan_5419.jpg
img_0005420.jpeg renamed as dan_5420.jpg
img_0005421.jpeg renamed as dan_5421.jpg
In both cases above, the subpattern '\d{4}' captures four consecutive digits. The captured four digits are then referred to as $1, and used as part of new filenames.
--------------------------------------------------------------------------------
via: http://ask.xmodulo.com/rename-multiple-files-linux.html
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://xmodulo.com/xargs-command-linux.html

View File

@ -0,0 +1,83 @@
Linux有问必答-- 如何在Linux重命名多个文件
================================================================================
> **提问**我知道我可以用mv命令重命名文件。但是当我想重命名很多文件怎么办如果为每个文件都这么做将会是很乏味的。有没有办法一次性重命名多个文件
在Linux中当你想要改变一个文件名使用mv命令就好了。然而mv不能使用通配符重命名多个文件。可以用sed、awk或者与[xargs][1]结合使用来处理多个文件的情况。然而这些命令行即繁琐u又不友好并且如果不小心的话还很容易出错。你不会想要撤销1000个文件的错误名的。
When it comes to renaming multiple files, the rename utility is probably the easiest, the safest, and the most powerful command-line tool. The rename command is actually a Perl script, and comes pre-installed on all modern Linux distributions.
当你想要重命名多个文件的时候重命名的工具或许是最简单、最安全和最强大的命令行工具。重命名命令实际上是一个Perl脚本它预安装在所有的现在Linux发行班上
下面是重命名命令的基本语法。
rename [-v -n -f] <pcre> <files>
<pcre> 是Perl兼容正则表达式它表示的是要重命名的文件和该怎么做。正则表达式的形式是s/old-name/new-name/’。
-v选项会显示文件名改变的细节比如XXX重命名成YYY
-n选项告诉rename会在不实际改变名称的情况下显示文件将会重命名的情况。这个选项在你想要在不改变文件名的情况下模拟改变文件名的情况下很有用。
-f选项强制覆盖存在的文件。
下面就让我们看下rename命令的几个实际例子。
### 改变文件扩展名 ###
假设你有许多.jpeg的图片文件。你想要把它们的名字改成*.jpg。下面的命令就会将*.jpeg 文件改成 *.jpg。
$ rename 's/\.jpeg$/\.jpg/' *.jpeg
### 大写改成小写,反之亦然 ###
有时你想要改变文件名的大小写,你可以使用下面的命令。
把所有的文件改成小写:
# rename 'y/A-Z/a-z/' *
把所有的文件改成大写:
# rename 'y/a-z/A-Z/' *
![](https://farm9.staticflickr.com/8655/16054304245_bcf9d23b59_b.jpg)
### 更改文件名模式 ###
现在让我们考虑包含子模式的更复杂的正则表达式。在PCRE中子模式包含在圆括号中$符后接上数字(比如$1$2
比如下面的命令会将img_NNNN.jpeg变成dan_NNNN.jpg
# rename -v 's/img_(\d{4})\.jpeg$/dan_$1\.jpg/' *.jpeg
----------
img_5417.jpeg renamed as dan_5417.jpg
img_5418.jpeg renamed as dan_5418.jpg
img_5419.jpeg renamed as dan_5419.jpg
img_5420.jpeg renamed as dan_5420.jpg
img_5421.jpeg renamed as dan_5421.jpg
比如下面的命令会将img_000NNNN.jpeg变成dan_NNNN.jpg
# rename -v 's/img_\d{3}(\d{4})\.jpeg$/dan_$1\.jpg/' *jpeg
----------
img_0005417.jpeg renamed as dan_5417.jpg
img_0005418.jpeg renamed as dan_5418.jpg
img_0005419.jpeg renamed as dan_5419.jpg
img_0005420.jpeg renamed as dan_5420.jpg
img_0005421.jpeg renamed as dan_5421.jpg
上面的例子中,子模式‘\d{4}会捕捉4个连续的数字捕捉的四个数字就是$1, 将会用于新的文件名。
--------------------------------------------------------------------------------
via: http://ask.xmodulo.com/rename-multiple-files-linux.html
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://xmodulo.com/xargs-command-linux.html