DaemonForums  

Go Back   DaemonForums > OpenBSD > OpenBSD General

OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 30th May 2008
plexter plexter is offline
Shell Scout
 
Join Date: May 2008
Posts: 124
Thanked 0 Times in 0 Posts
Question Delete Files By Schedule

Hi,

I am currently in the process of setting up a temporary file storage server.

I am trying to have a 'script' running which will automatically remove files older than X number of days.

File upload/retrieval is done via FTP. So the script will need to be run separately of this. Basically the file system will look something like this:

[FTP ROOT] -> [username] -> uploaded files and directories

Ideally I would like to a) remove all files older than X days and than after Y remove all files and directories including the [username].

I think I know how to do this (more or less) however I would like to know from others what might be the best way of doing this.

Simplicity is ideal. I would also prefer not to have to install additional software unless its really worth it.

Your help is appreciated. Thanks
Reply With Quote
  #2   (View Single Post)  
Old 30th May 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

How I would first try to do it:

a cron job A that uses find to recurse for all files/dir's in the target hier and rm -rf's them if criteria X are met.

a cron job B that uses find to do likewise but for Y instead of X.


GNU Find#time


Another trick would be using a cronjob to run a custom made hunt & kill script or a self-rolled daemon to hunt and peck the files, perls a nice handler for that.

But cron+find should be able to do it all.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #3   (View Single Post)  
Old 31st May 2008
plexter plexter is offline
Shell Scout
 
Join Date: May 2008
Posts: 124
Thanked 0 Times in 0 Posts
Default

Hi Terry thanks for the reply.

Would you happen to have an example command line for cron?

Say for something that will check every 5 minutes and will remove all files older (but not younger) than 7 days.

Thanks for your help!

Last edited by plexter; 31st May 2008 at 10:48 PM.
Reply With Quote
  #4   (View Single Post)  
Old 1st June 2008
scottro's Avatar
scottro scottro is offline
Real Name: Scott Robbins
Spam Deminer
 
Join Date: Apr 2008
Location: NYC
Posts: 298
Thanked 31 Times in 25 Posts
Default

Something like

cd /var/ftp (or wherever you keep the files)
/usr/bin/find ./ -type f -mtime +7 -exec rm -f '{}' +

-type f file
-mtime + 7 older than 7 days
-exec rm -f '{}' +
That + at the end was something I found while googling--I'm trying to remember its purpose. Ah, I think it was a way to avoid getting the error of filelist too long.

http://www.hccfl.edu/pollock/Unix/FindCmd.htm

WARNING: DANGER and all that stuff.

I would definitely test it with echo rm rather than just rm before putting it into action.
Reply With Quote
  #5   (View Single Post)  
Old 1st June 2008
robbak's Avatar
robbak robbak is offline
Real Name: Robert Backhaus
VPN Cryptographer
 
Join Date: May 2008
Location: North Queensland, Australia
Posts: 366
Thanked 40 Times in 39 Posts
Default

The + is simply a delimiter to tell find where the -exec command ends. Convenient, because it means that we can avoid a heap of multiple escaping and quoting. (normally, these sort of commands are entered as a single unit, which means quoting with ' or ", escaping with many \\\\'s, or some hellish combination of the three! )
__________________
The only dumb question is a question not asked.
The only dumb answer is an answer not given.
Reply With Quote
  #6   (View Single Post)  
Old 1st June 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Not familiar with all of finds many innerds so I didn't know that, thanks a lot for the info about the + Scottro.


And yeah, testing with echo is one of the scripters best friends.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #7   (View Single Post)  
Old 3rd June 2008
plexter plexter is offline
Shell Scout
 
Join Date: May 2008
Posts: 124
Thanked 0 Times in 0 Posts
Default

Hello all,

Thank you very much for your replies.

scottro:

I am having trouble getting your command to work.

I tried inputting into CRON


30 * * * * cd /usr/path && /usr/bin/find ./-type f -mtime +7 -exec rm -f '{}'+
and
30 * * * * cd /usr/path /usr/bin/find ./-type f -mtime +7 -exec rm -f '{}'+

and nothing seems to happen.

I have also tried simply typing the command on the command line and I get dropped into a > prompt where I do not seem to be able to do anything. Or it says "find: -exec: no terminating ";" "

I tried adding a ; but that didnt seem to help.


I am not sure if I am doing something wrong, if cron is not running for root??? or what could be up.

Oh and I setup cron by: crontab -u root -e

Help would be greatly appreciated.
Reply With Quote
  #8   (View Single Post)  
Old 3rd June 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Helpful companion
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Thanked 193 Times in 184 Posts
Default

$ /usr/bin/find /usr/path -type f -mtime +7 -exec ls {} \;
That would list files older then 7 days, adjust it to your needs...
Reply With Quote
  #9   (View Single Post)  
Old 3rd June 2008
scottro's Avatar
scottro scottro is offline
Real Name: Scott Robbins
Spam Deminer
 
Join Date: Apr 2008
Location: NYC
Posts: 298
Thanked 31 Times in 25 Posts
Default

Oops, typo on my part, BSDFan fixed it.
Reply With Quote
Old 3rd June 2008
plexter plexter is offline
Shell Scout
 
Join Date: May 2008
Posts: 124
Thanked 0 Times in 0 Posts
Default

hmm.. thanks BSDfan666

I issued the command:

/usr/bin/find /path/ -type f -mtime +7 -exec rm -f {} \;

and it removed the files older than 7.

However it does not seem to run when I put it as a crontab.

I suspect the cron job is not even running. Do I have to 'enable' cron? Any ideas?

Thanks for your help.
Reply With Quote
Old 3rd June 2008
plexter plexter is offline
Shell Scout
 
Join Date: May 2008
Posts: 124
Thanked 0 Times in 0 Posts
Default

I just don't get it... I'm positive CRON is not running the command now.


I have done the following:

mkdir /path/LOGS

crontab -e

Code:
5     *     *     *     *     /usr/bin/find /path/ftp/ -print -type f -mtime +7 -exec rm -f {} \; > /path/LOGS/deleted.log
The command (minus CRON stuff) works fine via command line.

I do not see anything show up in the log (file does not even get created) and my files older than 7 days are still there.

Does anyone here have more experience with CRON who might know why it is not running?

Thanks a lot.
Reply With Quote
Old 3rd June 2008
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 3,610
Thanked 214 Times in 189 Posts
Default

You have what appears to be shell script, rather than a single executable command in your crontab entry.

Stick your shell script in a text file. Make your first line of that file:
#!/bin/sh
After saving the file, use chmod +x to mark the file executable.
Edit your crontab entry to make that file the command.
Reply With Quote
Old 4th June 2008
plexter plexter is offline
Shell Scout
 
Join Date: May 2008
Posts: 124
Thanked 0 Times in 0 Posts
Default

Hi again,

I just wanted to let you know that with your help all seems to be working now. I've tested and the script seems to run as planned.

Thanks a lot for your help!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FreeBSD console delete key graudeejs FreeBSD General 4 24th August 2008 01:37 PM
Cannot delete it.... graudeejs FreeBSD General 9 20th July 2008 12:45 PM
Delete Files By Schedule Continued plexter OpenBSD General 4 28th June 2008 03:43 AM
How to delete account? khdf Feedback and Suggestions 5 9th May 2008 09:05 PM


All times are GMT. The time now is 11:05 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick