Page 1 of 1

rename multiple files recursively

Posted: Fri Dec 12, 2003 5:16 pm
by linuxbox
Hello. my issue is having hundreds of html files i need to rename with .php file extentions. .html to .php. my problem:
i have for instance, /news directory on my linux box. that dir has subdir's like jan, feb, mar..etc etc..and in those directories are more directories with numbers like 01, 02, etc etc. these directories have .html files that need to have their ext changed to php. how can i change all of these files to .php extentions. i would like to start at the base directory say the "march" directory and traverse each dir beneath it renaming all html files to .php.
is this possible?
thank you. i've searched for how to do this but have not found.

Posted: Fri Dec 12, 2003 5:33 pm
by Pyrite
It is possible of course, and I have a script somewhere to do it. I'll get back to you on it.

Btw: there's a Linux forum now for these kinds of questions.

Posted: Fri Dec 12, 2003 5:38 pm
by Pyrite
K, gotta get this nifty perl script.

http://www.blemished.net/chcase.html

Then you can do stuff like:

Code: Select all

$ chcase -o -x 's/htm/php/' '*.php' RET

Posted: Fri Dec 12, 2003 6:23 pm
by m3mn0n
I remember reading about a linux command that does this, I don't know where I put that printed documentation though. :wink:

Check out:
http://linux.tucows.com/preview/8622.html

And:
Google: [google]rename multiple files recursively[/google]

Posted: Fri Dec 12, 2003 6:25 pm
by Pyrite
I'm sure you could write a bash script to do it easily. But chcase is easy too.

thanks

Posted: Sat Dec 13, 2003 9:29 am
by linuxbox
i'll check these links out and see what i can come up with. i just wasn't sure if php could do this or if i would have to use a command within linux itself.

ok, here's a difficult one for you

Posted: Sat Dec 13, 2003 10:28 am
by linuxbox
i have successfully changed the extention of all of the html files to .php. Now i must insert a few lines of code in each of the php files at the very top. can a php script open all files ending in .php in a directory and insert some code at the very top of the page?
like at the top of all of my php files i need something like:

Code: Select all

<?php
require('somefile.php');
//some conditional statements;
?>
this is probably advanced but it's gonna be a headache to edit each file manually :)

thanks very much!

Posted: Sat Dec 13, 2003 3:12 pm
by m3mn0n
How did you get it to work?

i used

Posted: Sat Dec 13, 2003 3:18 pm
by linuxbox
chcase:
i renamed chcase to rename.cgi and did this:

./rename.cgi -r -x 's/.html/.php/' '*.html'

that recursively changed .html in every .html file to .php.
worked like a charm. now i need something that will open all of the php files and place a strand of code at the top of each php page.

Posted: Sun Dec 14, 2003 2:03 am
by Pyrite
Ok.

Freshmeat.net

Search for multiple file editing programs..

Posted: Sun Dec 14, 2003 2:44 am
by m3mn0n
[php_man]filesystem[/php_man] functions combined with a [php_man]foreach[/php_man] loop will do the trick. :)