3

我的服务器上有一堆文件,如下所示。我需要编写一个脚本,将所有文件从 JPG 重命名为 .jpg,因为您可以看到其中一些已经具有 .jpg 扩展名,而有些则没有。

你能帮助我吗?我可以用 bash 或通过 php 来完成,不管怎样更容易,我只是不知道怎么做。

谢谢

杰森

jects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4742JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4749JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4806JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4726JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4716JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/AGSM-Ground-Floor-2010-036JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4728JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4736JPG.jpg
media/projects/Education/UNSW-AGSM-Ground-Floor-Thr/DSC4724JPG.jpg
media/projects/Education/St_Gregorys_School/IMG0100JPG
media/projects/Education/St_Gregorys_School/IMG0099JPG
media/projects/Education/St_Gregorys_School/IMG0092JPG
media/projects/Education/St_Gregorys_School/IMG0090JPG
media/projects/Education/St_Gregorys_School/IMG0084JPG
media/projects/Education/St_Gregorys_School/IMG0093JPG
media/projects/Education/St_Gregorys_School/IMG0097JPG
media/projects/Education/St_Gregorys_School/IMG0098JPG
media/projects/Education/St_Gregorys_School/IMG0085JPG
media/projects/Education/St_Gregorys_School/IMG0091JPG
media/projects/Education/St_Gregorys_School/IMG0094JPG
media/projects/Education/St_Gregorys_School/IMG0088JPG
media/projects/Education/St_Gregorys_School/IMG0087JPG
media/projects/Education/St_Gregorys_School/IMG0095JPG
media/projects/Education/St_Gregorys_School/IMG0096JPG
media/projects/Education/St_Gregorys_School/IMG0089JPG
media/projects/Education/St_Gregorys_School/IMG0086JPG
4

5 回答 5

4
find . -name '*JPG.jpg' -print0 | xargs -0 rename JPG.jpg .jpg
find . -name '*JPG' -print0 | xargs -0 rename JPG .jpg

rename(1)是标准工具util-linux

于 2010-09-07T06:39:19.570 回答
3

鉴于您示例中的文件名模式,这将解决问题:

for F in *[0-9]JPG; do mv $F $F.jpg; done

如果您需要递归解决方案:

find . -name "*[0-9]JPG" -exec mv \{\} \{\}.jpg \;
于 2010-09-07T07:12:52.460 回答
1

重击 4

#!/bin/bash
shopt -s globstar
for file in **/*JPG
do
  mv "$file" "${file/JPG/.jpg}"
done
于 2010-09-07T10:35:37.063 回答
0

这只是松散未经测试,但应该工作....

printf '#!/bin/sh\nmv -v $1 "$1".jpg' > addext.sh
chmod u+x addext.sh
find . -type f -name "*[0-9]JPG"  -exec ${PWD}/addext.sh "{}" ";"

这假设您正在使用 GNU 工具运行(因为您已将问题标记为 linux,所以您将使用它)。

于 2010-09-07T06:40:41.253 回答
0

我会去找这只野兽。不是最优雅的版本,但会重命名任何以(不区分大小写)JPG.jpg 结尾的文件

find . -type f | grep -i -E '(JPG)*\.*(JPG)+$' | while read file ; do mv $file `echo $file | perl -pe 's!(JPG)*\.*(JPG)+$!.jpg!gi'`; done
于 2010-09-07T06:49:40.837 回答