Jump to content
frozen

bulk rename bash script

Recommended Posts

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?

Edited by frozen

Share this post


Link to post
Share on other sites

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:

< 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

Edited by Steve Scrimpshire

Share this post


Link to post
Share on other sites

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. :)

 

neddle: unfortunately, there is no rename command available on the FreeBSD box I'm using..

Share this post


Link to post
Share on other sites
or
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?

Edited by mandri

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Edited by mandri

Share this post


Link to post
Share on other sites

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

Edited by Steve Scrimpshire

Share this post


Link to post
Share on other sites
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:

 

ciao!

Share this post


Link to post
Share on other sites
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.

 

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.

Share this post


Link to post
Share on other sites

Ack. It's not the same command any more. You can copy this text to a file:

 

#!/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' *

Edited by Steve Scrimpshire

Share this post


Link to post
Share on other sites

I tried and got this?

$ 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;'
$

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...