[英]Shell/Bash shortcut for bulk renaming of files in a folder
Is there a shortcut in Shell/Bash that can rename all the files in a folder based on a regex or some other criteria. Shell/Bash 中是否有快捷方式可以根据正则表达式或其他一些条件重命名文件夹中的所有文件。 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:
我在这里寻找的是在我的文件夹文档中,假设有 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?有没有可以帮助我解决上述问题的单线?
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.如果您需要进行多次替换(请参阅注释编号 1),则需要使用
${var//Pattern/Replacement}
变体。 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.如果您需要替换名称的开头,您需要使用
${var/#Pattern/Replacement}
,如果您需要替换结尾(即:扩展名),您需要使用${var/%Pattern/Replacement}
表格。
See Shell Parameter Expansion for more details.有关更多详细信息,请参阅外壳参数扩展。 This expansion is bash specific.
此扩展是特定于 bash 的。
如果您rename
则rename 's/^.*-doc-/doc-/' *.txt
应该可以解决问题。
There is prename
, that allows you to use REGEX:有
prename
,它允许您使用正则表达式:
prename 's/^.*-doc-(.*\.txt)$/doc-$1/' *.txt
Use the option -n
to simulate:使用选项
-n
来模拟:
prename -n 's/^.*-doc-(.*\.txt)$/doc-$1/' *.txt
Note: This is the shipped as rename
in many Linux distributions, but not in all of them -- so I'm using the canonical name for the utility that comes with Perl.注意:这是在许多 Linux 发行版中作为
rename
提供的,但并非在所有发行版中都如此——所以我使用 Perl 附带的实用程序的规范名称。
If you want to recurse into sub-directories, there is also:如果你想递归到子目录,还有:
find . -maxdepth N -type f -name "$pattern" | sed -e 'p' -E -e "s/$str1/$str2/g" | xargs -n2 mv
On system that automatically support extended Regexps, you can leave away the -E
.在自动支持扩展正则表达式的系统上,您可以省略
-E
。
Advantages:好处:
Disadvantages:缺点:
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: $0 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:安装后,您可以执行以下操作:
script-name 's/.*-doc(.*).txt/doc$1.txt/' *.txt
mmv "*-doc-*" "doc-#2"
mmv 命令代表“大规模移动”
find .
找 。 -name '*scss' |
-name '*scss' | xargs -L1 -I {} echo {} {} |
xargs -L1 -I {} echo {} {} | sed 's/css.scss$/scss/' |
sed 's/css.scss$/scss/' | xargs -L1 mv
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调整正则表达式并找到满足您需求的参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.