Script to delete a file tree

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Hello, I goofed!

When our site went live, I forgot to disable debug logging. Now I have a ton of files and directories that are owned by "nobody" (as they were created by the web site), that I can't just go in and hit delete.

(I'd do have certain ones I need to keep).

Before I go and write my own script to delete all of these, does anyone know of where one already exists that I could use and modify?

My debug directory looks kinda like so (Used fake IP's and usernames for sample)

  /debug
    /IP_265.32.234.34
  2005-03-23_14.23.14.log
   2005-03-23_14.24.33.log
   2005-03-23_14.33.93.log
    /IP_265.132.234.34
   2005-03-23_14.23.14.log
   2005-03-23_14.24.33.log
    /UN_joeuser
   2005-03-23_14.23.14.log
   2005-03-23_14.24.33.log
   2005-03-23_14.33.93.log
    /IP_265.32.134.34
   2005-03-23_14.23.14.log
   2005-03-23_14.24.33.log
    2005-03-23_14.33.93.log
   2005-03-23_16.24.33.log
    2005-03-23_16.33.93.log
    /UN_sampleuser
   2005-03-23_14.23.14.log
   2005-03-23_14.24.33.log
   2005-03-23_14.33.93.log
'

The IP_ ones are for before people log in. THe UN_ ones are once they are logged in entering data.

I'm wanting to keep the UN_ ones for now. I would like to eventually get it where it checks, anything in IP_ older thant 2 days delete (and the directory if it is empty) and anthing that is UN_ older than 10 days delete (don't delete directory).

I know I can write this, but just would save a lot of time if there is something canned I can use for now.

Thanks for your help.

-Greg

He has: 1,758 posts

Joined: Jul 2002

Easiest solution would be to contact your host and get them to log in as root and delete the files. It will only take them a couple of seconds.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Yeah, but i have to write a scrip to dothis every so often anyhow. Once it is up and running, will have it run as cron each night.

-Greg

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Here is what I ended up throwing together for my use in case anyone ever comes across this thread needing something similar.

This script just deleted any log files over a week old in the directories starting with IP_ (indicates not logged in as a user). And then if that directory becomes empty after deleting the files, then it will delete the directory as well.

Enjoy, let me know if you have any questions using/modifying it.

-Greg

PS. Note that I am using PHP 4.something, so I couldn't the scandir function built into version 5. I wrote my own and added a little extra:

$path = (string) the path you are wanting a listing of
$incRel = (true/false) should include "." and ".." in the list
$getWhat = ('d','f','b') entriest to list, directories, files, or both
RETURNS: Array of the filenames (and direcotry names) in the path specified

<?
  function scandir2($path,$incRel=true,$getWhat='b')
  {
    $dirList = array();
    $dirHand = opendir($path);
 
    while (false !== ($file = readdir($dirHand)))
    {
  if (is_dir($path . "/" . $file))
  {
    if ($getWhat != 'f')
  if ($file == "." || $file == "..")
    { if ($incRel) $dirList[] = $file; }
  else
    $dirList[] = $file;
  }
  else
     if ($getWhat != 'd')
  $dirList[] = $file;
    }
 
    closedir($dirHand);
     return $dirList;
  }
 
  $basePath = "/base/dir/path/without/trailing/slash";
 
  $lastWeek = mktime(0, 0, 0, date("m"), date("d")-7,  date("Y"));
 
  $rootDir = scandir2($basePath,false,'d');
 
  foreach ($rootDir as $thisDir)
    if (substr($thisDir,0,3)=="IP_")
    {
  $subDir = scandir2($basePath . "/" . $thisDir,false,'f');
  $canDelete = true;
 
  foreach ($subDir as $thisFile)
  {
    $fullFile = $basePath . "/" . $thisDir . "/" . $thisFile;
    if (filemtime($fullFile)<$lastWeek)
  unlink($fullFile);
    else
  $canDelete = false;
  }
 
  if ($canDelete)
    rmdir($basePath . "/" . $thisDir);
    }
 
  ?>
'

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.