Renaming file-extention in the terminal?

fixarfrazze

Registered
Hi!

I'm trying to rename alot of file-extentions from the terminal with

>mv *.* *.mp3

But I seem to be missing something. I dont want to do it manualy cause there are allot of files... also the file renaming programs dont mannage the long filenames propperly (you get a strange name but no .mp3 at the end).
 
I dont think you can do that with mv , but there is another tool "mmv" that i downloaded and compiled a long time ago(in the public beta days)
I don't remeber where I found it though so you'l have to search for it.
 
Originally posted by fixarfrazze
Hi!

I'm trying to rename alot of file-extentions from the terminal with

>mv *.* *.mp3

But I seem to be missing something. I dont want to do it manualy cause there are allot of files... also the file renaming programs dont mannage the long filenames propperly (you get a strange name but no .mp3 at the end).

Assumign you have the right stuff installed
open a terminal
type bash

for i in files
do
mv $i $i.mp3
done
 
Thanx for your help but
Installed... something special?
i get:
mv: rename files to files.mp3: No such file or directory

I'v tried (in a beastie boys folder)
for i in Beastie* ; do mv $i $i.mp3 ; done
with
usage: mv [-fi] source target
mv [-fi] source ... directory
usage: mv [-fi] source target
mv [-fi] source ... directory... and so on for every file as response
 
Originally posted by fixarfrazze
Thanx for your help but
Installed... something special?
i get:
mv: rename files to files.mp3: No such file or directory

I'v tried (in a beastie boys folder)
for i in Beastie* ; do mv $i $i.mp3 ; done
with
usage: mv [-fi] source target
mv [-fi] source ... directory
usage: mv [-fi] source target
mv [-fi] source ... directory... and so on for every file as response

I always forget BSD's move is inheritly differnt than Linux's move.
 
Please try:
for i in Beasty* ; do echo $i ; done

You should see that the file names contain blanks, hence the error. Has nothing to do with bsd or linux.

So to correctly process these file names you should quote them, as in:

for i in Beasty* ; do mv "$i" "$i.mp3" ; done

This should work, but please try this first in a test directory ;)

Anyway, what will happen is that you append .mp3 to each file name. This solution does not trim a previous file extension and replace it with .mp3, instead it appends .mp3 to any existing extension. I'm not sure if this is what you want.
 
The files i want to change have no file extention... So thats what i want.
Anyway thanx for your help now it worked:

bash-2.05a$ for i in B* ; do mv "$i" "$i.mp3"; done
bash-2.05a$ ls
Beastie Boys - (You Gotta) Fight for Your Right (To Party).mp3
Beastie Boys - Alive.mp3

Now i just wonder how i add an alias for that in bash.
 
ln -s source_path destination_path

i.e. Say you're in your Documents folder and want to make a symbolic link (alias) to your Pictures folder, you'd do ln -s Users/your_user_name/Pictures . (the . means "this directory" in the shell, you could also have put /Users/your_user_name/Documents). Also, you can rename the symbolic link by naming it in the destination (say you want to change the name of the above to Pics, you'd either do ./Pics or the full path with Pics at the end).

Note that symbolic links look like unknown files in the Finder and can't be used (well, in 10.2.x, anyway; they acted more like normal aliases in 10.1.x if you're still using it). So if you want to use the alias in the Finder rather than the terminal, make an alias in the Finder in the normal manner.
 
ok what i meant was an alias like in the .tcshrc file. So i can type 'mp3' in bash and bash executes it like "for i in * ; do mv "$i" "$1".mp3 ; done"
 
Ah. I thought you meant a file alias. My fault :D

Add to .tcshrc:

alias mp3='for i in !^*; do mv "$i" "$i".mp3; done'

(The !^ is a command substitute with tcsh...this would let you name the directory to use i.e. mp3 B would do like what you did above.)

Now, I believe this will work, I actually use the bash shell rather than the default tcsh, but I think the alias syntax is the same. If it's not, somebody correct me. ;)
 
There's just one problem. The "for" control structure does not exist in tcsh. Why don't you just create a shell script? Create a plain text file either with 'pico' in Terminal or with TextEdit.app. Write this:
Code:
#!/bin/tcsh

foreach file (*)
    mv "$file" "$file:r.mp3"
end
This will replace the previous extension with ".mp3". When you use a variable with a trailing ":r" it gives you the root file name, i.e., without the extension.
 
Permission denied? Is what I get when running ./rename (the file with the code)

If I have a file with a link (one on each row like: "/Folder/file") to all of those files I wish to rename is there anyway to get the "foreach" to go through just these files?

I'm realy grateful for your time...
 
You should see that the file names contain blanks, hence the error. Has nothing to do with bsd or linux.

Now I know what I was doing, previously I was using ls to list the files of a directory, then i'd cat those files. ls would save the \ delimiters, so thats where my error was. Lateron in my script I changed it to "$I"

It's all good...wrote that post on a windows machine, not on the mac at werk.
:>
 
Originally posted by fixarfrazze
Permission denied? Is what I get when running ./rename (the file with the code)

You probably forgot to make that file executable. chmod +x rename
this sets the x-permission (execute bit), that's it.
 
I found this:

foreach old ( *.c )
set new=`echo $old | sed 's/.c/.cc/'`
mv $old $new
end
here

To test, I made it a shell script called renamer in a test directory called renamer.
( I changed .c to .j because I had no .j files, just in case)

#!/bin/tcsh
foreach old ( *.j )
set new=`echo $old | sed 's/.j/.jj/'`
mv $old $new
end

chmod +x renamer



Made a bunch of files ala touch, a.j b.j c.j d.j in the same directory, and put content in one to see if it was retained.
did ./renamer
and they all became a.jj b.jj c.jj d.jj --content unchanged.
 
use cp *.* *.mp3 it works with any file extension, so if u wanted to name hi.jar.1 to hi.jar then type cp hi.jar.1 hi.jar
 
Back
Top