在 Unix 中根据模式重命名多个文件
一个目录中有多个以前缀 fgh
开头的文件,例如:
fghfilea
fghfileb
fghfilec
我想将它们全部重命名为以前缀 jkl
开头。 是否有一个命令可以做到这一点,而不是单独重命名每个文件?
There are multiple files in a directory that begin with prefix fgh
, for example:
fghfilea
fghfileb
fghfilec
I want to rename all of them to begin with prefix jkl
. Is there a single command to do that instead of renaming each file individually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
有多种方法,但使用
rename
可能是最简单的。使用
rename
(Perl 的重命名
):使用另一个版本的
重命名
(与 Judy2K 的回答):您应该检查您平台的手册页以了解上述哪一项适用。
There are several ways, but using
rename
will probably be the easiest.Using one version of
rename
(Perl'srename
):Using another version of
rename
(same as Judy2K's answer):You should check your platform's man page to see which of the above applies.
这就是
sed
和mv
一起使用来重命名的方式:根据下面的注释,如果文件名中包含空格,则可能需要用引号引起来 返回文件移动到的名称的子函数:
This is how
sed
andmv
can be used together to do rename:As per comment below, if the file names have spaces in them, quotes may need to surround the sub-function that returns the name to move the files to:
重命名可能并不适用于每个系统。 所以如果你没有它,请使用 shell
这个例子在 bash shell 中
rename might not be in every system. so if you don't have it, use the shell
this example in bash shell
使用 mmv:
Using mmv:
有很多方法可以做到这一点(并非所有这些方法都适用于所有 unixy 系统):
ls | 切-c4- | xargs -I§ mv fgh§ jkl§
§ 可以替换为您认为方便的任何内容。 您也可以使用
find -exec
来完成此操作,但在许多系统上其行为略有不同,因此我通常会避免这样做for f in fgh*; do mv "$f" "${f/fgh/jkl}";完成
正如他们所说,粗鲁但有效
rename 's/^fgh/jkl/' fgh*
非常漂亮,但是 rename 在 BSD 上不存在,据我所知,BSD 是最常见的 unix 系统。
重命名 fgh jkl fgh*
ls | perl -ne 'chomp; 接下来除非-e; $o = $_; s/fgh/jkl/; 下一个如果-e; 重命名 $o, $_';
如果你坚持使用Perl,但你的系统上没有重命名,你可以使用这个怪物。
其中一些有点复杂,列表还远未完成,但您会在这里找到几乎所有 UNIX 系统所需的内容。
There are many ways to do it (not all of these will work on all unixy systems):
ls | cut -c4- | xargs -I§ mv fgh§ jkl§
The § may be replaced by anything you find convenient. You could do this with
find -exec
too but that behaves subtly different on many systems, so I usually avoid thatfor f in fgh*; do mv "$f" "${f/fgh/jkl}";done
Crude but effective as they say
rename 's/^fgh/jkl/' fgh*
Real pretty, but rename is not present on BSD, which is the most common unix system afaik.
rename fgh jkl fgh*
ls | perl -ne 'chomp; next unless -e; $o = $_; s/fgh/jkl/; next if -e; rename $o, $_';
If you insist on using Perl, but there is no rename on your system, you can use this monster.
Some of those are a bit convoluted and the list is far from complete, but you will find what you want here for pretty much all unix systems.
使用
find
、xargs
和sed
:它比 @nik 的解决方案,但它允许递归地重命名文件。 例如,结构
将转换为这样,
使其与 xargs 一起使用的关键是 从 xargs 调用 shell。
Using
find
,xargs
andsed
:It's more complex than @nik's solution but it allows to rename files recursively. For instance, the structure,
would be transformed to this,
The key to make it work with
xargs
is to invoke the shell from xargs.通用命令为
find /path/to/files -name '*' -exec bash -c 'mv $0 ${0//}' {} \;
其中
和
应分别替换为您的源和目标。作为针对您的问题量身定制的更具体的示例(应从文件所在的同一文件夹运行),上述命令如下所示:
find 。 -name 'gfh*' -exec bash -c 'mv $0 ${0/gfh/jkl}' {} \;
对于“试运行”,请在
之前添加
,这样您就可以看到生成了哪些命令:echo
mvfind 。 -name 'gfh*' -exec bash -c 'echo mv $0 ${0/gfh/jkl}' {} \;
Generic command would be
find /path/to/files -name '<search>*' -exec bash -c 'mv $0 ${0/<search>/<replace>}' {} \;
where
<search>
and<replace>
should be replaced with your source and target respectively.As a more specific example tailored to your problem (should be run from the same folder where your files are), the above command would look like:
find . -name 'gfh*' -exec bash -c 'mv $0 ${0/gfh/jkl}' {} \;
For a "dry run" add
echo
beforemv
, so that you'd see what commands are generated:find . -name 'gfh*' -exec bash -c 'echo mv $0 ${0/gfh/jkl}' {} \;
要安装 Perl rename 脚本:
有 两个重命名,如 Stephan202 的回答中的评论中所述。
基于 Debian 的发行版具有 Perl 重命名。 Redhat/rpm 发行版具有 C 重命名。
OS X 默认情况下没有安装(至少在 10.8 中),Windows/Cygwin 也没有。
To install the Perl rename script:
There are two renames as mentioned in the comments in Stephan202's answer.
Debian based distros have the Perl rename. Redhat/rpm distros have the C rename.
OS X doesn't have one installed by default (at least in 10.8), neither does Windows/Cygwin.
以下是使用命令行 Groovy 执行此操作的方法:
Here's a way to do it using command-line Groovy:
在 Solaris 上您可以尝试:
On Solaris you can try:
该脚本适用于对可能包含空格的目录/文件名进行递归重命名:
请注意
sed
表达式,在此示例中,它用空格替换所有出现的
;
>。 当然这个要根据具体需要来更换。This script worked for me for recursive renaming with directories/file names possibly containing white-spaces:
Notice the
sed
expression which in this example replaces all occurrences of;
with space. This should of course be replaced according to the specific needs.
使用 StringSolver 工具(windows 和 Linux bash)通过示例进行处理:
它首先计算一个过滤器基于示例,其中输入是文件名和输出(ok 和 notok,任意字符串)。 如果过滤器有选项 --auto 或在此命令后单独调用,它将创建一个文件夹
ok
和一个文件夹notok
并将文件分别推送到它们。然后使用过滤器,
mv
命令是半自动移动,通过修饰符 --auto 变为自动移动。 通过 --filter 使用之前的过滤器,它会找到从fghfilea
到jklfilea
的映射,然后将其应用于所有过滤的文件。其他单行解决方案
执行相同操作的其他等效方法(每行都是等效的),因此您可以选择您最喜欢的执行方式。
多步解决方案
要仔细查找命令是否执行良好,您可以键入以下内容:
当您确信过滤器良好时,请执行第一步:
如果您想测试,并且使用之前的过滤器,输入:
如果转换不是您想要的(例如,即使使用
mv --explain
您也会发现有些问题),您可以输入mv --clear 重新开始移动文件,或添加更多示例
mv input1 input2
其中 input1 和 input2 是其他示例当您有信心时,只需键入
即可瞧瞧! 所有重命名都是使用过滤器完成的。
免责声明:我是这部出于学术目的而创作的作品的合著者。 很快可能还会有一个 bash 生成功能。
Using StringSolver tools (windows & Linux bash) which process by examples:
It first computes a filter based on examples, where the input is the file names and the output (ok and notok, arbitrary strings). If filter had the option --auto or was invoked alone after this command, it would create a folder
ok
and a foldernotok
and push files respectively to them.Then using the filter, the
mv
command is a semi-automatic move which becomes automatic with the modifier --auto. Using the previous filter thanks to --filter, it finds a mapping fromfghfilea
tojklfilea
and then applies it on all filtered files.Other one-line solutions
Other equivalent ways of doing the same (each line is equivalent), so you can choose your favorite way of doing it.
Multi-step solution
To carefully find if the commands are performing well, you can type the following:
and when you are confident that the filter is good, perform the first move:
If you want to test, and use the previous filter, type:
If the transformation is not what you wanted (e.g. even with
mv --explain
you see that something is wrong), you can typemv --clear
to restart moving files, or add more examplesmv input1 input2
where input1 and input2 are other examplesWhen you are confident, just type
and voilà! All the renaming is done using the filter.
DISCLAIMER: I am a co-author of this work made for academic purposes. There might also be a bash-producing feature soon.
在 Ruby 中(在我的 Mac 上)执行此操作要容易得多。 这里有 2 个例子:
It was much easier (on my Mac) to do this in Ruby. Here are 2 examples:
使用 renamer:
删除
--dry-run
标志很高兴输出看起来正确。Using renamer:
Remove the
--dry-run
flag once you're happy the output looks correct.我的重命名大量文件的版本:
My version of renaming mass files:
另一种可能的参数扩展:
Another possible parameter expansion:
我建议使用我自己的脚本来解决这个问题。 它还具有更改文件名编码以及将组合变音符号转换为预组合字符的选项,这是我从 Mac 复制文件时经常遇到的问题。
I would recommend using my own script, which solves this problem. It also has options to change the encoding of the file names, and to convert combining diacriticals to precomposed characters, a problem I always have when I copy files from my Mac.
这对我使用正则表达式有用:
我希望像这样重命名文件:
使用 [az|_]+0*([0-9]+.) 正则表达式,其中 ([0-9]+.) 是在重命名命令上使用的组子字符串
Produces:
另一个示例:
Produces:
警告,小心。
This worked for me using regexp:
I wanted files to be renamed like this:
usig the [a-z|_]+0*([0-9]+.) regexp where ([0-9]+.) is a group substring to use on the rename command
Produces:
Another example:
Produces:
Warning, be careful.
我编写了此脚本来搜索所有 .mkv 文件,并将找到的文件递归重命名为 .avi。 您可以根据您的需要对其进行自定义。 我添加了一些其他内容,例如从文件路径获取文件目录、扩展名、文件名,以防您将来需要引用某些内容。
I wrote this script to search for all .mkv files recursively renaming found files to .avi. You can customize it to your neeeds. I've added some other things such as getting file directory, extension, file name from a file path just incase you need to refer to something in the future.
用于在文件列表上运行
sed
表达式的通用脚本(结合了sed
解决方案 与rename
解决方案):通过向脚本传递 < code>sed 表达式,然后是任何文件列表,就像
rename
< 的版本一样/a>:A generic script to run a
sed
expression on a list of files (combines thesed
solution with therename
solution):Invoke by passing the script a
sed
expression, and then any list of files, just like a version ofrename
:您还可以使用下面的脚本。 在终端上运行非常容易...
//一次重命名多个文件
示例:-
You can also use below script. it is very easy to run on terminal...
//Rename multiple files at a time
Example:-
这是
find
+sed
+xargs
解决方案的扩展版本。原始解决方案:这个和这个。
要求:搜索、修剪、正则表达式、重命名
perl rename
工作,这是 所必需的最流行的解决方案(我认为它很慢,因为它似乎没有修剪选项?)解决方案
find
有效地获取文件(通过修剪),并使用许多定制选项。示例 1:重命名
*.js
文件但忽略node_modules
此示例查找文件并回显找到的文件和重命名的文件。 出于安全考虑,目前它不会移动任何东西。 为此,您必须将
echo
替换为mv
。上面的脚本将这样:
变成这样:
解决方案分解
find "." -type f -not \( -path "**/node_modules/**" -prune \) -name "*.js"
-type f
)。-not
部分排除(而且重要的是,它不会遍历!)(众所周知的巨大)node_modules
文件夹。“*.js”
匹配。sed -nE "s/(.*)\/my \-(.*\.js)/&\1\/你的-\2/p"
sed
总是需要一些时间来适应。-E
启用“扩展”(即更现代的)正则表达式语法。-n
与尾随/p
标志结合使用:-n
隐藏所有结果,而/p< /code> 将仅打印匹配的结果。 这样,我们只查看/移动需要更改的文件,而忽略所有其他文件。
sed
(和其他正则表达式工具)替换正则表达式始终采用以下格式:s/regex/replacement/FLAGS
&
表示匹配的输入字符串。 这将是mv
的第一个参数。xargs -n 2 echo
echo
。祝你好运!
This is an extended version of the
find
+sed
+xargs
solution.Original solutions: this and this.
Requirements: search, prune, regex, rename
cygwin
and cannot getperl rename
to work, which is required for the most popular solution (and I assume it to be slow, since it does not seem to have a pruning option?)Solution
find
to get files effectively (with pruning), and with many customization options.sed
for regex replacement.xargs
to funnel the result into the final command.Example 1: rename
*.js
files but ignorenode_modules
This example finds files and echos the found file and the renamed file. For safety reasons, it does not move anything for now. You have to replace
echo
withmv
for that.The above script turns this:
Into this:
Breakdown of Solution
find "." -type f -not \( -path "**/node_modules/**" -prune \) -name "*.js"
-type f
).-not
part excludes (and, importantly does not traverse!) the (notoriously ginormous)node_modules
folder."*.js"
.sed -nE "s/(.*)\/my\-(.*\.js)/& \1\/YOUR-\2/p"
sed
always takes some getting used to.-E
enables "extended" (i.e. more modern) regex syntax.-n
is used in combination with the trailing/p
flag:-n
hides all results, while/p
will print only matching results. This way, we only see/move files that need changing, and ignore all others.sed
(and other regex tools) is always of the format:s/regex/replacement/FLAGS
&
represents the matched input string. This will be the first argument tomv
.xargs -n 2 echo
echo
with (the first two strings of) the replaced string.Good luck!
这个答案只是其他答案中解释的内容的具体应用,我希望它有用:
我遇到过一个情况,我必须重命名一些格式为
CodeAPE_fr.properties
的属性文件到CodeAPE.properties
,在整个目录中,我做了:This answer is only a concrete application of what is explained in other answers, I hope it will be useful:
I went in a case where I had to rename some properties files that were in the form
CodeAPE_fr.properties
toCodeAPE.properties
, in a whole directory and I did:如果文件名包含空格并且您想要根据文件名中包含空格模式的部分进行过滤,则需要进行一些轻微的改进/调整:
我的答案中的主要区别是您需要在引号中添加引号“带空格的文件名”(如果这是您要用作过滤器来选择要操作的文件的文件名部分)。 前面的答案提到了带空格的文件名,但随后基于 jkl* 选择了不带空格的文件。
注意:您不必替换为 FilenameWithNoMoreSpaces。 我只是想展示一个典型的用例作为例子。
如果带有空格的模式位于文件名的中间,则调整后的命令将遵循以下模式:
注意,我在文件名模式的开头添加了另一个星号,其中双引号中包含空格,并且删除了克拉 ^来自 sed 命令的“查找”部分。 如果我解释过度,我很抱歉。 我考虑到有些阅读本文的人可能对编程完全陌生。
Here is a slight improvement/adjustment that you need if the filenames have whitespaces AND you want to filter based on the portion of the filename that has the pattern with the whitespaces:
The key difference in my answer is that you need to put quotation marks around the "Filename With Spaces" if that is the portion of the filename that you want to use as the filter to choose which files to operate on. The previous answer(s) mentioned filename with spaces but then selected files based on jkl* which does not have spaces.
Note: You do not have to replace with FilenameWithNoMoreSpaces. I just wanted to show a typical use case as an example.
If the pattern with the whitespace is in the MIDDLE of the filename, then the adjusted command would follow this pattern:
Notice, I added another asterisk to the beginning of the filename pattern with spaces that is in the double quotes and I removed the carat ^ from the "find" part of the sed command. My apologies if I'm over-explaining. I'm taking into account that some people who read this might be completely new to programming.
使用 nautilus:
With nautilus: