Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Renaming multiple files

Renaming multiple files
Thread Tools
Andrew at work
Fresh-Faced Recruit
Join Date: Jul 2001
Status: Offline
Reply With Quote
Jul 24, 2001, 04:59 PM
 
Is there any easy way to rename a directory full of files from the terminal? I have a dir with 50 some images in it. I'd like to be able to add a word to the front of each file name. So, instead of having:

image.jpg
neatpicture.jpg
cry.jpg

I could rename them all at once to:

web_image.jpg
web_neatpicture.jpg
web_cry.jpg

Any ideas? Thanks!
     
<trey>
Guest
Status:
Reply With Quote
Jul 24, 2001, 05:53 PM
 
Originally posted by Andrew at work:
<STRONG>Is there any easy way to rename a directory full of files from the terminal? I have a dir with 50 some images in it. I'd like to be able to add a word to the front of each file name. So, instead of having:

image.jpg
neatpicture.jpg
cry.jpg

I could rename them all at once to:

web_image.jpg
web_neatpicture.jpg
web_cry.jpg

Any ideas? Thanks!</STRONG>
find *.jpg -exec mv {} web_{} \;
     
cpt kangarooski
Mac Elite
Join Date: May 2001
Status: Offline
Reply With Quote
Jul 25, 2001, 01:43 AM
 
Wow. Even DOS has a REN command. (which in this case would be REN web_*.*, I think) IF Apple wants OS X to be easy to use, that pretty much includes dumping bash and writing a good shell.

At any rate, there's a Classic program called File Renamer... MacRenamer... something like that. It's on the regular ftp sites, and will do what you want within the old 31 character limit. I use it a lot at work.
--
This and all my other posts are hereby in the public domain. I am a lawyer. But I'm not your lawyer, and this isn't legal advice.
     
gorgonzola
Admin Emeritus
Join Date: Nov 2000
Location: New Yawk
Status: Offline
Reply With Quote
Jul 25, 2001, 04:40 AM
 
Mac OS X doesn't use bash. I know what you mean though.
"Do not be too positive about things. You may be in error." (C. F. Lawlor, The Mixicologist)
     
Andrew at work  (op)
Fresh-Faced Recruit
Join Date: Jul 2001
Status: Offline
Reply With Quote
Jul 25, 2001, 11:40 AM
 
Originally posted by &lt;trey&gt;:
<STRONG>

find *.jpg -exec mv {} web_{} \;
</STRONG>


All this does is take all my .jpg files, and rename them one at a time to web_ . I'm left with a single file (the last .jpg) named web_ .

I'd really like to have a bunch of separate files each with a web_ prefix. As it is, the command is handy for deleting a bunch of .jpgs, but not so good for me.

The code trey posted is probably close, but not close enough. Any more ideas? Thanks!
     
acur128
Junior Member
Join Date: May 2001
Status: Offline
Reply With Quote
Jul 25, 2001, 01:34 PM
 
adding quotes to the new name should do it:

find *.jpg -exec mv {} 'web_{}' \;

if you want to confirm each change, (& see what the result will be), try:

find *.jpg -ok mv {} 'web_{}' \;

or... for the gui approach, you could save the following in "Script Editor" as a Mac OS X applet, & run it from the Finder (or from the shell (open scriptname)):
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set theFolder to choose folder with prompt <font color = red>"Please choose a folder..."</font>
set ext_to_process to text returned of (display dialog <font color = red>"Please enter the file extension <font color = green>for</font> files to be processed in the selected folder..."</font> <font color = green>default</font> answer <font color = red>".jpg"</font> with icon <font color = blue>1</font>)
set the_prefix to text returned of (display dialog <font color = red>"Please enter the new prefix <font color = green>for</font> files with the extension \"</font>" & ext_to_process & <font color = red>"\"</font> in the selected folder...<font color = red>" <font color = green>default</font> answer "</font>web_" with icon <font color = blue>1</font>)
set rename_all to false
<font color = green>if</font> ext_to_process as string = <font color = red>""</font> then
<font color = green>if</font> button returned of (display dialog <font color = red>"Add the prefix \"</font>" & the_prefix & <font color = red>"\"</font> to all files in the selected folder (regardless of extension)?<font color = red>" with icon <font color = blue>1</font>) = "</font>OK" then
set rename_all to true
end <font color = green>if</font>
end <font color = green>if</font>
set theList to list folder theFolder
repeat with anitem in theList
set theFile to alias ((theFolder as string) & contents of anitem)
tell application <font color = red>"Finder"</font>
<font color = green>if</font> rename_all is true then
set name of theFile to ((the_prefix as string) & (name of theFile)) as string
<font color = green>else</font>
<font color = green>if</font> name of theFile ends with (ext_to_process as string) then
set name of theFile to ((the_prefix as string) & (name of theFile)) as string
end <font color = green>if</font>
end <font color = green>if</font>
end tell
end repeat</font>[/code]
edit: some problems with /usr/bin/osascript & dialogs (should be fixed in 10.1 (hopefully))... opening the script from the shell should be fine

[ 07-25-2001: Message edited by: acur128 ]
     
<unregistered>
Guest
Status:
Reply With Quote
Jul 25, 2001, 02:00 PM
 
Originally posted by cpt kangarooski:
<STRONG>Wow. Even DOS has a REN command. (which in this case would be REN web_*.*, I think) IF Apple wants OS X to be easy to use, that pretty much includes dumping bash and writing a good shell.

At any rate, there's a Classic program called File Renamer... MacRenamer... something like that. It's on the regular ftp sites, and will do what you want within the old 31 character limit. I use it a lot at work.</STRONG>
please don't blame a shell for your lack of knowledge, while Mac OS X defaults to tcsh, which is actually not being developed anymore, bash, and other Bourne-style shells are the most featureful and powerful shells that exist. COMMAND.COM is not really a shell, it's a poor command interpreter. and by the way your ren isn't correct, "The syntax of the command is incorrect." REN = move, mv in unix. You're moving one file to the new name. There's no easy way to do it in Windows, or Mac OS 9 and below, without extra additional ShArEwArE SoFtWaRe, so using simple unix utilities, like find, awk, and sed, are pretty damn awesome.

much like programming, in unix there are many ways to accomplish something.
     
LordJavac
Forum Regular
Join Date: Oct 2000
Location: Portland, OR USA
Status: Offline
Reply With Quote
Jul 25, 2001, 03:40 PM
 
Most implementations of find don't resolve {} as part of a string. I suggest a for loop thusly:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<font color = green>for</font> file in *.jpg
<font color = green>do</font>
mv $file web_$file
done
</font>[/code]

which can be compressed to one line as:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier><font color = green>for</font> file in *.jpg; <font color = green>do</font> mv $file web_$file; done</font>[/code]
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:03 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,