frozen Posted April 17, 2006 Report Share Posted April 17, 2006 (edited) 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 April 17, 2006 by frozen Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted April 18, 2006 Report Share Posted April 18, 2006 (edited) 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 April 18, 2006 by Steve Scrimpshire Quote Link to comment Share on other sites More sharing options...
neddie Posted April 18, 2006 Report Share Posted April 18, 2006 or rename "%%20" " " * Quote Link to comment Share on other sites More sharing options...
frozen Posted April 18, 2006 Author Report Share Posted April 18, 2006 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.. Quote Link to comment Share on other sites More sharing options...
mandri Posted June 19, 2008 Report Share Posted June 19, 2008 (edited) 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 June 19, 2008 by mandri Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted June 19, 2008 Report Share Posted June 19, 2008 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 Quote Link to comment Share on other sites More sharing options...
ramfree17 Posted June 19, 2008 Report Share Posted June 19, 2008 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! Quote Link to comment Share on other sites More sharing options...
neddie Posted June 19, 2008 Report Share Posted June 19, 2008 or krename? Quote Link to comment Share on other sites More sharing options...
mandri Posted June 19, 2008 Report Share Posted June 19, 2008 (edited) 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 June 19, 2008 by mandri Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted June 20, 2008 Report Share Posted June 20, 2008 (edited) 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 June 20, 2008 by Steve Scrimpshire Quote Link to comment Share on other sites More sharing options...
ramfree17 Posted June 20, 2008 Report Share Posted June 20, 2008 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. ciao! Quote Link to comment Share on other sites More sharing options...
mandri Posted June 20, 2008 Report Share Posted June 20, 2008 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. Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted June 21, 2008 Report Share Posted June 21, 2008 (edited) 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 June 22, 2008 by Steve Scrimpshire Quote Link to comment Share on other sites More sharing options...
mandri Posted June 22, 2008 Report Share Posted June 22, 2008 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;' $ Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted June 22, 2008 Report Share Posted June 22, 2008 Sorry about that. I lost a closing }. Add } on the very last line...you'll see I edited my post above to fix it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.