LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-03-2004, 07:31 PM   #1
MaverickApollo
Member
 
Registered: Oct 2003
Distribution: Debian
Posts: 57

Rep: Reputation: 15
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!!!
 
Old 02-03-2004, 07:45 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
The Linux Cookbook has some tips on things like this; check out this chapter for specifics.
 
Old 02-03-2004, 07:52 PM   #3
TBC Cosmo
Member
 
Registered: Feb 2004
Location: NY
Distribution: Fedora 10, CentOS 5.4, Debian 5 Sparc64
Posts: 356

Rep: Reputation: 43
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>
 
Old 02-03-2004, 07:54 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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 {} \;
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete mail based on date gquiring Linux - General 8 05-08-2013 09:24 AM
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 01:21 AM
Delete files based on date stefaandk Linux - General 3 06-17-2005 02:20 AM
Deleting data from file by date? mephitic Linux - Newbie 3 09-22-2004 12:33 PM
Listing Files based on date axero Linux - Newbie 2 10-19-2003 04:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration