Cron job to delete files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cron job to delete files
# 1  
Old 02-21-2009
Cron job to delete files

Hi everyone! I'm sorry, I'm a total noob but would really appreciate any advice or help. I want to create a cron job that would run every hour and would look inside a few different folders. If any new files were created within those folders within the last hour they would be destroyed, but any other files that are older than one hour would be left alone.

I've been reading various sites and forum threads and I'm starting to grasp the concept of how it would work. I found another forum thread that had cron job that would delete files that were older than thirty days:

find /home/myAccount/mail/example.com/spam/cur -mtime +30 -exec rm {} \;

I can see where I need to spell out the path to the folders. My question is, how would I tell it to destroy any new files made within the past hour? Is that even possible? And would I have to have a different cron job for every folder that I want files erased from?
# 2  
Old 02-21-2009
Quote:
Originally Posted by jessn
My question is, how would I tell it to destroy any new files made within the past hour? Is that even possible?
Use the -mmin option of find.

Quote:
Originally Posted by jessn
And would I have to have a different cron job for every folder that I want files erased from?
It's unnecessary to create different cronjobs, put the commands in a script and run it with one cronjob.

Regards
# 3  
Old 02-25-2009
Code:
find folder1 folder2 folder3 -mmin -60 -exec rm -f {} \;

Something like this should do what you want.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cron job that checks for new/recent files

I'm new to shell scripting, and I want to set up a cron job that scans the date/time stamp of all files in a directory, and then if any file is, say, less than 10 minutes old, I want it to execute a command on that file. What would be the best way to go about this? Thanks. Not sure if it makes a... (1 Reply)
Discussion started by: DavidHoffman
1 Replies

2. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

3. Shell Programming and Scripting

Commented cron job -- cron monitoring

Hi I have a requirement to write a shell script,that will check the all commented job in cron job.Please help !! (2 Replies)
Discussion started by: netdbaind
2 Replies

4. UNIX for Advanced & Expert Users

cron job to extact lines from files to another file and ftp to new server

i have a text file in this format: which creates a new one everyday in the form of filename _zing__r200_2012_8_10_log.txt Fri Aug 10 07:29:17 EDT 2012, usera(192.168.0.245) to anotheruser: hey top, this is a private test Fri Aug 10 07:29:28 EDT 2012, anotheruser(192.168.0.245) to usera: got... (2 Replies)
Discussion started by: bkkid
2 Replies

5. Shell Programming and Scripting

Script that will be a cron job to export rrd files for cacti server

I wrote a quick little script that will eventually end up as a cron job to export rrd files for my cacti server. Here is the script: #!/bin/bash rm -rf /backup/cacti_xml/* cd /var/www/html/rra ls -1 *.rrd | awk '{print "rrdtool dump "$1" > /backup/cacti_xml/"$1".xml"}' | sh -x Is there... (5 Replies)
Discussion started by: TheBigAmbulance
5 Replies

6. Shell Programming and Scripting

Find and delete files before a job is submitted to queue

Hello all. I need some help modifying the following script: #!/bin/bash #PBS -l nodes=1:ppn=8,walltime=48:00:00,os=centos53computeA ## To submit type: qsub x.sh # If not an interactive job (i.e. -I), then cd into the directory where # I typed qsub. if ; then if ; then cd... (3 Replies)
Discussion started by: marcozd
3 Replies

7. UNIX for Dummies Questions & Answers

Cron Job Config files

Hi All, I have created 2 shell scripts and set Cron jobs which runs it at various frequencies.The first one which runs every 2 minutes Monday to Friday and another cron job runs at 11PM on the last Sunday of every month. I have given the cron job entries below. Was wondering whether instead... (5 Replies)
Discussion started by: valluvan
5 Replies

8. Shell Programming and Scripting

A Batch job to delete files from various directories

Hi, I have a shell script to find files older than 'X' days ($2) in directory path ($1) and delete them. Like this: my_file_remover.sh /usr/home/c 90 Now, I need to modify this script and add it in CRON, so that it checks other directories also. Like: my_file_remover.sh /usr/home/c... (3 Replies)
Discussion started by: guruparan18
3 Replies

9. UNIX for Dummies Questions & Answers

CRON. How to delete files within a directory

I'd like to delete ALL files on a daily basis within a directory that are over a day old. Anyone know how I can automate this through Cron as I have 146 websites to administer. I've tried... 30 02 * * * /home/myspace/tmp/webalizer -atime + 1\! -type d -exec rm -f {} \; but all i get is an... (1 Reply)
Discussion started by: southoxon
1 Replies

10. Shell Programming and Scripting

CRON-Job / Shell-Skript deleting special files

Just I'm trying to find script, which will do the following job: 1. as a CRON-Job it shoult a) delete files which will be either older than 24 hours or b) all files within a) a directory deleting recursive b) only a special directory. 2. write an error-/Delete_log... (9 Replies)
Discussion started by: ManfredWL
9 Replies
Login or Register to Ask a Question