在Mac上批量重命名文件正则表达式 [英] Batch rename files regular expression on Mac
问题描述
我正在尝试批量重命名PDF文件,以从右到左删除所有字符,直到在Mac OS上达到数字字符为止.
I'm trying to batch rename PDF files to remove all characters from right to left until a numeric character is reached on Mac OS.
例如:
10002ASmith.pdf> 10002.pdf
10002ASmith.pdf > 10002.pdf
1023AJones.pdf> 1023.pdf
1023AJones.pdf > 1023.pdf
文件开头的数字长度是可变的,因此我认为最好将regex从右到左重命名,直到达到数字为止.
The numeric length in the beginning of the files is variable so I figure would be best to have regex rename from right to left until a number is reached.
我知道Mac Yosemite具有批量重命名功能,但是没有看到正则表达式重命名选项.在终端中执行此操作是否容易?如果可以,如何操作?或者我可以使用免费的程序来使用正则表达式吗?
I know Mac Yosemite comes with batch rename capabilities but didn't see a regex rename option. Is it easy to do this in terminal and if so how, or is there a free program I can use to use a regular expression?
我想我只需要知道实际的正则表达式是什么,因为我对regex的经验还很远.
I suppose I just need to know what the actual regular expression would be since I'm far from experienced in regex.
谢谢您的帮助!
乔恩
推荐答案
您可以将这个 find
与 sed
一起使用:
You can use this find
with sed
:
cd to /path/of/pdfs
while IFS= read -rd '' line; do
echo mv "$line" $(sed 's~\./[a-zA-Z]*\([0-9]*[a-zA-Z]\).*\(\.pdf\)$~\1\2~' <<< "$line")
done < <(find . -maxdepth 1 -regex '\./[a-zA-Z]*[0-9]*[a-zA-Z]\{1,\}\.pdf$' -print0)
确认其正常运行后,
删除 echo
.
Remove echo
after you verify it is working.
这篇关于在Mac上批量重命名文件正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!