Need some ideas for bulk renaming

1 view
Skip to first unread message

tuxsun1

unread,
Dec 21, 2009, 12:15:10 PM12/21/09
to linuxus...@googlegroups.com
I have always used Bash variable substitution chars (#, ##, %, %%) to
rename files when replacing the beginning or end of a filename.

Now I have a need to replace the middle portion of filenames from their
html code, '&', to 'and'.

e.g. list of filenames

Payables & Receivables
Sales & Marketing
Shipping & Receiving

Normally, I would use something like, for i in '*&amp*'; do mv "$i"
(this is where I'm stumped); done

TIA!


chutsu

unread,
Dec 21, 2009, 12:43:12 PM12/21/09
to Linux Users Group
Well I think the follow code will help you

for i in *;do mv "$i" "${i/\&amp/} done;

-So basically the for loop runs through every file in that particular
directory.
-Uses the "mv" command to rename files, the second statement is just
search and replace
-The "/\&amp/" means I want to search the term "&amp" and replace it
with "something".
-NOTE: the "\" infront of the "&", because you need to escape special
symbols such as "%" "-" etc ...
-After the second "/" in the search and replace term you can put
whatever you want...
eg.) if I wanted to replace "&amp" with "HELLO" the move command will
be:

mv "$i" "${i/\&amp/HELLO}"

Hope this helps :)
Chris

tuxsun1

unread,
Dec 21, 2009, 1:06:58 PM12/21/09
to linuxus...@googlegroups.com

Ahhh!! I'm guessing the i/ / format is a regex insert command?
I'll test it with the echo command before I use mv.

Thanks, chutsu!

tuxsun1

unread,
Dec 21, 2009, 1:08:53 PM12/21/09
to linuxus...@googlegroups.com

I may have replied too hastily. The 'i' is the original variable your
using a regex substitution form, is that it?

chutsu

unread,
Dec 21, 2009, 1:12:33 PM12/21/09
to Linux Users Group
I think so... lol.. am not too sure what you mean, but the "i" just
means that your taking what ever filename the for loop is currently
handling, and doing a search and replace on variable "i".

chutsu

unread,
Dec 21, 2009, 1:15:18 PM12/21/09
to Linux Users Group
Because if you remember, at the start of the for loop:

for $i in *

which I declared a bash variable called "i", and what ever it finds
(filenames), the filename will be assigned to "i", then later on I
used "i/search/replace" to say in variable "i" search and replace....

Hope my explanations where clear!

tuxsun1

unread,
Dec 21, 2009, 1:19:20 PM12/21/09
to linuxus...@googlegroups.com

Got it with echo, so mv is next. TYVM!!

I use this substitution form in Vim all the time, just never occurred to
me to use it in the commandline like that.

for i in "*\&amp*"; do echo "${i/\&/and/}"; done

Curiously, there's a forgiving anomaly. The 1st time I left off the last
trailing slash and it worked.

Thanks again, chutsu!

tuxsun1

unread,
Dec 21, 2009, 5:29:08 PM12/21/09
to linuxus...@googlegroups.com
chutsu,

Yes, you were clear. I wasn't.... My sentence was missing a word, so it
didn't make sense to you.

As for the anomaly of the trailing slash, I turned out to be wrong. Your
syntax of leaving off the trailing slash is the only syntax that worked
for me. The substitution syntax of
/<search-string>/<replacement-string/ as used by sed and vim gave errors
until I left off the trailing slash.

For anyone else that needs to rename part of a filename, here's an
example that worked for me--thanks to chutsu's suggestions and help:

#"for i in *\&amp*" limits the list of filenames passed to the mv
command to ONLY filenames containing '&amp'.
#
#mv "$i" "${i/\&amp;/and}" replaces '&amp;' with 'and' in each filename
passed to it.

for i in *\&amp*; do mv "$i" "${i/\&amp;/and}"; done

------------------------------

JTF

unread,
Dec 22, 2009, 8:20:29 AM12/22/09
to Linux Users Group
look into PyRenamer. It is a python project and it works well.

tuxsun1

unread,
Dec 22, 2009, 4:41:02 PM12/22/09
to linuxus...@googlegroups.com

ONLY the first match is replaced; subsequent matches are ignored.

Where can I find documentation on the search criteria you're using in
the shell?

Thanks again!

tuxsun1

unread,
Dec 22, 2009, 4:41:56 PM12/22/09
to linuxus...@googlegroups.com

I'll do that. Thanks, JTF!
Reply all
Reply to author
Forward
0 new messages