简体   繁体   English

在Linux中大规模重命名文件

[英]Mass rename of files in linux

I have files i linux that are in format : 我的Linux档案格式如下:

img_1234563_@_12345.XX-FFF-123334-35370-122232-12342.gif
img_1234fdfd3_fd12345.XX-FdFF-1233343-35370-145232-d12342.gif

that I would like to rename to the something like 我想重命名为

img_1234fdfd3_fd12345.jpg

So cut all after first dot and put jpg at the end 因此,请在第一个点后将所有内容剪掉,并在最后放置jpg

I have tried to use for and rename with pattern like img_/\\S[//^.]+ but without much success :( 我尝试使用img _ / \\ S [//^.]+这样的模式进行重命名,但没有成功:(

You can use the String split to divide your file names. 您可以使用字符串拆分来分割文件名。 When having a variable s you can retain the shortest match of a pattern from the beginning using ${MYVAR/%pattern} . 当具有变量s您可以使用${MYVAR/%pattern}从一开始就保留模式中最短的匹配项。

I do not know if you are processing all the entries on a directory so I suppose you have a variable files containing all the file names you need to rename. 我不知道您是否正在处理目录中的所有条目,所以我想您有一个包含所有需要重命名的文件名的变量files Otherwise you just need to change the loop bound to fit your list of files. 否则,您只需要更改循环绑定即可适合您的文件列表。

#!/bin/bash

files="img_1234563_@_12345.XX-FFF-123334-35370-122232-12342.gif img_1234fdfd3_fd12345.XX-FdFF-1233343-35370-145232-d12342.gif"

for file in $files; do
        name="${file/%.*}"
        fullName="${name}.jpg"                                                                                                                                                                                                                                             
        mv "$file" "$fullName"
done 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM