-   Linux - Newbie (
-   -   Rename list of files from command promp!!! (

Drigo 02-18-2010 10:52 AM

Rename list of files from command promp!!!
Suppose I have the following files:


I want to rename them so everyone look like:


So I want to add PD_ to those who dont have this preindex and get rid of _fr.

Please help....basically rename files in a same directory!!

Tinkster 02-18-2010 11:06 AM


for i in *fr.mp3; do echo mv $i $( echo "PD_"$(echo $i|egrep -o '[0-9]{4}_[0-9]+')".mp3" ); done

When you're sure it works, remove the bold echo.

rikijpn 02-18-2010 11:13 AM

man bash, man sed, man any_decent_editor
There are like a thousand of ways to do this, you should try google too.

The shortest way (to explain) is using bash like this:

for FILE in $(ls PD_*fr*mp3); do mv $FILE "${FILE//_fr}" ; done

for FILE in $(ls [0-9]*mp3); do mv $FILE PD_"${FILE//_fr}" ; done
The first for, is for files starting with PD, containing fr and ending with mp3. It just changes their names to a name without the _fr part.
The second one moves any file starting with a number and ending mp3, to PD_"+its name". So no files starting with PD_ will be changed. But any already existing files would be overwritten. Might want to try "mv -i" just in case.

Drigo 02-18-2010 12:13 PM how do I create bash? (Sorry Im pretty new at linux...I know what it is but I dont know how to create it.)

reed9 02-18-2010 12:30 PM

Bash is the default shell in most distributions. Any time you open a terminal, you are using bash.

In brief you can either enter those commands into the terminal to run it, or create a script. A script is just a text file with the commands, basically.

So it might look like


for i in *fr.mp3; do echo mv $i $( echo "PD_"$(echo $i|egrep -o '[0-9]{4}_[0-9]+')".mp3" ); done

Drigo 02-18-2010 03:13 PM

Thank you.. I got it working but I tried it again and....

~\EXAMPLE> ./exe | sh
mv: cannot stat `*fr.mp3': No such file or directory

My file editor:

for i in *fr.mp3;
do echo mv $i $( echo "PD_"$(echo $i|egrep -o '[0-9]{4}_[0-9]+')".mp3" );

Tinkster 02-18-2010 03:26 PM


Are those files in the EXAMPLE directory?

Btw, if you get rid of the first echo as I suggested in my
first post you save yourself the "| sh"


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