Forum Moderators: bakedjake

Message Too Old, No Replies

Want to "wildcard" delete files from sub-directories

Not rm -rf ...

         

shrirch

3:08 am on Aug 31, 2003 (gmt 0)

10+ Year Member



I have hundreds of php files lying around in hundreds of sub directories. How can I delete just the .php files? There are hundreds of other files .html etc which need to be preserved.

Any ideas?

marcs

3:19 am on Aug 31, 2003 (gmt 0)

10+ Year Member



There probably better way, this is what I usually do.

Say the directory is /www/htdocs/domain then do :

locate *.php¦grep /www/htdocs/domain

make sure that give you the list you need and not more (add a ¦more at the end if you have many, as you likely do). This should show you .php file in that directory and subdirectories

Once satisfied those are the files you want to remove :

rm `locate *.php¦grep /www/htdocs/domain`

This does require that the locate db is up to date. If not, run this as root first :

locate -u

There's probably easier ways and if so, someone will likely point it out.

MonkeeSage

3:46 am on Aug 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use a perl script:

#!/usr/bin/perl -w
use File::Find;

sub rm();

@directories = ("/home/dir1", "/home/dir2");

find(\&rm, @directories);

sub rm() {
my ($filename) = $_;
if ($filename =~ /\.php$/) {
unlink($File::Find::name);
# $File::Find::dir contains the current directory
# $File::Find::name contains the complete path name (dir + filename)
# $_ or $filename contains the filename alone.
}
}

Ps. This is a mod of a script I found a few weeks back on the DevShed forums, works for me, but I would recommend testing it on some fluff dirs first to make sure it doesn't go haywire and delete the wrong files or something.

seindal

10:04 am on Aug 31, 2003 (gmt 0)

10+ Year Member



find . -type f -name '*.php' print0 ¦ xargs -0 rm -f

Not tested, but its fairly straightforward.

panic

9:29 pm on Aug 31, 2003 (gmt 0)

10+ Year Member



Might want to try this :

su
(type root password)
cd /
find ¦grep *.php

Then, when you find directories with PHP files you want to delete, (like /home/user/php/, for example) :

cd /home/user/php/
rm -rf *.php

And wham... there you go. :)

-p

shrirch

9:20 am on Sep 9, 2003 (gmt 0)

10+ Year Member



Thanks folks!