[英]How to rename all files in folders with pattern
I have a bunch of files like: 我有一堆文件,例如:
bla.super.lol.S01E03.omg.bbq.mp4
bla.super.lol.S01E04.omg.bbq.mp4
bla.super.lol.s03e12.omg.bbq.mp4
I need to rename them all like: 我需要将它们重命名为:
s01e03.mp4
s01e04.mp4
s03e12.mp4
I've tried to do it with for file in *; do mv $file ${file%%\\.omg*}; done
我已经尝试用
for file in *; do mv $file ${file%%\\.omg*}; done
来做到这一点for file in *; do mv $file ${file%%\\.omg*}; done
for file in *; do mv $file ${file%%\\.omg*}; done
for file in *; do mv $file ${file%%\\.omg*}; done
but it removes only part after S01E01, not before it so please, help for file in *; do mv $file ${file%%\\.omg*}; done
但仅删除S01E01之后的部分,而不是之前,因此请帮助
许多Linux都提供了一个很好的命令行工具,称为“ rename
,它使用了Perl正则表达式:
rename 's/.*\.(\w+\d)\..*/$1.mp4/;y/A-Z/a-z/' *.mp4
Simple Perl script that tries to parse out the episode information, skips files where it can't find them. 尝试解析情节信息的简单Perl脚本,跳过无法找到情节信息的文件。
#!perl
use strict;
use warnings;
use File::Copy qw(move);
foreach my $file ( glob('*.mp4') ) {
my ($info) = $file =~ m/([sS]\d+[eE]\d+)/;
next unless $info;
my $new_filename = lc $info . ".mp4";
move $file, $new_filename
or die "$!";
}
A pure Bash solution : 一个纯Bash解决方案 :
for f in *.mp4; do
IFS=. read -r _ _ _ s _ <<< "$f"
mv -- "$f" "${s,,}.mp4"
done
To test it, I created the following tree: 为了测试它,我创建了以下树:
tree
.
├── bla.super.lol.S01E03.omg.bbq.mp4
├── bla.super.lol.S01E04.omg.bbq.mp4
└── bla.super.lol.s03e12.omg.bbq.mp4
0 directories, 3 files
Let's put a printf
before mv
to be sure about the changes: 让我们将
printf
放在mv
之前,以确保更改:
mv -- bla.super.lol.S01E03.omg.bbq.mp4 s01e03.mp4
mv -- bla.super.lol.S01E04.omg.bbq.mp4 s01e04.mp4
mv -- bla.super.lol.s03e12.omg.bbq.mp4 s03e12.mp4
Looks good. 看起来不错。 The season part is extracted, and lower-cased as requested.
提取季节部分,并根据要求将其小写。
If your filenames are always dot-separated and the SExxEmm part is always the 4th one, then I'd do it with awk: 如果您的文件名始终以点分隔,并且SExxEmm部分始终为第4个,那么我将使用awk进行操作:
for file in *; do
mv $file $(echo $file | awk -F. '{print $4 "." $NF}');
done
awk -F.
splits the filename at the dots apart and then prints the 4th and last field. 将文件名分开,然后打印第四个和最后一个字段。
To do a "dry run" (ie show the commands instead of executing them), prepend them with echo
: 要进行“空运行”(即显示命令而不是执行命令),请在命令前添加
echo
:
for file in *; do
echo mv $file $(echo $file | awk -F. '{print $4 "." $NF}');
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.