如果名称与特定模式匹配,则需要批次重命名文件列表,模式以1或更多位数开始,后面跟着下划线,然后是字母数字。例如:"123_ABC123.txt“(扩展可以是任何东西,而不必是'txt')。
我想大霸王会是这样的:
\d+_*.*但我不知道如何在Unix中实现这一点,具体来说,如何表示第一部分(\d+)应该与第二部分(*)之间的下划线进行交换?
谢谢!
发布于 2022-05-26 09:48:00
您可以使用此rename command
rename -n 's/^(\d+)_(.+)(\.[^.]+)$/$2_$1$3/' [0-9]*.*
123_ABC123.txt' would be renamed to 'ABC123_123.txt'一旦满意,您就可以删除-n (试运行)选项。
解释:
在捕获组的开头,
^(\d+):匹配1+数字,#1_:匹配捕获组中任意字符的_(.+):匹配1+,匹配点和扩展,在捕获组2和1之间插入_之前在第3组中捕获,并在$3中保留扩展
发布于 2022-05-26 19:12:27
使用您所显示的示例,请尝试使用纯BASH的解决方案。
这将打印mv(重命名)命令,一旦您满意,您就可以使用实际的代码重命名文件。
for file in [0-9]*.*
do
firstPart=${file%%_*}
secondPart1=${file%.*}
secondPart=${secondPart1#*_}
extension=${file##*.}
echo "File $file will be renamed to: ${secondPart}_${firstPart}.${extension}"
echo "mv \"$file\" " "\"${secondPart}_${firstPart}.${extension}\""
done显示的文件名为123_ABC123.txt的输出如下:
File 123_ABC123.txt will be renamed to: ABC123_123.txt
mv "123_ABC123.txt" "ABC123_123.txt"注意:一旦您对上述代码的结果满意,然后运行以下代码来重命名实际的文件:
for file in [0-9]*.*
do
firstPart=${val%%_*}
secondPart1=${val%.*}
secondPart=${secondPart1#*_}
extension=${val##*.}
echo "File $file will be renamed to: ${secondPart}_${firstPart}.${extension}"
mv "$file" "${secondPart}_${firstPart}.${extension}"
donehttps://stackoverflow.com/questions/72389795
复制相似问题