It is currently Mon, 24 Nov 2014 06:47:19 GMT



 
Author Message
 ksh: how to find/ls files older than n HOURS, not DAYS
hi all, I am trying to find a korn shell function that lists/finds all files
older than n hours, just like find -mtime/atime, but not by days. more
specifically I want to remove files (*.log) in a directory older than 25-26
hours. would someone help? thanks

JW



 Sat, 10 Apr 2004 08:56:31 GMT   
 ksh: how to find/ls files older than n HOURS, not DAYS

One way is to touch a file with a specific date & time, then use that
file as a base for your find command

$ find / -newer touched_file -exec blah...



 Sat, 10 Apr 2004 12:42:55 GMT   
 ksh: how to find/ls files older than n HOURS, not DAYS

Gnu find, www.gnu.org (www.sunfreeware.com if you use SunOS or Solaris) or
touch and find ! -newer



 Sat, 10 Apr 2004 13:07:47 GMT   
 ksh: how to find/ls files older than n HOURS, not DAYS
It is not easy to simply touch a file in my case.

I want to have a script to run hourly, to remove database logfiles offsite,
but keeps 25-26 hours worthy of logfiles on local disk. It is a moving
window. I prefer not  to touch a file every hour since I have up to 10
databases running on the box and each one has its own scheduled jobs. I am
looking for something self-contained (not depends on other messaging files).

JW

news:E98B898CBF025618.3DEC98C1B173BE47.55E62CBE31272F70@lp.airnews.net...



 Sun, 11 Apr 2004 07:42:55 GMT   
 ksh: how to find/ls files older than n HOURS, not DAYS

If you have the GNU versions of date and find, try this:

/usr/local/bin $ cat olderthan
## USAGE: olderthan N ## where N is the number of hours
now=`date +%s`
then=$(( $now - (3600 * ${1:-1}) )) ## default to one hour old

find . -name '*.log' -printf "%A@ %p\n" |
    awk -v cutoff=$then -v now=$now '
        $1 < cutoff { print $2 }
'
## EOS ##

If you don't have the GNU commands (some other versions of date have the
%s formatting option) you could parse the output of ls -l and work from
that:

ls -l *.log | while read p _ u g s m d t f _
do
    ## convert and compare
done

## or

ls -l *.log | awk '{
        file = $9
        month = $6
        ## etc....

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



 Sun, 11 Apr 2004 10:22:21 GMT   
 ksh: how to find/ls files older than n HOURS, not DAYS

And, to touch the file 2 hour old:
HOURS=2
TZ=GMT touch -t `TZ=GMT+$HOURS date +%Y%m%d%H%M.%S` touched_file

--
Stphane



 Mon, 12 Apr 2004 03:57:50 GMT   
 ksh: how to find/ls files older than n HOURS, not DAYS
thanks all replied. Since I am using IBM AIX which doesn't GNU extension for
its date and find commands, I eventually wrote a small C program to
accommodate this.

JW

news:slrn9te31a.8o.stephane.chazelas@pcchazelas.free.fr...



 Tue, 13 Apr 2004 09:12:14 GMT   
 ksh: how to find/ls files older than n HOURS, not DAYS

news:OL2C7.684$lY5.702625@newssvr28.news.prodigy.com...

Why couldn't you just download GNU find and date?  I'm sure you'll find many
other features in them that you'll find invaluable.



 Tue, 13 Apr 2004 16:47:27 GMT   
 
   [ 8 post ] 

Similar Threads

1. find -name a* -mtime +1 -exec rm {} -- in hours not days --

2. find -name abc -mtime +1 -exec rm {} -- in hours not days --

3. finding files older than x hours

4. find file more than one hour old

5. Find files older than x hours

6. Find file less than hour old?????

7. find files < 8 hours old

8. Finding files older than X days WITHOUT recursing subdirectories

9. finding files less than a day old

10. ksh shell: Finding and moving old files


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software