LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Rename problems.... (https://www.linuxquestions.org/questions/linux-newbie-8/rename-problems-565570/)

karnaf 06-30-2007 05:27 AM

Rename problems....
 
Searched many of the threads here trying to figure out how to use rename function, also checked the man and info pages, to no avail.

I've got a folder with plenty of mp3 tracks. The problem is many of them have multiple spaces in the name and the extension is in CAPS. So I thought I'd start little by replacing all extensions to lower case.

Following the rename example from the man pages, I tried this, but it didn't work
Code:

rename .MP3 .mp3 *.MP3
Searching a little the web, I've encountered the following format, which didn't work either
Code:

rename 's/\.MP3/\.mp3/g' *.MP3
I've checked my syntax using sed which echoed the right results, but of course, did not rename the files
Code:

ls -R | sed 's/\.MP3/\.mp3/g'
What am I missing?

Thanks,
karnaf

p.s. Running openSUSE 10.2 -> bash 3.1.17(1)

titopoquito 06-30-2007 05:58 AM

Your example works without a problem here, with or without spaces in the file names. Bash 3.2.15(2) on Slackware, rename from util-linux 2.12r.

SciYro 06-30-2007 05:59 AM

The problem: there are two seperate rename commands, one from perl, another more basic. The first example is the more basic rename command from util-linux i think, the second is for the perl version. So, the first question, what rename command do you have installed (as they are not compatible)?

Also, what do you mean by it dident work? Any error messages to show? And i assume are you already in the same directory as the files you want renamed.

karnaf 06-30-2007 06:21 AM

Using rename from util-linux-2.12r, so I guess we're dealing with the basic one.

Didn't work == no error msgs, just acting as if it has worked but no file changes made. Of course I'm in the directory and have permission to rename files there.

I've tried the more complex bash script
Code:

for OLD_FILE in *.MP3
do
  NEW_FILE=$(echo $OLD_FILE | tr '[:upper:]' '[:lower:]' | tr '[ ]+' '.')
  mv $OLD_FILE $NEW_FILE
done

Which throws errors for each file saying that $NEW_FILE is not a directory (???)

Cheers!
karnaf

titopoquito 06-30-2007 06:34 AM

I don't know why it won't work for you, but another solution:

for file in *.MP3; do mv ${file} ${file//.MP3/.mp3}; done

karnaf 06-30-2007 07:06 AM

Code:

for file in *.MP3; do mv ${file} ${file//.MP3/.mp3}; done
Not working either, but it looks like the original file name has character which mess up the scripts (such as spaces and brackets)

I ended up editing my above script to do the job
Code:

for OLD_FILE in *.MP3
do
  NEW_FILE=$(echo $OLD_FILE | tr '[:upper:]' '[:lower:]' | tr '[ ]+' '.' | sed 's/(\(.*\))//g' | sed 's/[\.]\{1,\}/./g')
  mv "$OLD_FILE" "$NEW_FILE"
done

But I am still curious how come the simple 'rename' thing could not work...


All times are GMT -5. The time now is 09:18 AM.