Linux 用于批量重命名文件夹中文件的 Shell/Bash 快捷方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8416990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:32:10  来源:igfitidea点击:

Shell/Bash shortcut for bulk renaming of files in a folder

linuxbashfile-ioscripting

提问by sc_ray

Is there a shortcut in Shell/Bash that can rename all the files in a folder based on a regex or some other criteria. What I am looking for here is in my folder documents, that has let's say a 100 text files with the following naming convention:

Shell/Bash 中是否有快捷方式可以根据正则表达式或其他一些条件重命名文件夹中的所有文件。我在这里寻找的是在我的文件夹文档中,假设有 100 个具有以下命名约定的文本文件:

<longdocumentidentifier>-doc-<counter>.txt.

I need to rename all the files with the above given convention to just:

我需要使用上述给定的约定将所有文件重命名为:

doc-<counter>.txt

Is there a one-liner that can help me with the above?

有没有可以帮助我解决上述问题的单线?

采纳答案by Sorin

I would suggest something like this:

我会建议这样的事情:

for i in *-doc-*.txt; do mv "$i" "${i/*-doc-/doc-}"; done

${i/*-doc-/doc-}replaces the first occurrence of *-doc-with doc-.

${i/*-doc-/doc-}替换第一次出现*-doc-doc-

If you need to do more than one replacement (see comment number 1), you need to use the ${var//Pattern/Replacement}variant. If you need to replace the beginning of the name you need to use ${var/#Pattern/Replacement}, if you need to replace the end (ie: the extension) you need to use the ${var/%Pattern/Replacement}form.

如果您需要进行多次替换(请参阅注释编号 1),则需要使用${var//Pattern/Replacement}变体。如果需要替换名称的开头需要使用${var/#Pattern/Replacement},如果需要替换结尾(即:扩展名)则需要使用${var/%Pattern/Replacement}表单。

See Shell Parameter Expansionfor more details. This expansion is bash specific.

有关更多详细信息,请参阅外壳参数扩展。此扩展是特定于 bash 的。

回答by Wes Hardaker

The rename command built in to most linux, eg, will do this easily.

例如,大多数 linux 内置的 rename 命令将很容易做到这一点。

Personally, I prefer regexps too which is why I've been carrying around this script for a very very very long time (read: since the late 80s or early 90s):

就我个人而言,我也更喜欢正则表达式,这就是为什么我一直在使用这个脚本很长一段时间(阅读:自 80 年代末或 90 年代初以来):

#!/usr/bin/perl

($op = shift) || die "Usage: 
script-name 's/.*-doc(.*).txt/doc.txt/' *.txt
expr [files]]\n"; if(!@ARGV) { @ARGV = <STDIN>; chop(@ARGV); } for (@ARGV) { $was = $_; eval $op; die $@ if $@; if ($was ne $_) { print "rename($was,$_)\n"; rename($was,$_); } }

Which, when installed lets you do things like this:

安装后,您可以执行以下操作:

prename 's/^.*-doc-(.*\.txt)$/doc-/'  *.txt

回答by Elias Dorneles

There is prename, that allows you to use REGEX:

prename, 允许您使用正则表达式:

prename -n 's/^.*-doc-(.*\.txt)$/doc-/'  *.txt

Use the option -nto simulate:

使用选项-n来模拟:

mmv "*-doc-*" "doc-#2"

Note:This is the shipped as renamein many Linux distributions, but not in all of them -- so I'm using the canonical name for the utility that comes with Perl.

注意:这是rename在许多 Linux 发行版中提供的,但不是在所有发行版中都提供——所以我使用 Perl 附带的实用程序的规范名称。

回答by telenachos

If you have renamethen, rename 's/^.*-doc-/doc-/' *.txtshould do the trick.

如果你有rename那么,rename 's/^.*-doc-/doc-/' *.txt应该做的伎俩。

回答by Ahmed

find . -maxdepth N -type f -name "$pattern" | sed -e 'p' -E -e "s/$str1/$str2/g" | xargs -n2 mv

mmv command stands for "mass move"

mmv 命令代表“大规模移动”

回答by Ron Wertlen

If you want to recurse into sub-directories, there is also:

如果你想递归到子目录,还有:

rnm -rs '/.*-doc-/doc-/' *.txt

On system that automatically support extended Regexps, you can leave away the -E.

在自动支持扩展正则表达式的系统上,您可以将-E.

Advantages:

好处:

  • recurses into sub-directories
  • you can control the maxdepth of the recursion
  • you can rename files and/or directories (-type f|d)
  • 递归到子目录
  • 您可以控制递归的最大深度
  • 您可以重命名文件和/或目录(-type f|d)

Disadvantages:

缺点:

  • slightly more complicated regexps, because you have to strip out the path to get at the file name
  • 稍微复杂一点的正则表达式,因为你必须去掉路径才能得到文件名

(answer amended from here)

(答案从这里修改)

回答by Jahid

If you don't mind external tool, then here's one: rnm(web page)

如果您不介意外部工具,那么这里有一个:rnm( web page)

For your particular problem the command would be:

对于您的特定问题,命令将是:

rnm -rs '/.*-(doc-.*\.txt)//' *.txt

Or

或者

##代码##

You can find more examples/docs here.

您可以在此处找到更多示例/文档。

回答by John M Naglick

find . -name '*scss' | xargs -L1 -I {} echo {} {} | sed 's/css.scss$/scss/' | xargs -L1 mv

找 。-name '*scss' | xargs -L1 -I {} echo {} {} | sed 's/css.scss$/scss/' | xargs -L1 mv

for example if you have a bunch of files ending with ".css.scss" and you want to rename them to end with simply ".scss" (ie remove the .css part)

例如,如果您有一堆以“.css.scss”结尾的文件,并且您想将它们重命名为仅以“.scss”结尾(即删除 .css 部分)

tweak the regexp and find arguments to your needs

调整正则表达式并找到满足您需求的参数