rename multiple files recursively

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
linuxbox
Forum Newbie
Posts: 18
Joined: Fri Dec 12, 2003 5:16 pm

rename multiple files recursively

Post 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.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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.
Last edited by Pyrite on Fri Dec 12, 2003 5:44 pm, edited 1 time in total.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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]
Last edited by m3mn0n on Fri Dec 12, 2003 6:28 pm, edited 1 time in total.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

I'm sure you could write a bash script to do it easily. But chcase is easy too.
linuxbox
Forum Newbie
Posts: 18
Joined: Fri Dec 12, 2003 5:16 pm

thanks

Post 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.
linuxbox
Forum Newbie
Posts: 18
Joined: Fri Dec 12, 2003 5:16 pm

ok, here's a difficult one for you

Post 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!
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

How did you get it to work?
linuxbox
Forum Newbie
Posts: 18
Joined: Fri Dec 12, 2003 5:16 pm

i used

Post 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.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Ok.

Freshmeat.net

Search for multiple file editing programs..
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[php_man]filesystem[/php_man] functions combined with a [php_man]foreach[/php_man] loop will do the trick. :)
Post Reply