Koozali.org: home of the SME Server
Legacy Forums => Experienced User Forum => Topic started by: Byte on August 23, 2002, 07:34:36 PM
-
I want to make a script to delete any files or folders that havent been used for a month or so
has anyone done this before
Any help would be most greatful
Thanks Byte
-
I do it with Perl and I would be willing to share the script, but you're going to want to isolate your search folder. I pray you're not talking about starting with /
Nathan
-
Nathan Thats great, i would only want to do this on the /home/e-smith/files/ibays/public
As people just dump stuff for a week or so and doesnt ever get touched. if you could share how to do this would be great how would you want to share this?
Thanks for response
Byte
-
Nathan,
If you wouldn't mind sharing that script with me I also appreciate it.
Thanks,
-jeff
-
Create a file in /etc/cron.daily named Cleanup
chmod 755 /etc/cron.daily/Cleanup
pico -w /etc/cron.daily/Cleanup
[Add the following code to the file:]
#!/usr/bin/perl
# /* August 23, 2002
# * Nathan Fowler
# * evilghost@packetmail.net
# * This script will clean-up the public files to ensure old files are deleted
# */
#Directory containing the files, must be a folder
$popauthspool = "/home/e-smith/files/ibays/public";
#Logfile to recored deleted files to.
$watcherlog = "/var/log/public_cleanup.log";
$date = localtime();
open(LOG,">>$watcherlog") || die("Can't open $watcherlog");
use File::stat;
@ips = ls $popauthspool;
foreach $ip (@ips) {
chop($ip);
$file = $popauthspool ."/". $ip;
$s = stat($file) || die "Can't stat($file) $!\n";
#Delete files 31 Days old. (60 * 60 * 24 * [Num Days] = The Value)
$modtime = $s->mtime + 2678400;
$now = time;
if($modtime < $now){
push(@expired,$ip);
}
}
foreach $expired (@expired){
$date = localtime();
print LOG "$date - Removing $expired\n";
unlink($popauthspool ."/". $expired);
}
close(LOG);
[Save the file]
To view the deleted files:
cat /var/log/public_cleanup.log|more
Hope this helped,
Nathan
-
I do backups to disk at one of my client's site and as the disk fills up, I remove older files. I use the following lines within the SME backup script and it works very well. I have modified for your case in order to have an example you can use.
Create a new executable shell script as follows:
#!/bin/sh
# CD to a specific directory
cd /home/e-smith/files/ibays/public
# Test with the following line as it will just list the files and not remove them
find -name "*" -atime +30 -exec ls -la {} \;
# Once you are happy, use the following line instead ro remove the files with force
# find -name "*" -atime +30 -exec rm -f {} \;
Call the file anything you want but make sure you enable the execute permission.
You can execute it from the command line or include it within the /etc/crontab.
That's it!
You can expand on this by repeating for other directories, You can mount removable zip drives and clean them before copying new files to ensure there will be enough room...
Hope this helps.
Serge.
-
Hey thanks for sharing that!!!!!!!!
Great Stuff
Byte
PS Thanks to Nathan & Serge for 2 great scripts!!!!