Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find and Replace in file names.

2 views
Skip to first unread message

Andy J.

unread,
Aug 15, 2006, 2:50:57 PM8/15/06
to
I have a directory (/home/andrew/mp3) containing hundreds of files
which, thanks to Grip, have 'underscores' instead of 'spaces'. I
would like to replace the underscores with spaces but, as you can
appreciate, this would be a big job by hand.

I'm trying to get to grips with regular expressions but I'm struggling.
I don't want it handed to me on a plate but I wouldn't mind some
beginners guidance.

Am I right in thinking of using 'awk'? I can see how it works on the
contents of a file but not on the file-name itself. Is there another
easier way of changing file-names en-masse?

Thanks in advance,
Andy J.

Dan Espen

unread,
Aug 15, 2006, 3:04:42 PM8/15/06
to
"Andy J." <and...@pcy.net> writes:

Here's a rule I use.

Everytime you think awk is the right tool,
use Perl instead.

Something like this doesn't require anything so complicated.

For example, this is untested, but probably pretty close
to working:

cd mp3
for i in */*/* ; do
newname=`echo $i | tr '_' ' '`
mv "$i" "$newname"
done


but what I really wanted to tell you is leave the underscores
in there. If you remove them, you'll regret it.

Andy J.

unread,
Aug 15, 2006, 3:18:43 PM8/15/06
to


Cheers Dan,

I'll try to interpret that first. As I said I want to try and
understand what I'm doing and how it works. I've got a book on
Perl here somewhere, best I get down to some serious reading.

I see where you're coming from with the "leave the underscores"
but I've done some files in the past (and shared them with
Windows-using friends) and there's been no problems so far with
the spaces. I'll bear it in mind though, after all if I can learn
how to change underscores to spaces then the reverse should be
quite simple too....:-)

Andy J.

Wes Newell

unread,
Aug 15, 2006, 3:38:38 PM8/15/06
to
On Tue, 15 Aug 2006 19:50:57 +0100, Andy J. wrote:

> Am I right in thinking of using 'awk'? I can see how it works on the
> contents of a file but not on the file-name itself. Is there another
> easier way of changing file-names en-masse?
>

[wes@wes2 mp3]$ cd noartist/unknown_disc/
01_choir_01.mp3 08_choir_08.mp3 How_marvelous.mp3
02_choir_02.mp3 09_choir_09.mp3 I_call_you_holy.mp3
03_choir_03.mp3 10_choir_10.mp3 Let_there_be_glory.mp3
04_choir_04.mp3 All_I_Need.mp3 Perpetual_Praise.mp3
05_choir_05.mp3 He's_good.mp3 Shout.mp3
06_choir_06.mp3 How_great.mp3 When_We're_Together.mp3
07_choir_07.mp3 HowGreat.mp3
[wes@wes2 mp3]$ cd noartist/unknown_disc/
[wes@wes2 unknown_disc]$ rename _ " " *.mp3
[wes@wes2 unknown_disc]$ ls
01 choir_01.mp3 06 choir_06.mp3 All I_Need.mp3 I call_you_holy.mp3
02 choir_02.mp3 07 choir_07.mp3 He's good.mp3 Let there_be_glory.mp3
03 choir_03.mp3 08 choir_08.mp3 How great.mp3 Perpetual Praise.mp3
04 choir_04.mp3 09 choir_09.mp3 HowGreat.mp3 Shout.mp3
05 choir_05.mp3 10 choir_10.mp3 How marvelous.mp3 When We're_Together.mp3
[wes@wes2 unknown_disc]$ rename _ " " *.mp3
[wes@wes2 unknown_disc]$ ls
01 choir 01.mp3 06 choir 06.mp3 All I Need.mp3 I call you_holy.mp3
02 choir 02.mp3 07 choir 07.mp3 He's good.mp3 Let there be_glory.mp3
03 choir 03.mp3 08 choir 08.mp3 How great.mp3 Perpetual Praise.mp3
04 choir 04.mp3 09 choir 09.mp3 HowGreat.mp3 Shout.mp3
05 choir 05.mp3 10 choir 10.mp3 How marvelous.mp3 When We're Together.mp3
[wes@wes2 unknown_disc]$


--
Want the ultimate in free OTA SD/HDTV Recorder? http://mythtv.org
http://mysettopbox.tv/knoppmyth.html Usenet alt.video.ptv.mythtv
My server http://wesnewell.no-ip.com/cpu.php
HD Tivo S3 compared http://wesnewell.no-ip.com/mythtivo.htm

Dan Espen

unread,
Aug 15, 2006, 3:44:07 PM8/15/06
to
"Andy J." <and...@pcy.net> writes:

Don't thank me yet.
I see that 'mv' won't create directories that
don't exist so you'll probably have to parse the target name,
move the directories then move the file.

Sounds like a job for perl to me.

ArameFarpado

unread,
Aug 15, 2006, 3:47:45 PM8/15/06
to
Andy J. wrote:

If you don't wanna do things in konsole, install and try "krename".

Regards
ArameFarpado

Andy J.

unread,
Aug 15, 2006, 4:35:56 PM8/15/06
to

Thanks for the pointer but I'd really like to get to grips with stuff
like awk, grep and regular expressions. It seems that avoiding these
sort of tools means that I may as well stayed with Windows. After all,
it's the command line (DOS) power that I missed in the later M$
products and Linux brings it all back (with steroids added).

Andy J.

Dan Espen

unread,
Aug 15, 2006, 4:54:35 PM8/15/06
to
Dan Espen <dan...@MORE.mk.SPAMtelcordia.com> writes:

Actually, this seems to work and is pretty simple:

for i in * ; do


newname=`echo $i | tr '_' ' '`

if [ "$i" != "$newname" ] ; then
echo mv "$i" "$newname"
mv "$i" "$newname"
fi
done
for i in */* ; do


newname=`echo $i | tr '_' ' '`

if [ "$i" != "$newname" ] ; then
echo mv "$i" "$newname"
mv "$i" "$newname"
fi
done


for i in */*/* ; do
newname=`echo $i | tr '_' ' '`

if [ "$i" != "$newname" ] ; then
echo mv "$i" "$newname"
mv "$i" "$newname"
fi
done


Test with test data, or make backups.

Chris F.A. Johnson

unread,
Aug 15, 2006, 5:00:21 PM8/15/06
to
On 2006-08-15, Andy J. wrote:
> I have a directory (/home/andrew/mp3) containing hundreds of files
> which, thanks to Grip, have 'underscores' instead of 'spaces'.

If you are misguided enough to want spaces instead, you can tell
grip not to replace spaces with uynderscores.

> I would like to replace the underscores with spaces but, as you can
> appreciate, this would be a big job by hand.

And something I would recommend NOT doing.

> I'm trying to get to grips with regular expressions but I'm struggling.
> I don't want it handed to me on a plate but I wouldn't mind some
> beginners guidance.
>
> Am I right in thinking of using 'awk'? I can see how it works on the
> contents of a file but not on the file-name itself. Is there another
> easier way of changing file-names en-masse?

There is a rename command; see the man page for details.

The renaming can be done with sed or awk, but bash also has
search-and-replace parameter expansion which is much faster:

$ var=qwertyqwerty
$ echo "${var//e/E}"
qwErtyqwErty

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence

Wes Newell

unread,
Aug 16, 2006, 12:30:25 AM8/16/06
to

You guys really should read man rename. All that's needed is a simple
rename command. rename _ " " *.mp3 will replace the first underscore of
any *.mp3 file with a space. if you've got filenames with 4 undescores in
it, run the command 4 times. About 10 seconds of your time.:-)

Dan Espen

unread,
Aug 16, 2006, 8:45:58 AM8/16/06
to
Wes Newell <w.ne...@TAKEOUTverizon.net> writes:

The OP is working with a directory structure created by grip.
There are underscores in the artist directory, the title directory under the
artist directory and the track files.

Rename will work if run like this:

cd mp3
rename '_' ' ' *
(repeat some number of times until
ls *_* returns nothing)
rename '_' ' ' */*
(repeat some number of times until
ls */*_* returns nothing)
rename '_' ' ' */*/*
(repeat some number of times until
ls */*/*_* returns nothing)

Of course if the OP continues to use grip
the process needs to be repeated.
I think running the above script would be
a bit easier.

Wes Newell

unread,
Aug 16, 2006, 9:49:35 AM8/16/06
to
On Wed, 16 Aug 2006 08:45:58 -0400, Dan Espen wrote:

> Wes Newell <w.ne...@TAKEOUTverizon.net> writes:
>
>>>>>> "Andy J." <and...@pcy.net> writes:
>>>>>>
>>>>>>> I have a directory (/home/andrew/mp3) containing hundreds of files
>>>>>>> which, thanks to Grip, have 'underscores' instead of 'spaces'. I
>>>>>>> would like to replace the underscores with spaces but, as you can
>>>>>>> appreciate, this would be a big job by hand.

>> You guys really should read man rename. All that's needed is a simple
>> rename command. rename _ " " *.mp3 will replace the first underscore of
>> any *.mp3 file with a space. if you've got filenames with 4 undescores in
>> it, run the command 4 times. About 10 seconds of your time.:-)
>
> The OP is working with a directory structure created by grip.

The directory structure created by grip is the same as any other.

> There are underscores in the artist directory, the title directory under
> the artist directory and the track files.
>

From what he wrote (above), there are underscores in the actual file names
and that's what he wants to change. He didn't mention changing directory
names, which is really quiet simple anyway,

> Rename will work if run like this:
>

Rename will work as described in the man page and as I described earlier.
What you are describing is the exact same thing except you have added
different target directories. From his post he only has one directory. I
answered his post precisely and correctly from the information given. You
seem to want to make a mountain out of a mole hill. It's simply a matter
of putting the target directory in the rename command, repeat the command
by using the up arrow key several times and be done with it. Still a
matter of just a few seconds.

> Of course if the OP continues to use grip the process needs to be
> repeated.

If he doesn't want underscores created by grip, maybe he configure it so
it yses spaces instead.

>I think running the above script would be a bit easier.

Everyone's entitled to their opinion. I just can't see writing a script
when there's already a straight forward command to do the same thing.

Philippe Martin

unread,
Aug 16, 2006, 5:15:44 PM8/16/06
to
Andy J. wrote:

Hi,

I'm including a python script I wrote to get rid of weird characters in my
mp3 file names ... you can easily modify it for your purpose.

Philippe

import sys
import os
import traceback

def lister(dummy, dirname, filesindir):
for l in filesindir:
lr1 = dirname + '/'+l
lr2 = dirname + '/'+l.upper()

for i in lr2:
if ord(i) not in range (32,127):
print lr2 + ' :',
lr2 = raw_input()
lr2 = dirname + '/' + lr2
if lr1 != lr2:
print lr1 + ' ===> ' + lr2
try: #if dest file exists already
os.remove(lr2)
except:
#traceback.print_exc()
pass
try:
lr1 = '\"' + lr1 + '\"'
lr2 = '\"' + lr2 + '\"'
print 'RENAMING ' + lr1 + ' TO ' + lr2
os.rename(lr1,lr2)
break
except:
traceback.print_exc()
pass

os.path.walk('/home/philippe/mp3',lister, None)

0 new messages