Find files without extension in linux and bulk rename them

I have a bunch of files on my server that look below. I need to write a script that renames all files from JPG to .jpg as you can see, some of them already have the extension .jpg, and some do not.

Could you help me? I could do this either in bash or through php, which is simpler, I just don't know how to do it.

thank

Jason

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
+3
source share
5 answers

Given the file name patterns in your example, this will do the trick:

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

If you need a recursive solution:

find . -name "*[0-9]JPG" -exec mv \{\} \{\}.jpg \;
+3
source
find . -name '*JPG.jpg' -print0 | xargs -0 rename JPG.jpg .jpg
find . -name '*JPG' -print0 | xargs -0 rename JPG .jpg

rename(1) is a standard tool from util-linux

+4
source

bash 4

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

, .

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).

0
source

I will go after this beast. Not the most elegant version, but rename any files ending in (case insensitive) 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
0
source

Source: https://habr.com/ru/post/1763561/


All Articles