how can I seriously MASS delete .cnf files from a server....

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
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

how can I seriously MASS delete .cnf files from a server....

Post by Deseree »

A friend's system uses flat file caching and the extention type is .txt.cnf , and we are trying to figure out how to set a php script to the

/home/

directory and just blast / delete all those .txt.cnf files' away. ( there are ten-hundreds of thousands of small files of them, not a very good system he had but I'm trying to clean up his mess ).

ty
User avatar
tasteslikepurple
Forum Commoner
Posts: 46
Joined: Thu Jan 26, 2006 3:38 am
Location: Bath, UK

Post by tasteslikepurple »

this bit of code should do it :) (note: I haven't tested it, if it doesn't work come back and shout at me and either me or another nice person will fix it)

Code: Select all

function customDelete ($dir, $pattern) 
{
  if(is_dir($dir))
  {
    if ($handle=opendir($dir))
    {
      while($file=readdir($handle)) 
      { 
        if($file!="." && $file!=".." && strpos ($file, $pattern) !== false) 
        { 
          if(!is_dir($dir."/".$file))
          {
             unlink ($dir."/".$file);
          }
        } 
     }
  }
  else
  {
     trigger_error ( $dir . " isn't a directory", E_USER_ERROR);
  }
  closedir($handle); 
  return true; 
}


// now call the function

customDelete ("/home", ".txt.cnf");
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Parse error: syntax error, unexpected $end in 

I am getting that error message it says on the last line of the code....

Also, I edited a second copy of this to delete only if the file is in a folder called "/static/" to delete just .txt files from the cache dir...

Code: Select all

<?
function customDelete ($dir, $pattern) 
{ 
  if(is_dir($dir)) 
  { 
    if ($handle=opendir($dir)) 
    { 
      while($file=readdir($handle)) 
      { 
        if($file!="." && $file!=".." && strpos ($file, $pattern) !== false && preg_match("/\/static\/.*?\.txt/is",$file) )
        { 
          if(!is_dir($dir."/".$file)) 
          { 
             //unlink ($dir."/".$file); 
             echo $dir."/".$file."<br>\n";
          } 
        } 
     } 
  } 
  else 
  { 
    // trigger_error ( $dir . " isn't a directory", E_USER_ERROR); 
  } 
  closedir($handle); 
  return true; 
} 


// now call the function 

customDelete("/home/info0001/public_html/~subdomains/", ".txt");


?>
The main problem is the purely enourmous NUMBER of .txt.cnf files on the server, we are talking tens or hundreds of thosuands and I think that is where my old recursive delete function would fail ...


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Why don't you just delete them via your FTP client?
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

jayshields wrote:Why don't you just delete them via your FTP client?
uhm, because ftp has a limit of 2000 files per directory for starters, second off because they are SCATTERED ALL OVER THE server, third off, it would take 10 years to delete them all manually.
User avatar
tasteslikepurple
Forum Commoner
Posts: 46
Joined: Thu Jan 26, 2006 3:38 am
Location: Bath, UK

Post by tasteslikepurple »

Deseree wrote:Parse error: syntax error, unexpected $end in

I am getting that error message it says on the last line of the code....

Also, I edited a second copy of this to delete only if the file is in a folder called "/static/" to delete just .txt files from the cache dir...

<?
function customDelete ($dir, $pattern)
{
if(is_dir($dir))
{
if ($handle=opendir($dir))
{
while($file=readdir($handle))
{
if($file!="." && $file!=".." && strpos ($file, $pattern) !== false && preg_match("/\/static\/.*?\.txt/is",$file) )
{
if(!is_dir($dir."/".$file))
{
//unlink ($dir."/".$file);
echo $dir."/".$file."<br>\n";
}
}
}
}
else
{
// trigger_error ( $dir . " isn't a directory", E_USER_ERROR);
}
closedir($handle);
return true;
}


// now call the function

customDelete("/home/info0001/public_html/~subdomains/", ".txt");


?>
The main problem is the purely enourmous NUMBER of .txt.cnf files on the server, we are talking tens or hundreds of thosuands and I think that is where my old recursive delete function would fail ...
add another closing curly bracket before the else and it will fix the problem. your function won't fail based on the number of files, php should be able to handle it fine, it's only dealing with them one at a time

your preg_match won't work because the file will just be the file name, not the full path with directory name.

why not call the function a few times with different arguments to delete whatever you need. i.e.

Code: Select all

customDelete ('/static', '.txt');
customDelete ('/home', '.cnf.txt');
// etc etc
p.s. this version should work (got the extra bracket in it)

Code: Select all

function customDelete ($dir, $pattern)
{
  if(is_dir($dir))
  {
    if ($handle=opendir($dir))
    {
      while($file=readdir($handle))
      {
        if($file!="." && $file!=".." && strpos ($file, $pattern) !== false)
        {
          if(!is_dir($dir."/".$file))
          {
            unlink ($dir."/".$file);
          }
        }
      }
    }
  }
  else
  {
     trigger_error ( $dir . " isn't a directory", E_USER_ERROR);
  }
  closedir($handle);
  return true;
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: how can I seriously MASS delete .cnf files from a server

Post by timvw »

Deseree wrote:delete all those .txt.cnf files' away
With the basic shell tools ;) (remove the echo in order to actually remove the files)

Code: Select all

find /home -name '*txt.cnf' -exec echo rm {} \;
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

ok i deleted .txt.cnf files ok it seems... thanks....

now my problem is deleting .txt ONLY in /home/user/public_html/subdomain/static/ folders and lower....

I tried...

rm -fr /home/user/public_html/*/static/*.txt

but it came back " Argument list is too big"

so how would you advise this....

also i need to delete

rm -fr /home/user/public_html/*/*.html

files for another friend ( well this friend's friend's site(s) ).

Thanks again!!! This will be big help if I can do this
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

anyone can help me test and find a working solution.... I'm needing to delete thousands and thousands of files across a very large hard drive all hundreds in diffrent folders but all named static/

so i need to delete...

/home/userXXXX/public_html/*/static/*.txt

and any folders within. or else just DELETE every single folder named static/ on the server and recreate it immediately after deleting it, but with same ch permissions and chowned 0777.

Anyone can please help, outta disk space here...
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

tasteslikepurple wrote:
Deseree wrote:Parse error: syntax error, unexpected $end in

I am getting that error message it says on the last line of the code....

Also, I edited a second copy of this to delete only if the file is in a folder called "/static/" to delete just .txt files from the cache dir...

<?
function customDelete ($dir, $pattern)
{
if(is_dir($dir))
{
if ($handle=opendir($dir))
{
while($file=readdir($handle))
{
if($file!="." && $file!=".." && strpos ($file, $pattern) !== false && preg_match("/\/static\/.*?\.txt/is",$file) )
{
if(!is_dir($dir."/".$file))
{
//unlink ($dir."/".$file);
echo $dir."/".$file."<br>\n";
}
}
}
}
else
{
// trigger_error ( $dir . " isn't a directory", E_USER_ERROR);
}
closedir($handle);
return true;
}


// now call the function

customDelete("/home/info0001/public_html/~subdomains/", ".txt");


?>
The main problem is the purely enourmous NUMBER of .txt.cnf files on the server, we are talking tens or hundreds of thosuands and I think that is where my old recursive delete function would fail ...
add another closing curly bracket before the else and it will fix the problem. your function won't fail based on the number of files, php should be able to handle it fine, it's only dealing with them one at a time

your preg_match won't work because the file will just be the file name, not the full path with directory name.

why not call the function a few times with different arguments to delete whatever you need. i.e.

Code: Select all

customDelete ('/static', '.txt');
customDelete ('/home', '.cnf.txt');
// etc etc
p.s. this version should work (got the extra bracket in it)

Code: Select all

function customDelete ($dir, $pattern)
{
  if(is_dir($dir))
  {
    if ($handle=opendir($dir))
    {
      while($file=readdir($handle))
      {
        if($file!="." && $file!=".." && strpos ($file, $pattern) !== false)
        {
          if(!is_dir($dir."/".$file))
          {
            unlink ($dir."/".$file);
          }
        }
      }
    }
  }
  else
  {
     trigger_error ( $dir . " isn't a directory", E_USER_ERROR);
  }
  closedir($handle);
  return true;
}
thanks for your function, but it does not recurse sub directories, it only does for the folder you give ..... I just ran it and it did one level deep but didn't do any of the files in a folder in that directory.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Deseree wrote: now my problem is deleting .txt ONLY in /home/user/public_html/subdomain/static/ folders and lower....
find /home/user/public_html/subdomain/static/ -iname '*.txt' -exec echo rm {} \;
Deseree wrote: also i need to delete
rm -fr /home/user/public_html/*/*.html
man find
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Deseree wrote: or else just DELETE every single folder named static/ on the server and recreate it immediately after deleting it, but with same ch permissions and chowned 0777.
How can a folder have the same permissions if it needs to be chmodded 0777?? (Assuming you meant chmod since i don't expect a user 0777 to exist that will own the files)

find /home/wherever -type f -iname 'static' -exec echo rm -f {} \;

From now ion t's up to you to read the find manpage..
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

timvw wrote:
Deseree wrote: or else just DELETE every single folder named static/ on the server and recreate it immediately after deleting it, but with same ch permissions and chowned 0777.
How can a folder have the same permissions if it needs to be chmodded 0777?? (Assuming you meant chmod since i don't expect a user 0777 to exist that will own the files)

find /home/wherever -type f -iname 'static' -exec echo rm -f {} \;

From now ion t's up to you to read the find manpage..
thanks for your help, i HAVE been trying to use man find and find --help but I can't get it right....

anyways, all of your commands did absolutely nothing.


top priority for me, is to....

rm -fr /home/*/public_html/*/static*.txt

That would work BUT the arguement list is way too long because it finds thosuands of folders and tens/hundreds of thousands of .txt files...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Deseree wrote: anyways, all of your commands did absolutely nothing.
I added the echo in order you wouldn't shoot yourself in the foot... As long as you don't remove the echo, it will do absolutely nothing (apart from outputting the commands it would perform)..
Post Reply