LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
  Search this Thread
Old 04-25-2008, 10:48 PM   #1
sajro
Member
 
Registered: Jan 2008
Location: Tennessee
Distribution: Arch, Debian, Ubuntu, Puppy
Posts: 39

Rep: Reputation: 15
Trying to batch rename some files in bash, no luck. Any ideas?


Hey LQ'ers,

I have some files that have a domain name appended to them (where they came from) in the names (and their MP3 ID3 tags) which I want to get rid of.

I've tried these commands to no avail:
Code:
sed 's/_-_www.thewebsite.com/""/g' *.mp3
ls *.mp3 | sed 's/_-_www.thewebsite.com/""/g'
ls *.mp3 | sed 's/_-_www.thewebsite.com/""/g' | mv *.mp3
I'm a newb at bash scripting and don't know these programs' more advanced features. Nor do I really understanding pipelining.

So, if you can think of anything, please tell me! Also, if anyone knows how to do something similar with the id3 tags, that would help a lot (id3 'name' attribute).

Dankeschön!
Sajro
 
Old 04-25-2008, 11:05 PM   #2
duryodhan
Senior Member
 
Registered: Oct 2006
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054

Rep: Reputation: 46
try

Quote:
#!/bin/bash
for file in $(ls *.mp3)
do
temp=${file#_-_www.thewebsite.com}

mv $file $temp

done
put it in a file and make it executable and run it in the folder containing the mp3s


Take a backup before you try this out ... It might rename it to something weird ... but I am pretty sure this will work ...
 
Old 04-25-2008, 11:17 PM   #3
sajro
Member
 
Registered: Jan 2008
Location: Tennessee
Distribution: Arch, Debian, Ubuntu, Puppy
Posts: 39

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by duryodhan View Post
try



put it in a file and make it executable and run it in the folder containing the mp3s


Take a backup before you try this out ... It might rename it to something weird ... but I am pretty sure this will work ...
Hmm...doesn't seem to work. Mv complains the target and source are the same file. Apparently, the variable "temp" becomes just the filename...

Thanks though!
 
Old 04-26-2008, 01:12 AM   #4
duryodhan
Senior Member
 
Registered: Oct 2006
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054

Rep: Reputation: 46
what is the output when you do
#!/bin/bash
for file in $(ls *.mp3)
do
echo $file
temp=${file#thewebsite}
echo $temp


done
 
Old 04-26-2008, 01:16 AM   #5
Pikidalto
Member
 
Registered: Apr 2008
Location: North Versailles, PA
Distribution: Ark Linux
Posts: 63

Rep: Reputation: 15
EDIT: My bad, didn't notice the "batch rename" part. This will work only for one file at a time. Again, my bad. You can ignore this post.

Here's what usually does the job for me:
Code:
mv /home/your_user/file_to_rename /home/your_user/target_file_name
This may seem a little unnecessary to say but many people fail to heed it anyway: be careful when renaming system files as root, you could ruin it that way.

Last edited by Pikidalto; 04-26-2008 at 01:18 AM.
 
Old 04-26-2008, 01:45 AM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
This uses the binary version of rename which is supplied with Mandriva etc. (There is a perl script version, supplied with e.g. Debian, which has a different syntax.)
It will overwrite existing files without warning.

Code:
rename "_-_www.thewebsite.com" "" *.mp3

Alternatively:-

Code:
for old in *.mp3;do
  new="$(echo $old | sed 's/_-_www.thewebsite.com//')"
  if [ -f "$new" ];then continue; fi # Don't overwrite existing files
  mv "$old" "$new"
done

Last edited by Kenhelm; 04-26-2008 at 02:01 AM.
 
Old 04-26-2008, 03:53 AM   #7
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 61
I usually check this article for answers to questions like that.
 
Old 04-26-2008, 04:06 AM   #8
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 61
Since you didn't show what a typical file name looks like, and the way you want it renamed, I'll show an example I had.
When downloading Linux updates with Linux in my laptop, I had to download to either the NTFS shared partition or my Fat32 USB pen drive. Firefox would append an extra file extension to the file's name when writing to a Windows file system like so: packagename.deb.deb, or packagename.rpm.rpm.
Using that article I came up with the commands below I issue from withing the folder containing all the updates on the pen drive to batch rename them with only one extension.
Code:
for i in *.rpm.rpm; do mv "$i" "`basename $i .rpm.rpm`.rpm"; done

for i in *.deb.deb; do mv "$i" "`basename $i .deb.deb`.deb"; done
 
Old 04-26-2008, 04:19 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
using "for file in $(ls *.mp3); do" is a pet peeve of mine.
You could just use:
for file in *.mp3; do
as was used in post 6.

If you have the rename command, that will work as well. It comes from the util-linux package so it is probably a linux specific command.

Code:
#!/bin/bash
for file in *_-_www.thewebsite.com*.mp3
do
temp=${file/_-_www.thewebsite.com/}

mv $file $temp

done
I'm not certain exactly where your _-_www.thewebsite.com pattern is in the filename. Posting a handful of real file names would be helpful. I assume that "thewebsite" part is variable so in a real script you could use:
temp=${file/_-_www.*.com/}

Code:
#!/bin/bash
for file in *_-_www.*.com*.mp3
do
temp=${file/_-_www.*.com/}

mv $file $temp

done
I changed the second line so that the pattern would match all www.<thewebsite>.com patterns. This will prevent false matches. A false match at this point will result in the oldname and the newname being the same.

There could still be a problem if some of the files are from a website with a .net or .biz domain.
Code:
#!/bin/bash
for domain in net biz tv com; do
    for file in *_-_www.*.${domain}*.mp3; do
        mv "$file" "${file/_-_www.*.${domain}/}"
    done
done
Whenever you use regular expressions, you need to be particular with recognizing that the patterns are. I think the reason that the previous attempt failed is that you used "thewebsite" in the post in place of a real website name but then used "thewebsite" literally in your script.
 
Old 04-26-2008, 10:27 AM   #10
sajro
Member
 
Registered: Jan 2008
Location: Tennessee
Distribution: Arch, Debian, Ubuntu, Puppy
Posts: 39

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
Posting a handful of real file names would be helpful.
I'll go ahead and stop the ambiguity. Here's the direct output of ls.
Code:
01-my_chemical_romance-the_end_-_www.file24ever.com.mp3
02-my_chemical_romance-dead_-_www.file24ever.com.mp3
03-my_chemical_romance-this_is_how_i_disappear_-_www.file24ever.com.mp3
04-my_chemical_romance-the_sharpest_lives_-_www.file24ever.com.mp3
05-my_chemical_romance-welcome_to_the_black_parade_-_www.file24ever.com.mp3
06-my_chemical_romance-i_dont_love_you_-_www.file24ever.com.mp3
07-my_chemical_romance-house_of_wolves_-_www.file24ever.com.mp3
08-my_chemical_romance-cancer_-_www.file24ever.com.mp3
09-my_chemical_romance-mama_-_www.file24ever.com.mp3
10-my_chemical_romance-sleep_-_www.file24ever.com.mp3
11-my_chemical_romance-teenagers_-_www.file24ever.com.mp3
12-my_chemical_romance-disenchanted_-_www.file24ever.com.mp3
13-my_chemical_romance-famous_last_words_-_www.file24ever.com.mp3
14-my_chemical_romance-hidden_track_-_www.file24ever.com.mp3
Yeah, it's an MCR album (The Black Parade) apparently from file24ever.com, but I found it on YouTorrent, before it sucked.
 
Old 04-26-2008, 11:12 AM   #11
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
For the second part of your question:-

Some linux distributions have EasyTag which can make various batch changes to ID3 tags using a GUI rather than from the command line. This includes "auto tagging": parse the filename and directory to automatically complete the ID3 fields.
 
Old 04-26-2008, 02:20 PM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
I created 3 files (using touch) with the same names. I would recommend preceding line with the "mv" command with echo. That way you can inspect the output without making changes.
Code:
for file in *.mp3; do
> echo mv "$file" "${file/_-_www.file24ever.com/}"
> done
mv 01-my_chemical_romance-the_end_-_www.file24ever.com.mp3 01-my_chemical_romance-the_end.mp3
mv 02-my_chemical_romance-dead_-_www.file24ever.com.mp3 02-my_chemical_romance-dead.mp3
mv 03-my_chemical_romance-this_is_how_i_disappear_-_www.file24ever.com.mp3 03-my_chemical_romance-this_is_how_i_disappear.mp3
All of your files used the server, so I didn't need a wild card.

The info bash manual has a section dealing with shell parameter expansion: 3.5.3 Shell Parameter Expansion.
This is were the forms like `${PARAMETER%WORD}' and `${PARAMETER/PATTERN/STRING}' are described.
 
Old 04-26-2008, 03:39 PM   #13
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 61
Ultimately the name of an mp3 should be like so: My Chemical Romance - The End.mp3
Notice the capital first letters, no underscore, and a space on each side of the dash between the name of the band and the name of the tune. It is recommended to use this format for naming music whether it is mp3 or wma or whatever, this is for display reasons while playing the tune, and for creating jewel case jackets properly.
Soooo.....were not done yet.

Last edited by Junior Hacker; 04-26-2008 at 07:37 PM.
 
Old 04-26-2008, 07:35 PM   #14
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Are you sure that you want to remove the file extension?

Here is a sed script that can convert your filenames to the pattern you want (but with the .mp3 extension). I got the last 7 lines from http://users.cybercity.dk/~bse26236/...d/CAPITALI.HTM

Code:
s/[[:digit:]]*-//
s/_-_www\..*\.com//
s/_/ /g
s/-/ - /g
s/^/ /
s/$/aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ/
:a
s/ \([a-z]\)\(.*\1\)\(.\)/ \3\2\3/
ta
s/.\{52\}$//
s/ //
Code:
> file=01-my_chemical_romance-the_end_-_www.file24ever.com.mp3
> echo $file | sed -f cap.sed
My Chemical Romance - The End.mp3
Code:
for file in [[:digit:]][[:digit:]]-*www.*.com.mp3; do
  mv "$file" "$(echo $file | sed -f cap.sed)"
done
If you didn't want the words capitalized, then you could use:
temp="${file#[[:digit:]][[:digit:]]-}"
temp="${temp_-_/www.*.com/}"
temp="${temp//_/ }"
newfilename="${temp//-/ - }"

Here is a bash capitalize function. I'm totally to blame for this:
Code:
#/bin/bash

# function to capitalize words
function capitalize() {
local firstletter capletter word sentence
for word in $*; do
    firstletter=${word:0:1}
    capletter=$(echo -n $firstletter | tr '[[:lower:]]' '[[:upper:]]')
    rest=${word#?}
#    echo -n "$capletter$rest "
    sentence=$sentence" "$capletter$rest
done
sentance=${sentence%% }
echo $sentence
}

# test of function

capitalize this is a test
capitalize this song is for you

Last edited by jschiwal; 04-26-2008 at 09:13 PM. Reason: normalize -> capitalize
 
Old 04-26-2008, 07:37 PM   #15
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 61
No!
Mistake that has been corrected. (The extension thinggy)

Last edited by Junior Hacker; 04-26-2008 at 07:39 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 02:26 PM
rename files using bash? ferradura Linux - General 4 09-14-2007 04:35 AM
rename several files in bash or python xeon123 Linux - Newbie 2 04-02-2007 10:08 AM
Bash - Batch File Rename Help... emailarron Linux - Newbie 4 01-26-2006 08:35 AM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 07:52 AM


All times are GMT -5. The time now is 09:32 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration