Operating System - HP-UX
1758535 Members
1794 Online
108872 Solutions
New Discussion юеВ

Re: how to delete files which are olderthan 24 hours

 
ram_26
Occasional Contributor

how to delete files which are olderthan 24 hours

hi,

i have a shell script which deletes files which are olderthan 2 days.... how can I change this to delete files which are olderthan one day (24 hours)

i need a shell script to do that... in hpux 11i

thnx in advance...

--ram
9 REPLIES 9
Sanjay_6
Honored Contributor

Re: how to delete files which are olderthan 24 hours

Hi,

you can try this with a combination of touch and find command.

say the current time is 10 am Nov 6th, touch a file with a timestamp of 10am nov 5th

touch -t 200411051000 /tmp/testfile

Now use this file to ocheck on the timestamp of the other files in the directory where you want to look for files more than 24 hours old.

find /your_dir -type f !-newer /tmp/testfile -exec ll {}\;

to delete these files
find /your_dir -type f !-newer /tmp/testfile -exec rm {}\;

Hope this helps.

regds


Hein van den Heuvel
Honored Contributor

Re: how to delete files which are olderthan 24 hours

>> have a shell script which deletes files which are olderthan 2 days.... how can I change this to delete files which are olderthan one day (24 hours)

So why don't you share (attach as txt?) with us the chunk of the script that you suspect selects the files now.
A reader may be able to adapt the existing script to the new requirement.


btw... for some quick & dirty cleanup scripts it is enough to just do
find $target -mtime +1 -exec rm {} \;

Be sure to check 'man find' for the various time selections you can make and details on how to specify older vs newer and so on.

hth,
Hein.


Muthukumar_5
Honored Contributor

Re: how to delete files which are olderthan 24 hours

You can do as,

find -type f -name "" -mtime +1 -exec rm -i {} \;


And,

if you are having script then, just change 2 day to 1 day :)

HTH.
Easy to suggest when don't know about the problem!
Jan Sladky
Trusted Contributor

Re: how to delete files which are olderthan 24 hours

Hi Ram,

use your script ;-)

example

find /opt/oracle/SID/bdump -mtime +1 -exec rm{} \;

br Jan
GSM, Intelligent Networks, UNIX
twang
Honored Contributor

Re: how to delete files which are olderthan 24 hours

Sanjay gave the wonderful solution, by the way, remember the following command will file under /dir from (the moment - 24hours) you issue the command:
# find /dir -type f -mtime +1 -exec rm -i {} \;
ram_26
Occasional Contributor

Re: how to delete files which are olderthan 24 hours

hi friends...,

thnx a lot.... for ur help.... one last and small doubt... if i want to delete the files which are olderthan 12 hours how can I do it.....?

thnx in advance...

--ram
twang
Honored Contributor

Re: how to delete files which are olderthan 24 hours

how about the following:
# touch -t /tmp/ref
find . !-newer /tmp/ref -type f -exec rm {}\;
Hein van den Heuvel
Honored Contributor

Re: how to delete files which are olderthan 24 hours

The same methods still apply... mostly.
The find, just with time becomes too coarse.
The find with -newer become more interesting.
You may also want to check out the perl -M operator to give an age to work with. The same perl script could do the finding ('glob') and the delete (unlink).

Howeverm, now that you are talking about such rapid turnaround on your files I'm starting to think this may be about log / trace / eds / temp files for a specific application which needs to be reigned in somewhat.

You may want to consider a build in method versus after the fact cleanup. For example, maybe those files are all created in a specific directory? How about making that directory a softlink to a time-stamped directory? Every hour, on the hour, or every 6 hours, you create a fresh directory and flip the soft-link to point to the new directory. Once that is done you delete the directory that corresponds with the current delete interval you need to maintain.
Or... you could look at the space used / left and delete older directory untill the space need is acceptable instead of blindly deleting older files whether you need the space or not.

[those directories could have a date+time encoded in the name YYMMDDHHMM, or could just have numeric sequence number as name.]
Just thinking out aloud...

hth.

Hein.
Fadia Almarei
Super Advisor

Re: how to delete files which are olderthan 24 hours

why you do not use the crontab process by adding a script to run every day

#crontab -e
* 6 * * * /testscript

and the testscript contant is

find / -atime +1 -exec rm {} \;

fadia.marei