PDA

View Full Version : [all variants] shell command to replace "%20" in filenames by "_" (underscore)



jaebe2
July 20th, 2009, 01:48 PM
Hi everyone,

I have these names of files with the %20 in between (I think they represent spaces in windows).
Anyway I find them really hard to read efficiently so I was trying to replace the "%20" with underscores "_".

I noticed that I can effectively 'seperate' the troublesome filename by using this wildcard combo: eg. mv *%20* ??????
But I don't know how to replace them with underscores.

Thanks,

beren.olvar
July 20th, 2009, 02:57 PM
I think something like

for a in *%20*; do
b=$(echo $a|awk '{ gsub("%20", "_"); print }');
#mv "$a" "$b";
cp "$a" "$b";
done

does the trick...
of course there are more elegant ways... but this should work
cheers!

EDIT: Although I tested that in some circumstances it could cause problems... you are better off using cp instead of mv, and deleting the files by hand... or moving once you check everything is ok

ajgreeny
July 20th, 2009, 02:59 PM
Navigate to the folder where the files are and use
rename -n 's/%20/_/g' * which will do it. To replace any other characters replace the %20, obviously, and if you want to replace spaces that are actually in the names replace the %20 with \space, ie, backslash followed by a space (difficult to write that).
rename 's/\ /_/g' *

If you want a gui you can use gprename as well.

l-x-l
July 20th, 2009, 03:42 PM
Navigate to the folder where the files are and use
rename -n 's/%20/_/g' * which will do it. To replace any other characters replace the %20, obviously, and if you want to replace spaces that are actually in the names replace the %20 with \space, ie, backslash followed by a space (difficult to write that).
rename 's/\ /_/g' *

If you want a gui you can use gprename as well.

God Bless you. You have no idea how helpful this is for me. This will save me enormous time.

l-x-l
July 20th, 2009, 04:05 PM
Only other question I have is it possible to rename a bunch of files in a bunch of subdirectories recursively?

For example, I have a mp3s in my Music folder each artist has their own sub-folder. If I want to rename all of my mp3s in my Music folder & its subdirectories using

rename 's/%20/_/g' *.mp3

Is it possible to do that?

ajgreeny
July 20th, 2009, 06:16 PM
I haven't actually got a clue about that, but it's worth a try with the option -r or -R after the rename command, but backup first, just in case.

No, I just tried it, and the -r or -R is not recognised as a regular expression by perl. Sorry.

unutbu
July 20th, 2009, 06:27 PM
I think this should work:

find ~/Music -name '*.mp3' -exec rename 's/%20/_/g' "{}" +

l-x-l
July 20th, 2009, 07:56 PM
I think this should work:

find ~/Music -name '*.mp3' -exec rename 's/%20/_/g' "{}" +

Thx. That works.

l-x-l
July 20th, 2009, 08:17 PM
I think this should work:

find ~/Music -name '*.mp3' -exec rename 's/%20/_/g' "{}" +

Can anyone tell me what the "{}" & "+" at the end of the code does? Also, instead of doing -exec, could I have piped instead?

slakkie
July 20th, 2009, 08:23 PM
Can anyone tell me what the "{}" & "+" at the end of the code does? Also, instead of doing -exec, could I have piped instead?



the {} is the stuff that find found.

Don't know what the + is, I usually do \; at the end, to end the find exec. Think/assume the + does the same.

And you could have piped it:

find /stuff -name stuff | xargs rename ...

unutbu
July 20th, 2009, 08:48 PM
find ... -exec rename ... "{}" +

The plus-sign tells "find" to collect together many filenames before feeding them all to the "rename" command. This reduces the number of times the "rename" command gets called, which is a good thing since in general this will reduce execution time.

See the "find" man page for the official explanation.

jaebe2
July 21st, 2009, 12:12 PM
Thanks everyone for replying.

It works great.

I'll mark it as solved.