LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script for deleting files based on date (https://www.linuxquestions.org/questions/linux-general-1/script-for-deleting-files-based-on-date-142002/)

MaverickApollo 02-03-2004 07:31 PM

Script for deleting files based on date
 
Does any one know of any ready written scripts that I can base a script of my own, which will be responsable for deleting files based on date. If that file is over 1 month old, it gets deleted. This will be run from a cron tab.

I have never even attempted to write a script before, and have no idea where to start, so any pointers would be appreciated!!!

wapcaplet 02-03-2004 07:45 PM

The Linux Cookbook has some tips on things like this; check out this chapter for specifics.

TBC Cosmo 02-03-2004 07:52 PM

how 'bout:

assume current dir has the old files

find . -mtime +30 -exec rm {} \;

and cron that to run each night. Should delete for each file not modified for a month

Code:

futur6/usr/users/wayne/junk> ls -lart
total 20
-rw-r--r--  1 wayne    dba          393 Nov 10 08:59 hung-ftp.txt
-rw-r--r--  1 wayne    dba          705 Nov 17 10:25 vmstat.txt
-rw-r--r--  1 wayne    dba        1867 Nov 17 10:57 vmstat-p.txt
drwxr-xr-x  2 wayne    dba        8192 Nov 20 09:32 .
drwx------  7 wayne    dba        8192 Feb  3 08:00 ..
futur6:/usr/users/wayne/junk> find . -mtime +30 -exec rm {} \;
rm: cannot remove '.' or '..'
futur6/usr/users/wayne/junk> ls
futur6/usr/users/wayne/junk>


homey 02-03-2004 07:54 PM

Here's mine. If you want to change the age before deleting, change the part -mtime +90 to -mtime +30

#!/bin/bash
filename=`date '+%m%d%y'`
tar -cvzf /mnt/backup/${filename}.tar.gz /home
#Delete old files with the following command
find /mnt/backup -type f -name '*.gz' -mtime +90 -exec rm {} \;


All times are GMT -5. The time now is 02:38 AM.