Operating System - HP-UX
1830085 Members
13605 Online
109998 Solutions
New Discussion

How to Delete files older than 6 hours

 
rajesh khanna
New Member

How to Delete files older than 6 hours

What is the best way to delte files older than six hours in HP-UX 11i.

Thanks in Advance
Raj
8 REPLIES 8
Pete Randall
Outstanding Contributor

Re: How to Delete files older than 6 hours

Use the touch command to create a reference file with the desired timestamp, then use the find command to delete files older than the reference file:

find /start_dir ! -newer ref_file -exec rm {} \;


Pete

Pete
rajesh khanna
New Member

Re: How to Delete files older than 6 hours

How do a touch a file with timestamp as current time minus six hours ?
James R. Ferguson
Acclaimed Contributor

Re: How to Delete files older than 6 hours

Hi Raj:

You can do this to create a reference point that is 6-hours ago:

# t=`perl -e 'use POSIX;$t=time-(6*60*60);print(strftime "%m%d%H%M",localtime($t))'`

# touch -amt ${t} /tmp/myref

# find /path -xdef -type f ! -newer /tmp/myref | xargs rm

This will find all files in the /path you specify; not cross mountpoints (-xdev) and remove all files that are older (i.e not newer) than the reference file's modification time in epoch seconds. Note that (6*60*60) is the 6-hour time you requested.

Regards!

...JRF...
Alzhy
Honored Contributor

Re: How to Delete files older than 6 hours

Your best bet is to ocmpile/install "GNU find" from the GNU findutils bundle. It adds "-cmin -amin -mmin" for "minutes ago" change, access and modidfication time searches on files.

Normal "find" command has a granularity of days.

Hakuna Matata.
Alzhy
Honored Contributor

Re: How to Delete files older than 6 hours

BTW, the Porting site already has a pre-built depot ready:

http://hpux.connect.org.uk/hppd/hpux/Gnu/findutils-4.2.27/

Enjoy!
Hakuna Matata.
A. Clay Stephenson
Acclaimed Contributor

Re: How to Delete files older than 6 hours

You also need to define what "older" means; UNIX has no concept of a file's creation time. If you happen to know that it is only by accident. You can, for example, know that a file has not been modified in 6 hours.
If it ain't broke, I can fix that.
Chan 007
Honored Contributor

Re: How to Delete files older than 6 hours

Raj,

touch a newfile, then use find with != newer , then remove

Chan
Kent Ostby
Honored Contributor

Re: How to Delete files older than 6 hours

BE CAREFUL ... Make sure you're only working in the current directory or below.

Things like /stand/vmunix are greater than 6 hours old. If you start throwing around "."s and "*"s then you're next post will be on how to re-ignite the system.

I speak from experience.
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"