# Renaming file-extention in the terminal?



## fixarfrazze (Apr 2, 2003)

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).


----------



## gumse (Apr 2, 2003)

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.


----------



## seann (Apr 3, 2003)

> _Originally posted by fixarfrazze _
> *Hi!
> 
> I'm trying to rename alot of file-extentions from the terminal with
> ...



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

for i in files
do
mv $i $i.mp3
done


----------



## fixarfrazze (Apr 3, 2003)

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


----------



## seann (Apr 4, 2003)

> _Originally posted by fixarfrazze _
> *Thanx for your help but
> Installed... something special?
> i get:
> ...



I always forget BSD's move is inheritly differnt than Linux's move.


----------



## fixarfrazze (Apr 4, 2003)

seann:

So what you ar saying is that it wont work, or just that you dont know how to do it?


----------



## rhg (Apr 4, 2003)

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.


----------



## fixarfrazze (Apr 4, 2003)

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.


----------



## Darkshadow (Apr 4, 2003)

*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.


----------



## fixarfrazze (Apr 4, 2003)

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"


----------



## Darkshadow (Apr 4, 2003)

Ah.  I thought you meant a file alias.  My fault 

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.


----------



## Pavao (Apr 4, 2003)

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:

```
#!/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.


----------



## fixarfrazze (Apr 5, 2003)

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...


----------



## seann (Apr 5, 2003)

_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.
 :>


----------



## rhg (Apr 6, 2003)

> _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.


----------



## fixarfrazze (Apr 6, 2003)

What about:

0: Event not found.

(should i put it in /usr/bin/ or some were else?)


----------



## recess20026 (Apr 7, 2003)

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.


----------



## AKsicppy (Jul 29, 2011)

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


----------

