Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Rename files in Terminal using mv and sed

I want to rename multiple files (in one directory) using mv and sed in Terminal (Mac OS 10.6.8).


I want to change each and every file name from IMG_nnnn.jpg to HVAC_nnnn.jpg, where nnnn is the camera's sequence number. I want to preserve the sequence number and the jpg extension while changing only the file name prefix (the part before the underscore).


Here's what I tried:


macatony-2:HVAC ctossc$ ls -d *.jpg | sed -e 's/.*/mv -f & &/' -e 's/IMG/HVAC/' | sh


This is the result:


mv: rename HVAC_0205.jpg to IMG_0205.jpg: No such file or directory


I wanted "rename IMG_0205.jpg to HVAC_0205.jpg". The arguments to mv seem to be reversed, so the mv command can't "rename" (or delete) the HVAC* file before it is created.


Any suggestions?

macbook, Mac OS X (10.6.7)

Posted on Aug 7, 2013 5:25 PM

Reply
Question marked as Best reply

Posted on Aug 7, 2013 6:32 PM

cd /path/to/folder

for filename in IMG_*; do mv "$filename" "${filename//IMG_/HVAC_}"; done



To check before execution to make sure the output is correct, you can pipe it to a text file before terminal actually executes:


for filename in IMG_*; do echo mv \"$filename\" \"${filename//IMG_/HVAC_}\"; done > test.txt

8 replies
Question marked as Best reply

Aug 7, 2013 6:32 PM in response to SemiTech

cd /path/to/folder

for filename in IMG_*; do mv "$filename" "${filename//IMG_/HVAC_}"; done



To check before execution to make sure the output is correct, you can pipe it to a text file before terminal actually executes:


for filename in IMG_*; do echo mv \"$filename\" \"${filename//IMG_/HVAC_}\"; done > test.txt

Aug 8, 2013 7:14 AM in response to SemiTech

I still wonder however, why my original code reversed the order of the mv arguments. Knowing that would make my earlier failed attempts a learning experience.


1st) nbar's 'for' loop is the best in my opinion 🙂


Why your script got things backwards. lets break it down



ls -d *.jpg | sed -e 's/.*/mv -f & &/'



generates:

mv -f IMG_1.jpg IMG_1.jpg
mv -f IMG_2.jpg IMG_2.jpg
mv -f IMG_3.jpg IMG_3.jpg
mv -f IMG_4.jpg IMG_4.jpg
mv -f IMG_5.jpg IMG_5.jpg
mv -f IMG_6.jpg IMG_6.jpg
mv -f IMG_7.jpg IMG_7.jpg
mv -f IMG_8.jpg IMG_8.jpg
mv -f IMG_9.jpg IMG_9.jpg


ls -d *.jpg | sed -e 's/.*/mv -f & &/' -e 's/IMG/HVAC/'



generates:

mv -f HVAC_1.jpg IMG_1.jpg
mv -f HVAC_2.jpg IMG_2.jpg
mv -f HVAC_3.jpg IMG_3.jpg
mv -f HVAC_4.jpg IMG_4.jpg
mv -f HVAC_5.jpg IMG_5.jpg
mv -f HVAC_6.jpg IMG_6.jpg
mv -f HVAC_7.jpg IMG_7.jpg
mv -f HVAC_8.jpg IMG_8.jpg
mv -f HVAC_9.jpg IMG_9.jpg


because when the -e 's/IMG/HVAC/' executed, it changed the 1st occurance of IMG, which was the IMG following the -f


I personally do not use 'sed' anymore except for some really trivial stuff. If I need to general messing with a list or the contents of a file I use 'awk' or 'perl', both of which are a bit easier to work with and for me to read afterwards. But if you really wanted to fix the 'sed' you could have used:


ls -d *.jpg | sed -e 's/IMG\(.*\)/mv -f & HVAC\1/' | sh


HOWEVER, I would not really use either 'awk' nor 'perl' for this task. I would use something similar to what 'nbar' gave you (although I might have over complicated mine some more where as 'nbar' kept it simple and easy to read 🙂


Message was edited by: BobHarris

Rename files in Terminal using mv and sed

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.