Operating System - HP-UX
1833513 Members
2944 Online
110061 Solutions
New Discussion

remove files older than a certain date/time

 
SOLVED
Go to solution
Kamran Hussain_1
Occasional Contributor

remove files older than a certain date/time

How do I do this: I want to remove files that are older than a certain date/time (to be passed as input params).

I am aware of the FIND command, but this only find the files that are older than 'n' days (in integer). I want to specify a time also.

Is there a quick one line code, or will I have to get a bigger set, and use SED etc to parse the columns and determine the time ?

Thanks in advance :-)
5 REPLIES 5
Patrick Wallek
Honored Contributor

Re: remove files older than a certain date/time

You could do this with a combination of the 'touch' command and 'find' with the '-newer' option.

You can touch a file and give it a specific date and time stamp.

You could then use this file as a reference file for the '-newer' argument to find. Just remember to do a '! -newer' so it looks for thing NOT NEWER than the referenced file.

Have a look at the man pages 'man find' and 'man touch' for more information.
Rodney Hills
Honored Contributor

Re: remove files older than a certain date/time

You could do the following-

touch 200302011137.43 /tmp/testfile
find /yourdir ! -newer /tmp/testfile

This creates a file with a modify time of the value specified on the "touch" command (see man touch).

The "!" means "not", so find will look for files not newer than the data/time stamp on /tmp/testfile.

HTH

-- Rod Hills
There be dragons...
Jeff Schussele
Honored Contributor
Solution

Re: remove files older than a certain date/time

Hi Kamran,

Use the touch command to create a file at the moment in time you wish to use as cutoff

touch -t 200301101234 foo

This will create foo & set it's date/time to Jan 10 12:34 PM

Then use find as follows:

find /search_start ! -newer /path/to/foo -exec rm {} \;

This tells it NOT newer than the date/time stamp on foo.
Do the above with ll in place of rm to verify just what WILL be deleted.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
James R. Ferguson
Acclaimed Contributor

Re: remove files older than a certain date/time

Hi:

# touch 02241400 /tmp/ref ...Feb 24 at 1400
# cd /dir
# find . -xdev -type f ! -newer /tmp/ref|xargs rm

Regards!

...JRF...
Ionut Grigorescu_2
Super Advisor

Re: remove files older than a certain date/time

Hi,

I have put such a line in my crontab (root) to remove files older than 4 days in a directory.
55 23 * * * /usr/bin/find /d/ -mtime 4 -type f| xargs rm -f

Cheers,
ionut
If it weren't for STRESS I'd have no energy at all