Computing.Net > Forums > Unix > Deleting old files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Deleting old files

Reply to Message Icon

Name: duffster
Date: February 21, 2003 at 00:41:22 Pacific
OS: AIX 4.3.3
CPU/Ram: na
Comment:

I am writing a script which will run every 15mins and need to delete files from a given directory which are over n number of mins old.
Any Ideas??



Sponsored Link
Ads by Google

Response Number 1
Name: Jimbo
Date: February 21, 2003 at 03:25:48 Pacific
Reply:

Create a cron entry like:
0,15,30,45 * * * * /path/to/script.sh > /dev/null 2>&1

In the script:
- check for the 'test' file --> exit if not there
- loop through the files
- Check if they are older that the 'test' file
- Remove them if they are.
- Create/update timestamp of the 'test' file
- exit

The first time the script runs nothing should happen because the 'test' file does not yet exist.

Here is my attempt:
#! /bin/ksh
PATH=/path/to/your/dir/
TEST="${PATH}test_file"
[[ ! -f $TEST ]] && exit 1

for i in `find $PATH -type f` ; do
[[ $i = $TEST ]] && continue
[[ $i -ot $TEST ]] && rm -r $i
done

touch test_file


-jim


0

Response Number 2
Name: Jimbo
Date: February 21, 2003 at 03:45:04 Pacific
Reply:

Ohh,

the script should be run from cron at the interval in minutes you are looking for old files.

i.e. if you want to remove files that are older that 30 minutes old, run the script every 30 minutes.

-jim


0

Response Number 3
Name: duffster
Date: February 24, 2003 at 06:32:26 Pacific
Reply:

Thanks Jimbo.
I will give it a go.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Deleting old files

Deleting old files using ftp script www.computing.net/answers/unix/deleting-old-files-using-ftp-script/4257.html

Script to delete old files www.computing.net/answers/unix/script-to-delete-old-files/4094.html

Delete all files in sub-dir +10 old www.computing.net/answers/unix/delete-all-files-in-subdir-10-old/7166.html