LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Rename list of files from command promp!!! (http://www.linuxquestions.org/questions/linux-newbie-8/rename-list-of-files-from-command-promp-789964/)

Drigo 02-18-2010 11:52 AM

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

1132_1_fr.mp3
1132_2_fr.mp3
1132_3_fr.mp3
.
.
.
1132_3_fr.mp3
PD_1132_65_fr.mp3
PD_1132_7_fr.mp3
.
.
.



I want to rename them so everyone look like:

PD_1132_1.mp3
PD_1132_2.mp3
PD_1132_3.mp3
.
.
.
PD_1132_65.mp3

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 12:06 PM

Code:

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 12:13 PM

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:
Code:

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

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 01:13 PM

Thanks...now 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 01: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

Code:

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

http://www.panix.com/~elflord/unix/bash-tute.html
http://en.wikipedia.org/wiki/Bash
http://tldp.org/LDP/abs/html/

Drigo 02-18-2010 04: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:

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

Tinkster 02-18-2010 04:26 PM

And?

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"



Cheers,
Tink


All times are GMT -5. The time now is 11:35 AM.