MandrivaUsers.org : bulk rename bash script - MandrivaUsers.org

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

bulk rename bash script can someone please help me with this?

#1 User is offline   frozen 

  • New Here
  • Group: Members
  • Posts: 26
  • Joined: 17-May 05

Posted 17 April 2006 - 08:27 PM

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?

This post has been edited by frozen: 17 April 2006 - 08:29 PM

0

#2 User is offline   Steve Scrimpshire 

  • This is a dark ride
  • Group: OTW
  • Posts: 3,590
  • Joined: 13-December 02

Posted 18 April 2006 - 03:49 AM

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

This post has been edited by Steve Scrimpshire: 18 April 2006 - 03:59 AM

Posted Image
0

#3 User is offline   neddie 

  • Moo
  • Group: Members
  • Posts: 2,068
  • Joined: 28-April 05

Posted 18 April 2006 - 07:52 AM

or
rename "%%20" " " *

Dell 510m, Mandriva 2009.0, KDE 3
Shuttle K48, Debian Lenny, Gnome
linux.activityworkshop.net
0

#4 User is offline   frozen 

  • New Here
  • Group: Members
  • Posts: 26
  • Joined: 17-May 05

Posted 18 April 2006 - 08:21 AM

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..
0

#5 User is offline   mandri 

  • New Here
  • Group: OTW
  • Posts: 26
  • Joined: 04-March 07

Posted 19 June 2008 - 12:01 AM

View Postneddie, on Apr 18 2006, 07:52 AM, said:

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?

This post has been edited by mandri: 19 June 2008 - 01:00 AM

0

#6 User is offline   Steve Scrimpshire 

  • This is a dark ride
  • Group: OTW
  • Posts: 3,590
  • Joined: 13-December 02

Posted 19 June 2008 - 02:36 AM

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
Posted Image
0

#7 User is offline   ramfree17 

  • Pinoy Newbie Home/Philippines
  • View blog
  • Group: Members
  • Posts: 1,936
  • Joined: 08-September 02

Posted 19 June 2008 - 04:56 AM

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!
You have read a post from a newbie. Take everything with a grain of salt.

"I am a newbie forever, I wont learn a damn thing if I am not." - pilfered from an email sig

Registered Linux User #246176
http://del.icio.us/ramfree17
Link to Frequently Asked Questions
When you absolutely have nothing else better to do...
and its always paul's fault. sometimes tyme's too.
0

#8 User is offline   neddie 

  • Moo
  • Group: Members
  • Posts: 2,068
  • Joined: 28-April 05

Posted 19 June 2008 - 06:01 AM

or krename?
Dell 510m, Mandriva 2009.0, KDE 3
Shuttle K48, Debian Lenny, Gnome
linux.activityworkshop.net
0

#9 User is offline   mandri 

  • New Here
  • Group: OTW
  • Posts: 26
  • Joined: 04-March 07

Posted 19 June 2008 - 10:50 AM

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.

This post has been edited by mandri: 19 June 2008 - 11:04 AM

0

#10 User is offline   Steve Scrimpshire 

  • This is a dark ride
  • Group: OTW
  • Posts: 3,590
  • Joined: 13-December 02

Posted 20 June 2008 - 01:53 AM

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...erl/regexp.html

This post has been edited by Steve Scrimpshire: 20 June 2008 - 01:57 AM

Posted Image
0

#11 User is offline   ramfree17 

  • Pinoy Newbie Home/Philippines
  • View blog
  • Group: Members
  • Posts: 1,936
  • Joined: 08-September 02

Posted 20 June 2008 - 04:20 AM

View PostSteve Scrimpshire, on Jun 20 2008, 01:53 AM, said:

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!
You have read a post from a newbie. Take everything with a grain of salt.

"I am a newbie forever, I wont learn a damn thing if I am not." - pilfered from an email sig

Registered Linux User #246176
http://del.icio.us/ramfree17
Link to Frequently Asked Questions
When you absolutely have nothing else better to do...
and its always paul's fault. sometimes tyme's too.
0

#12 User is offline   mandri 

  • New Here
  • Group: OTW
  • Posts: 26
  • Joined: 04-March 07

Posted 20 June 2008 - 10:26 AM

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.
0

#13 User is offline   Steve Scrimpshire 

  • This is a dark ride
  • Group: OTW
  • Posts: 3,590
  • Joined: 13-December 02

Posted 21 June 2008 - 02:25 AM

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' *

This post has been edited by Steve Scrimpshire: 22 June 2008 - 03:30 AM

Posted Image
0

#14 User is offline   mandri 

  • New Here
  • Group: OTW
  • Posts: 26
  • Joined: 04-March 07

Posted 22 June 2008 - 01:38 AM

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

0

#15 User is offline   Steve Scrimpshire 

  • This is a dark ride
  • Group: OTW
  • Posts: 3,590
  • Joined: 13-December 02

Posted 22 June 2008 - 03:31 AM

Sorry about that. I lost a closing }. Add } on the very last line...you'll see I edited my post above to fix it.
Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users