Operating System - HP-UX
1821641 Members
3335 Online
109633 Solutions
New Discussion юеВ

Re: How to find file newer than another while limiting the find criteria...

 
SOLVED
Go to solution
jmckinzie
Super Advisor

How to find file newer than another while limiting the find criteria...

Hello,

I need to find all files within a specific directory that contain the extension *.out that are newer than Jan 1, 2007.

To begin, I created a tmp file called jan 1 in the directory using touch witha DTS of jan 1 2007 0001.

Now, I am trying to do my find like this:

find . -newer jan1 -print > /tmp/out '*.out'

but it isn't working....
I get this error

find: missing conjunction
any ideas here?
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to find file newer than another while limiting the find criteria...



find . -type f -name '*.out' -newer jan1 > /tmp/out


You really should be using /tmp for user temporary files. /tmp is reserved for the OS; use /var/tmp instead.
If it ain't broke, I can fix that.
jmckinzie
Super Advisor

Re: How to find file newer than another while limiting the find criteria...

This may sound simple but how do i get it to print the apsolute path vice the relative one? I remember it has something to do with basename but cannot remember specifically.
James R. Ferguson
Acclaimed Contributor

Re: How to find file newer than another while limiting the find criteria...

Hi Jody:

> This may sound simple but how do i get it to print the apsolute path vice the relative one?

The simplest way is to specify the absolute path instead of the "." (relative path):

# find /path -newer jan1 -name "*.out" > /tmp/out

Regards!

...JRF...
jmckinzie
Super Advisor

Re: How to find file newer than another while limiting the find criteria...

Thanks all...exactly what I needed.

1. touch -t 200701010001 jan1
2.find APSOLUTEPATH -type f -name '*.out' -newer APSOLUTEPATH/jan1 > /var/tmp/out

3. Then i wrote a for loop to copy files...

for i in $FILE; do cp -p $i destdir; done
A. Clay Stephenson
Acclaimed Contributor

Re: How to find file newer than another while limiting the find criteria...

You aren't quite finished. You need to remove your temporary files; ideally this would be done using a trap statement. Note that a trap 0 means on exit so the trap would do cleanup upon normal exit as well as upon abnormal exit conditions.
If it ain't broke, I can fix that.
jmckinzie
Super Advisor

Re: How to find file newer than another while limiting the find criteria...

Stephen,

Could you please provide an example?
jmckinzie
Super Advisor

Re: How to find file newer than another while limiting the find criteria...

Sorry,

I meant Clay....
A. Clay Stephenson
Acclaimed Contributor

Re: How to find file newer than another while limiting the find criteria...

Okay something along these lines:

#!/usr/bin/sh

typeset TDIR=${TMPDIR:-/var/tmp}

typeset T1=${TDIR}/Timestamp_${$}_1
typeset T2=${TDIR}/Out_${$}_2

trap 'eval rm -f ${T1} ${T2}' 0 1 2 15
touch -t 200701010001 ${T1}
find ABSOLUTEPATH -type f -name '*.out' -newer ${T1} > ${T2}
...
...

Note that your temporary files now observe the TMPDIR convention so that if TMPDIR is set, it is used and if not it defaults to /var/tmp. The temporary files have been changed to use the PID as part of the filename so that multiple runs of the same script can happily coexist although you probably should pass the touch date as an argument.


If it ain't broke, I can fix that.