LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2007, 12:47 AM   #1
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Rep: Reputation: 0
How can I change the prefix of multiple files in a directory


Hello All,

I have found ways to change file names, file extensions, uppercase to lowercase but I cant figure out how to change the prefix of multiple files in a directory using a script. For example:

Change file 1: "Favorites_Jan1998_CD01.1c.mp3" to "199801_Favorites_CD01.1c.mp3"

Change file 2: "Favorites_Jan1998_CD01.2c.mp3" to "199801_Favorites_CD01.2c.mp3"

Change file 3: "Favorites_Jan1998_CD02.1c.mp3" to "199801_Favorites_CD02.1c.mp3"

and so on.

In other words, I want to strip everything before the CD number and add the string "199801_Favorites_CD"

Thanks

Last edited by 6millionbucks; 07-31-2007 at 10:05 AM.
 
Old 07-31-2007, 01:27 AM   #2
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Rep: Reputation: 30
Quote:
Originally Posted by 6millionbucks
Hello All,

I have found ways to change file names, file extensions, uppercase to lowercase but I cant figure out how to change the prefix of multiple files in a directory using a script. For example:

Change file 1: "Favorites_Jan1998_CD01.1c.mp3" to "199801_Favorites_CD01.1c.mp3"

Change file 2: "Favorites_Jan1998_CD01.2c.mp3" to "199801_Favorites_CD01.1c.mp3"

Change file 3: "Favorites_Jan1998_CD02.1c.mp3" to "199801_Favorites_CD01.1c.mp3"

and so on.

In other words, I want to strip everything before the CD number and add the string "199801_Favorites_CD"

Thanks
may be usefull.

Code:
for f in Favorites_*.mp3 ## ls is unnecesary and will break the script
do
  touch "${Favorites_f%}199801_Favorites_CD01.1c.mp3" ## no external command is necessary
done
cheers
munna
 
Old 07-31-2007, 10:24 AM   #3
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by munna_dude
may be usefull.

Code:
for f in Favorites_*.mp3 ## ls is unnecesary and will break the script
do
  touch "${Favorites_f%}199801_Favorites_CD01.1c.mp3" ## no external command is necessary
done
cheers
munna

Thanks munna for the reply. It kind of worked (as far as renaming) the first file in the directory and then I realized I had typos in my original post. Ooooppps, sorry. Its suppose to read:

Change file 1: "Favorites_Jan1998_CD01.1c.mp3" to "199801_Favorites_CD01.1c.mp3"

Change file 2: "Favorites_Jan1998_CD01.2c.mp3" to "199801_Favorites_CD01.2c.mp3"

Change file 3: "Favorites_Jan1998_CD02.1c.mp3" to "199801_Favorites_CD02.1c.mp3"


How do I get the script to rename for all the files in the directory. The files should have the same prefix (199801_Favorites_CD) but a different CD and track number (01.1c, 01.2c, 02.1c).

Lastly, the reason I said "kind of worked" is because the original file size was 3 MBs, the renamed file is 0. Why do you think that is?

Thanks again for your help.

Last edited by 6millionbucks; 07-31-2007 at 10:25 AM.
 
Old 07-31-2007, 10:47 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Try this:
Code:
for i in *.mp3; do
   rename Favorites_Jan1998 199801_Favourites $i
done
Or you could do it with a find command, which might be more efficient if you have a huge number of files.

edit: Clearly I have no idea what I'm talking about with respect to touch. Touch won't affect existing files other than to change certain aspects of them (e.g. timestamp etc.). Don't know why they're 0 bytes now. Sorry!

Last edited by pwc101; 07-31-2007 at 10:51 AM.
 
Old 07-31-2007, 11:50 AM   #5
nuying117
LQ Newbie
 
Registered: Jul 2007
Posts: 3

Rep: Reputation: 0
why dont you use mv command? I dont know why you all use rename or other command

Quote:
for f in `ls`
do
mv $f "198101_${f}"
done
 
Old 07-31-2007, 12:01 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pwc101
edit: Clearly I have no idea what I'm talking about with respect to touch. Touch won't affect existing files other than to change certain aspects of them (e.g. timestamp etc.). Don't know why they're 0 bytes now. Sorry!
Very dangerous to suggest some untested code to a newbie! And very dangerous for a newbie to accept some code without testing or seeing man pages if some command are unknown! Here is the explanation: the touch command update the timestamp of a file to the current time or a desired one (with -t option). The touch command with a non-existent file as argument, simply creates an empty file.
The command
Code:
touch "${Favorites_f%}199801_Favorites_CD01.1c.mp3"
probably was meant to strip a substring from the original file name and replace it with a new one, but as you have seen, this was not the correct way to do. The example with the rename command should work, instead. Cheers.
 
Old 07-31-2007, 12:06 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
for f in `ls`
do
mv $f "198101_${f}"
done
Same as above... dangeroooous! He he he... I look pedantic, I know!
 
Old 07-31-2007, 06:16 PM   #8
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks nuying117, pwc101 and colucix.

As far as

Code:
for i in *.mp3; do
   rename Favorites_Jan1998 199801_Favorites $i
done
Nothing happens when I run this.

As for the code

Code:
for f in `ls`
do
mv $f "198101_${f}"
done
It's getting closer to what I'm looking for except it didn't remove the "Jan1998":

"Favorites_Jan1998_CD01.1c.mp3" becomes "199801_Favorites_Jan1998_CD01.1c.mp3"

Still need to drop the "Jan1998" of the final name.

I'm currently digging up info on string replace. I think it would be simpler to replace the string "Favorites_Jan1998" with the string "199801_Favorites".

Would appreciate your help with this approach. Thanks everybody... you guys are great.
 
Old 07-31-2007, 10:24 PM   #9
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Original Poster
Rep: Reputation: 0
I've been at this all day... I'm calling it a night but here is what I came up with so far. I found a script on this website that changes "spaces" to "_".

So I figured I would try changing "Favorites_Jan1998" to "199801_Favorites" in the code below.

Code:
for f in *.mp3; do
     file=$(echo $f | tr 'Favorites_Jan1998' '1998_Favorites')
     [ ! -f $file ] && mv "$f" $file
done

Output:

The original file "Favorites_Jan1998_CD01.1c.mp3" becomes "1998_FavorJ9nieesrCD0i.ic.mp3"

Why the strange name? What am I doing wrong?

Help
 
Old 08-01-2007, 02:36 AM   #10
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
tr 'Favorites_Jan1998' '1998_Favorites' - tr doesn't do words, it does individual letters. For words, something like sed might be more appropriate:
Code:
for f in *.mp3; do
     file=$(echo $f | sed 's/Favorites_Jan1998/1998_Favorites/g')
     [ ! -f $file ] && mv "$f" $file
done
 
Old 08-01-2007, 02:48 AM   #11
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by colucix
Very dangerous to suggest some untested code to a newbie!
I didn't suggest the touch code! I had to edit my post as I had written something about touch which was clearly wrong. That's what I was apologising for there
 
Old 08-01-2007, 08:08 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pwc101
I didn't suggest the touch code! I had to edit my post as I had written something about touch which was clearly wrong. That's what I was apologising for there
Sorry, pwc101! You make me notice I have quoted the erroneous text. Indeed, it was not you which posted the example with touch. By the way my comment was simply and slightly ironic (I hope this was clear). Cheers
 
Old 08-01-2007, 08:12 AM   #13
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
I knew you were being slightly sarcastic, so I didn't take offence, but I thought I'd point it out in case someone else tried it and I got it in the neck! Got to keep up appearances, you know...
 
Old 08-01-2007, 11:05 AM   #14
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Talking SOLVED: How to change the prefix of multiple files in a directory

Quote:
Originally Posted by pwc101
tr 'Favorites_Jan1998' '1998_Favorites' - tr doesn't do words, it does individual letters. For words, something like sed might be more appropriate:
Code:
for f in *.mp3; do
     file=$(echo $f | sed 's/Favorites_Jan1998/1998_Favorites/g')
     [ ! -f $file ] && mv "$f" $file
done
That did the trick, thanks so much. I'm reading up on sed now. A week ago a didn't know a thing about scripting or programing, and today I still don't but I know I love it.

Thanks pwc101 and everyone else for your help and comments
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy files from multiple directories into one directory MadRabbit Linux - Newbie 8 02-07-2014 07:56 PM
How do I serch for files on multiple directory levels? vasco-t Ubuntu 5 03-15-2007 03:51 PM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 01:46 AM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 08:16 PM
symlink multiple files to new directory with new extension jmanjohn61 Linux - General 1 01-06-2005 12:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:14 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration