LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 02-18-2010, 10:52 AM   #1
Drigo
Member
 
Registered: May 2009
Posts: 103

Rep: Reputation: 15
Angry 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!!
 
Old 02-18-2010, 11:06 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
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.
 
Old 02-18-2010, 11:13 AM   #3
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
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.
 
Old 02-18-2010, 12:13 PM   #4
Drigo
Member
 
Registered: May 2009
Posts: 103

Original Poster
Rep: Reputation: 15
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.)
 
Old 02-18-2010, 12:30 PM   #5
reed9
Member
 
Registered: Jan 2009
Location: Boston, MA
Distribution: Arch Linux
Posts: 653

Rep: Reputation: 141Reputation: 141
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/
 
Old 02-18-2010, 03:13 PM   #6
Drigo
Member
 
Registered: May 2009
Posts: 103

Original Poster
Rep: Reputation: 15
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
 
Old 02-18-2010, 03:26 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename files matching a list Qu3ry Programming 34 07-22-2009 06:59 PM
Rename Files With Date Picture Taken On Command lonecrow Linux - General 1 10-30-2006 12:18 PM
How can I rename a lot of files with a single command? pujolasdf Linux - Software 4 07-20-2005 05:27 AM
awk cli to rename a list of files... pld Linux - General 4 02-15-2005 10:57 PM
rename files on a command line? CrazyPilot Linux - General 9 06-30-2004 04:10 PM


All times are GMT -5. The time now is 02:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration