How can I change the prefix of multiple files in a directory
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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!
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
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.
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.
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"
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
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
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...
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.