Unix/Linux Go Back    


Shell Programming and Scripting 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 Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-25-2010   -   Original Discussion by jiapei100
jiapei100's Unix or Linux Image
jiapei100 jiapei100 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 10 February 2012, 6:27 PM EST
Location: China
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
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,


Code:
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
JIA Pei

Last edited by pludi; 04-25-2010 at 12:39 PM.. Reason: code tags, please...
Sponsored Links
    #2  
Old Unix and Linux 04-25-2010   -   Original Discussion by jiapei100
pseudocoder's Unix or Linux Image
pseudocoder pseudocoder is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 17 February 2012, 6:42 AM EST
Location: /home/sea
Posts: 602
Thanks: 40
Thanked 83 Times in 81 Posts
I guess this is far from a professional solution, but it seems to work:


Code:
$ 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
001.JPG
002.JPG
img1/a.JPG
img1/b.JPG
img2/1.JPG
img2/2.JPG
img2/pic10/pic1.JPG
img2/pic10/pic2.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	.
$

Sponsored Links
    #3  
Old Unix and Linux 04-25-2010   -   Original Discussion by jiapei100
verdepollo's Unix or Linux Image
verdepollo verdepollo is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 16 March 2018, 1:12 PM EDT
Location: Mexico
Posts: 725
Thanks: 11
Thanked 105 Times in 102 Posts
Another way:



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

    #4  
Old Unix and Linux 04-25-2010   -   Original Discussion by jiapei100
alister's Unix or Linux Image
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 978 Times in 791 Posts
Quote:
Originally Posted by verdepollo View Post
Another way:



Code:
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".

Regards,
Alister

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

If the solution must handle every allowable filename, I would suggest:


Code:
find . -type f -name \*.JPG -exec ./mvjpg.sh {} +

Where mvjpg.sh is:


Code:
#!/bin/sh

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


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.

Regards,
Alister

Last edited by alister; 04-25-2010 at 03:08 PM..
Sponsored Links
    #5  
Old Unix and Linux 04-25-2010   -   Original Discussion by jiapei100
jiapei100's Unix or Linux Image
jiapei100 jiapei100 is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 10 February 2012, 6:27 PM EST
Location: China
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Dear alister:

This works perfect for me !!!!

Thank you very much !!! Thank you.

Best Regards
JIA
Sponsored Links
    #6  
Old Unix and Linux 04-25-2010   -   Original Discussion by jiapei100
masta's Unix or Linux Image
masta masta is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 26 April 2010, 3:58 PM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Some shell parameter substitution might help.



Code:
 blar=foo.JPG
 echo ${blar/JPG/jpg}

will output:


Code:
foo.jpg

So that means you can do:



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

No need for a separate script.
Sponsored Links
    #7  
Old Unix and Linux 04-26-2010   -   Original Discussion by jiapei100
thegeek's Unix or Linux Image
thegeek thegeek is offline
Read Only
 
Join Date: Apr 2009
Last Activity: 30 July 2012, 5:38 AM EDT
Location: /usr/bin/vim
Posts: 946
Thanks: 13
Thanked 38 Times in 36 Posts
you can even use a simple rename command as

Code:
find -name '*.JPG' -exec rename .JPG .jpg {} \;

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 03:15 AM



All times are GMT -4. The time now is 04:26 AM.