LinHES Forums
http://forums.linhes.org/

My purge ipodfeed perl script
http://forums.linhes.org/viewtopic.php?f=5&t=14715
Page 1 of 1

Author:  bzImage [ Tue Mar 27, 2007 3:44 am ]
Post subject:  My purge ipodfeed perl script

I just made a small perl script to purge the contents of my ipodfeed directory based on a certain number of days. I run it via cron (root user cron table) every day.

I hope it can be useful for someone:

Code:
#!/usr/bin/perl -w
use Fcntl ':mode';

# Files equal or older to $purgedays will be erased
$purgedays=3;

# This is the directory where you keep you'r ipod encoded files
$workdir="/myth/ipodfeed";


$purgediff=$purgedays*3600;
$now=time();

opendir(DIR,$workdir) or die "Can't Open directory: $workdir!!!";
@FILES=grep(/\.mp4$/, readdir(DIR));

foreach $file (@FILES)
{

    $filename="$workdir/$file";
    $atime = (stat($filename))[8];

    $timediff=$now - $atime;

    if ( $timediff >  $purgediff )
    {
       ($xmlfile) = split(/\./,$file);
       $xmlpath="$workdir/$xmlfile.ipod.xml";
       unlink($filename) or die "Can't delete file : $filename!";
       unlink($xmlpath) or die "Can't delete file : $xmlpath!";
    }

}



Author:  Spud911 [ Fri Mar 30, 2007 5:29 pm ]
Post subject: 

I have actually been looking for something like this for a while. I get alot of podcasts and video podcasts and I have been looking for a way to autodelete them. My problem is that I keep my podcasts in separate directories and this script only seems to work in the main one.

I have looked at your script and I can see how I can modify it to remove other types of files, but since I don't know anything about perl I am unable to modify it so that it Greps the files recursively.

Is it possible to make this script do this?

Author:  mbd [ Sat Mar 31, 2007 7:25 am ]
Post subject: 

Spud911 wrote:
since I don't know anything about perl I am unable to modify it so that it Greps the files recursively.

Is it possible to make this script do this?


grep has a "-r" switch to do recursive searches down a directory tree, so you could try adding that.

Alternatively using the 'find' command in a bash script may be easier than the above script, and will do recursive searches.

For example:

Code:
#!/bin/bash

purgedays=3

workdir="/myth/ipodfeed"

if [ ! -d "$workdir" ]; then
        echo "Can't Open directory: $workdir!!!"
        exit 0
fi

find "$workdir" -name \*.mp4 -mtime +"$purgedays" -exec rm {} \;
find "$workdir" -name \*.xml -mtime +"$purgedays" -exec rm {} \;

exit 0



Note: use the above code at your own risk!

Author:  bzImage [ Sat Mar 31, 2007 1:21 pm ]
Post subject: 

Code:
#!/bin/bash

purgedays=3

workdir="/myth/ipodfeed"

if [ ! -d "$workdir" ]; then
        echo "Can't Open directory: $workdir!!!"
        exit 0
fi

find "$workdir" -name \*.mp4 -mtime +"$purgedays" -exec rm {} \;
find "$workdir" -name \*.xml -mtime +"$purgedays" -exec rm {} \;

exit 0


Yes this is will work also.

thanks.

Author:  Spud911 [ Thu Apr 12, 2007 2:01 pm ]
Post subject: 

Thanks guys that worked great, easier for me to hack it the way I need to as well.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/