Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Deleting Files

by mantra2006 (Hermit)
on Oct 07, 2006 at 15:19 UTC ( [id://576856]=perlquestion: print w/replies, xml ) Need Help??

mantra2006 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks
We have a server and its space is 96% occupied with files
and I have to delete files which are 30 days older and
also have a log which files got deleted. If there is a perl
one liner it would be great as I was running out of time with server...any help in this greatly appreciated

Thanks & Regards
Sridhar

Replies are listed 'Best First'.
Re: Deleting Files
by Joost (Canon) on Oct 07, 2006 at 15:33 UTC
      find2perl ...

      will allow you to do more perlish manipulations inside the find loop.

      Enjoy,
      Mickey

Re: Deleting Files
by davido (Cardinal) on Oct 07, 2006 at 15:41 UTC

    If "one liner" wasn't a 100% necessary criteria, you could do it very easily with File::Find. In your callback subroutine, do a -f test and a -M test to see if the file in question matches your criteria for deletion (note, that's a modification time, not a creation time). Then use unlink to do the deed.

    Be sure to open a logfile, and within your callback function print to the logfile to log the action.


    Dave

Re: Deleting Files
by marto (Cardinal) on Oct 07, 2006 at 15:24 UTC
Re: Deleting Files
by RobPayne (Chaplain) on Oct 07, 2006 at 19:17 UTC
    Joost and the others have already provided working examples of tools which do what you have asked. The caveat with this operation is to be careful enough to not get bitten by the same types of bugs that have bitten others in this case. (You need to make sure that you adequately escape special characters in file names, quoting spaces, etc. so you don't, for example, call rm on a file named "/server/dir /etc/passwd" with that space in the name. You didn't mention what platform you were running on, but you might want to take a look at utilities that have already solved this problem and have already learned some of the lessons associated with this operation. One such utility is 'tmpwatch', which is included with redhat-based linux distros.

    Tmpwatch uses -atime instead of -mtime as the basis for how old a file is and whether it is ready to be deleted. That way you don't remove files that haven't been written to, which are still being read by current processes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://576856]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found