Help - Search - Members - Calendar
Full Version: bulk rename bash script
MandrivaUsers.org > Advanced Topics > Command Line, Kernel and Programming
frozen
i need a bash script to bulk rename a bunch of files containing %%20 in their filename with the space character " ".

this %%20 is generated when downloading files with wget that contain spaces..

can someone please help me?
Steve Scrimpshire
Put all the files in one directory and run this command

find . -name "*%%20*" -exec sh -c 'mv ${0} "${0//\%\%20/ }"' {} \;

P.S. I don't have this problem with wget:
CODE
< omar ~/rename/wget > wget "http://semperphi.com/res test"
--22:55:07--  [url=http://semperphi.com/res%20test]http://semperphi.com/res%20test[/url]
           => `res test'
Resolving semperphi.com... 66.98.142.2
Connecting to semperphi.com|66.98.142.2|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 0 [text/plain]

    [ <=>                                 ] 0             --.--K/s            

22:55:07 (0.00 B/s) - `res test' saved [0/0]

< omar ~/rename/wget > ls
res test
neddie
or
CODE
rename "%%20" " " *
frozen
thank you Steve.

Works like a charm on my Mandriva Box. However I also need to issue this command on a FreeBSD and 'find' there views files with just one percentage sign: %20 instead of %%20.

Can you please adjust the command above for that environment too. smile.gif

neddle: unfortunately, there is no rename command available on the FreeBSD box I'm using..
mandri
QUOTE (neddie @ Apr 18 2006, 07:52 AM) *
or
CODE
rename "%%20" " " *

Could you explain what the " " * after the "%%20" does?



I have been playing with this some with some success.
I am only guessing here,and would be grateful is someone corrects me if I'm wrong.

I am guessing that in this part,"%%20",what is inside the first set of quotes is what is to be renamed.

Then in this part " " *,what is inside the second set of quotes is what it is to be renamed to.

Since there is nothing inside the quotes in the second part,it will be renamed nothing,which in turn is actually just removing the first part.
The * is saying to do this for all the files found to contain %%20 in the directory this command was entered in.

Am I right?
Steve Scrimpshire
mandri,

That's pretty close to being exactly correct. The * is telling it to do this for all files in the directory. The "%%20" is the string to replace and " " is what to replace it with. " " is not exactly nothing, though. It's a space. The command is renaming all files that contain %%20, by using what they're named already, but replacing %%20 with a space.

like%%20this

becomes

like this

and%%20like%%20this

becomes

and like this
ramfree17
did a quick search for a gui for renaming and found the metamorphose project. the gui seems steep as far as learning curve goes but it might fit for those looking for a gui-based solution.

ciao!
neddie
or krename?
mandri
Thanks for replying.
I did miss the fact that a space was being substituted.
I tried putting the "'s together like this "" instead of this " ",and that gave unwanted results.

I still can't figure out a few things now.
For experimenting,I created 3 directories named
01-test_rename_a
02-test_rename_b
03-test_rename_c

I used this
rename "-test_" " " *

That removed what I wanted OK.
What I can't figure out is how to remove 01,02,03.
How do you use a wildcard here to remove these numbers?

Also,if I use this,just for experimenting purposes,
rename "_" " " *
it only removes the first instance of _.
How can I get it to remove all instances of _?

Example.Change this,
01 remove_all_underscores_1
02 remove_all_underscores_2
03 remove_all_underscores_3

to this?
remove all underscores 1
remove all underscores 2
remove all underscores 3

Do you know of a webpage that gives a lot of examples for the rename command?
man rename in terminal didn't tell me much at all.
Steve Scrimpshire
There aren't a lot of examples, but I found one:
http://tips.webdesign10.com/how-to-bulk-re...in-the-terminal

Remove all underscores:
rename -v 's/_/ /g' *.JPG


Remove the numbers (and the dash):
01-test_rename_a
02-test_rename_b
03-test_rename_c

rename -v 's/[0-9]+-//g' *

If you want to remove the test_ part, too:

rename -v 's/[0-9]+-test_//g' *

I've been studying regexes for years and I still have a very limited understanding of them:
http://www.cs.tut.fi/~jkorpela/perl/regexp.html
ramfree17
QUOTE (Steve Scrimpshire @ Jun 20 2008, 01:53 AM) *
I've been studying regexes for years and I still have a very limited understanding of them:


+1. well i am studying them on and off depending on what i need but it is not uncommon that i bang my head on the table whenever i try something simple and it does not work. having different kind of regex engines does not help, as well as windows and *nix quoting conventions. wall.gif

ciao!
mandri
QUOTE
rename -v 's/[0-9]+-//g' *

This isn't working for me?
I have perl base installed so I'm not sure why it isn't working.
Does the command work for you?
If it works for you,then I'll have to investigate what I may be missing related to perl.

I followed the link and it is better than most I have found.

QUOTE
not uncommon that i bang my head

I have been researching how to do this one task using rename for several days now.
This has proved most difficult.
Steve Scrimpshire
Ack. It's not the same command any more. You can copy this text to a file:

CODE
#!/usr/bin/perl -w
# rename - Larry's filename fixer
$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
}


And name it rename.pl, put it in your /usr/bin directory and chmod +x /usr/bin/rename.pl
Then you can do:
rename.pl 's/[0-9]+-//g' *
mandri
I tried and got this?
CODE
$ rename.pl 's/[0-9]+-//g' *
/usr/bin/rename.pl: line 4: =: command not found
/usr/bin/rename.pl: line 5: syntax error near unexpected token `@ARGV'
/usr/bin/rename.pl: line 5: `chomp(@ARGV = <STDIN>) unless @ARGV;'
$
Steve Scrimpshire
Sorry about that. I lost a closing }. Add } on the very last line...you'll see I edited my post above to fix it.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.