Home Man
Today's Posts

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Rename all ".JPG" files to ".jpg" under all subfolders...

shell scripts

Login to Reply

Thread Tools Search this Thread
# 1  
Old 04-25-2010
Rename all ".JPG" files to ".jpg" under all subfolders...

Hi, Dear all:

One question ! ^_^
I'm using bash under Ubuntu 9.10.

My question is not to rename all ".JPG" files to ".jpg" in a single folder, but to rename all ".JPG" files to ".jpg" in all subfolders.

To rename all ".JPG" to ".jpg" in a single folder,
for x in *.JPG; do mv "$x" "${x%.JPG}.jpg"; done

works fine for me.

But how to recursively enter every subfolder and rename all the files?
(Well, if possible, enter every subfolder's subfolder.. recursively?? )

In sum, given a folder, all files under this folder, and all its subfolder, and all the subsubfolders, should be finally renamed in one specific rule.

How to deal with it?

Thank you very much and looking forward to your reply.

Best Regards

Last edited by pludi; 04-25-2010 at 12:39 PM.. Reason: code tags, please...
# 2  
Old 04-25-2010
I guess this is far from a professional solution, but it seems to work:
$ du -a
0	./001.JPG
0	./002.JPG
0	./img1/a.JPG
0	./img1/b.JPG
2	./img1
0	./img2/1.JPG
0	./img2/2.JPG
0	./img2/pic10/pic1.JPG
0	./img2/pic10/pic2.JPG
2	./img2/pic10
4	./img2
8	.
$ du -a | cut -d '/' -f2- | grep JPG
$ for x in `du -a | cut -d '/' -f2- | grep JPG`; do mv "$x" "${x%.JPG}.jpg"; done
$ du -a
0	./002.jpg
0	./img1/b.jpg
0	./img1/a.jpg
2	./img1
0	./img2/2.jpg
0	./img2/pic10/pic2.jpg
0	./img2/pic10/pic1.jpg
2	./img2/pic10
0	./img2/1.jpg
4	./img2
0	./001.jpg
8	.

# 3  
Old 04-25-2010
Another way:

find /your/folder -type f | sed 's/\(.*\)\.JPG/mv \1.JPG \1.jpg/' | sh

# 4  
Old 04-25-2010
Originally Posted by verdepollo
Another way:

find /your/folder -type f | sed 's/\(.*\)\.JPG/mv \1.JPG \1.jpg/' | sh

That approach comes with hefty filename limitations (which may or may not be of importance to the original poster, but I'll mention them for completeness):

- Filenames cannot contain newlines (when reading find's output, it's impossible to distinguish between two filenames and one with an embedded newline. Unfixable.
- Filenames cannot contain any of the other shell-special characters (space, tab, backslash, single-quote, double-quote, semicolon, ampersand, pipe, >, <, etc). Possibly fixable, by backslash escaping every such character, but it would be a brittle solution.

Unrelatedly, either find should be constrained to match only files ending in ".JPG" or the matching regular expression in sed should be anchored to the end of the line, with "JPG$". Otherwise, mid-filename ".JPG" sequences will match filenames not ending in ".JPG".


---------- Post updated at 02:26 PM ---------- Previous update was at 01:52 PM ----------

If the solution must handle every allowable filename, I would suggest:
find . -type f -name \*.JPG -exec ./mvjpg.sh {} +

Where mvjpg.sh is:

for f; do
    mv "$f" "${f%.JPG}.jpg"

Obviously, you'll need to adjust the path to the shell script in the -exec primary to match its actual location on your system, if it's not on your $PATH.


Last edited by alister; 04-25-2010 at 03:08 PM..
# 5  
Old 04-25-2010
Dear alister:

This works perfect for me !!!!

Thank you very much !!! Thank you.

Best Regards
# 6  
Old 04-25-2010
Some shell parameter substitution might help.

 echo ${blar/JPG/jpg}

will output:

So that means you can do:

for JPG in `find /path -type f -name \*.JPG` ; do mv $JPG ${JPG/JPG/jpg} ; done

No need for a separate script.
# 7  
Old 04-26-2010
you can even use a simple rename command as
find -name '*.JPG' -exec rename .JPG .jpg {} \;

Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }" Siddharth shivh Shell Programming and Scripting 3 05-31-2018 10:46 AM
Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml" Hiroshi Shell Programming and Scripting 2 02-17-2017 01:06 PM
Using "mailx" command to read "to" and "cc" email addreses from input file asjaiswal UNIX for Dummies Questions & Answers 2 10-11-2014 09:32 PM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
ps -ef | grep "string1" "string2" " "string3" steve2216 Shell Programming and Scripting 11 09-03-2010 11:08 AM
"Join" or "Merge" more than 2 files into single output based on common key (column) Katabatic Shell Programming and Scripting 1 05-20-2010 11:41 AM
Delete files older than "x" if directory size is greater than "y" JamesCarter Shell Programming and Scripting 4 02-11-2010 07:41 AM
Unix commands delete all files starting with "X" except "X" itself. HELP!!!!? soccerball Shell Programming and Scripting 2 10-15-2009 07:24 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 12:52 AM

All times are GMT -4. The time now is 01:32 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
Show Password