在linux中查找没有扩展名的文件并将其重命名

时间:2010-09-07 06:26:15

标签: linux grep rename file-rename

我的服务器上有一堆文件如下所示。我需要编写一个脚本,将所有文件从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

5 个答案:

答案 0 :(得分: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

的标准工具

答案 1 :(得分:3)

鉴于您的示例中的文件名模式,这将起到作用:

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

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

find . -name "*[0-9]JPG" -exec mv \{\} \{\}.jpg \;

答案 2 :(得分:1)

bash 4

#!/bin/bash
shopt -s globstar
for file in **/*JPG
do
  mv "$file" "${file/JPG/.jpg}"
done

答案 3 :(得分: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)。

答案 4 :(得分: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