1833772 Members
2369 Online
110063 Solutions
New Discussion

Find command problem

 
SOLVED
Go to solution
Jayson B. Hurd
Advisor

Find command problem

I am trying to use the find command to look for files less than x minutes old. Problem is, most of these HP and Sun machines here have an older find command that only has switches that go by day, not minutes.

Is there any way I can use this old find command to find files less than x minutes old or is there another command that may do this directly or indirectly?

Thanks, Jayson
11 REPLIES 11
Pete Randall
Outstanding Contributor
Solution

Re: Find command problem

Jayson,

Your only hope is to use -newer . Touch a file with the time you need to check and then run find with either -newer or ! -newer as the case may be.


Pete

Pete
Mel Burslan
Honored Contributor

Re: Find command problem

if the files you are looking for, are in the same directory, this hack may work

touch markerfile -t MMDDhhmm
ls -rt > /tmp/tempfile
markerloc=`grep -n markerfile /tmp/tempfile | cut -d: -f1`
sed -e "1,${markerloc}d" /tmp/tempfile > /tmp/filelist

this procedure has gaping holes in it as it does not check against any errors but can give you a good idea how to tackle the problem.

hope it helps
________________________________
UNIX because I majored in cryptology...
Sandman!
Honored Contributor

Re: Find command problem

You can get the GNU version of find which has a "-mmin" option that will search for all files that are less than specified minutes old. e.g.

# find -mmin x

this will search all files that are less than x minutes old.
Kent Ostby
Honored Contributor

Re: Find command problem

find . -depth -exec ll {} \; > outputfile

Then you can manipulate outputfile with grep (i.e. grep "Jun 24" outputfile ) to narrow it down a bit.
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Jayson B. Hurd
Advisor

Re: Find command problem

Thanks for the input. I'm starting to get there. I've managed to get files less than one day old to narrow it down... Question is, how would I then use the time piece of it and then evaluate current time against that time? So for example I now have a list of files, but I want to see if any of the files listed are less than a certain amount of minutes old....

Man this would be so easy if my 'find' command were new enough to use the -mmin command...

Any ideas?
Patrick Wallek
Honored Contributor

Re: Find command problem

Go back and look at Pete's response.

Say you want to find files with a time stamp after 1:30PM today

# touch -t 06231330 afile

(man touch for more info.)

# find /dir -newer afile

That will find files that are newer than the file afile.
Jayson B. Hurd
Advisor

Re: Find command problem

OK I'm on that - however I need it to be dynamic. I need it to touch a file that is xx minutes older than the time when the script runs...
A. Clay Stephenson
Acclaimed Contributor

Re: Find command problem

Sometimes it's hard to beat Perl. I would use Perl's File::Find for the whole thing but here's something that will create a touch timestamp spec for you - a string in the format CCYYMMDDhhmm.ss that can be -p seconds before or -n seconds after now.

Use it like this:

#!/usr/bin/sh
typeset TMSPEC=''
typeset TDIR=${TMPDIR:-/var/tmp}
typeset REFFILE=${TDIR}/X${$}_1.ref

TMSPEC=$(timestamp.pl -p 300) # 300 seconds before now
touch -m -t ${TMSPEC} ${REFFILE}

# your find stuff goes here

rm -f ${REFFILE} # clean up after yourself

Here's the Perl attachment, timestamp.pl;
If it ain't broke, I can fix that.
vinod_25
Valued Contributor

Re: Find command problem

Hi Hurd

To find files that were created in the last few
minutes. The time given is just a example on how the command will work.

#touch -t 0304290900 test

This will create a file with time stamp of 4/29/2003 and time of 9:00 AM.

#find /home -newer test

This will search /home for files that have a newer time stamp of 9:00 AM.
For example, if user /home/bob touched/edited a file at 9:01 AM on 4/29/03,
then the output would return the file that Bob modified.

Regards

Vinod K
Eknath
Trusted Contributor

Re: Find command problem

Hi Jayson,

I have a logic, if you are good in scripting

1. Get a list of files modified in a day by find command and store in todaysfiles

2. Grep list of files modified on hour basis
e.g. grep "Jun 22 22:" todaysfiles
will give you files modified between 22:00 to 22:59.

if you still want to fine tune, you can use awk command compare hour first and then compare time with separate variables.


Cheers!!!
eknath
Jayson B. Hurd
Advisor

Re: Find command problem

The perl script was the answer in combination with the -newer parameter on the find command. It appears that only the new gnu find version does -cmin (minutes) thereby necessitating the workaround.

Thanks to all!