Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I find & delete old files?

Status
Not open for further replies.

ITadvice

IS-IT--Management
Jul 22, 2008
38
0
0
US
I have been asked to create a file that will delete files that are older than 90 days. I am having trouble using the find command to find and delete the files. The files I want to delete are in one directory. Since the files have different names how can I find the old files without knowing what the names are? Should I use wildcards?
 
Perl has no 'find' function/command so I am not sure what your question is. There are a number of ways to get the list of files in a directory:

opendir/readdir
glob()
File::Find


Post your current code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You can use stat() on a file and the 10th element returned (or 9th in the array index) is the "mtime", or "modified time", of the file. mtime is a value like Unix time() that you can compare with the current time() to see how old it is.

Code:
my ($mtime) = (stat("/path/to/old/file.lck"))[9];

# There are a ton of seconds in 90 days
my $limit = 60*60*24*90;

# See if this file is too darn old
if (time() - $mtime >= $limit) {
   print "the file is way ancient, deleting it!\n";
   unlink("/path/to/old/file.lck");
}

Look into the things Kevin listed to find out how to open a directory and read its contents. Then just substitute "/path/to/old/file.lck" with some $variable containing an arbitrary file name.

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
What OS are you on? Do you have to use perl? If you're on Linux, you can just do this from the shell:

Code:
find * -mtime +89

If you're on Windows or you have to use perl, then you can use File::Find and stat.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top