Re: help: newer option in find (99 Views)
Reply
Advisor
Rinky
Posts: 27
Registered: ‎05-03-2007
Message 1 of 12 (99 Views)
Accepted Solution

help: newer option in find

[ Edited ]

Hi,

In a given directory i want to find out the latest file(the file will be of the format filename.YYYYMMDDHHISS), store its time stamp in a file. Next day when i run my script i have to get all the recent files to the time stamp stored in that file. Can this be done using newer option in find. Please suggest any other options if available

Thanks in advance

 

 

 

Moved from HP-UX Technical Documentation to HP-UX > languages

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,385
Registered: ‎03-06-2006
Message 2 of 12 (99 Views)

Re: help: newer option in find

When you find that file, you can remember the name or touch a reference file:
touch -r saved-filename ref_file
Then use find:
find . -newer ref_file
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 12 (99 Views)

Re: help: newer option in find

Hi Rinky:

Dennis' solution is fine, except I would urge you to add the '-xdev' and '-type f' switches and arguments:

# find -xdev . -type f -newer ref_file

The '-xdev' prevents crossing mountpoints. This becomes particularly important if you want to search '/' where you might want to include directories like '/etc' and '/sbin' but not visit directories like '/var' and '/opt'.

Specifying '-type f' limits selection to only *files* and not directories.

Regards!

...JRF...
Please use plain text.
Respected Contributor
Deepak Kr
Posts: 424
Registered: ‎07-11-2008
Message 4 of 12 (99 Views)

Re: help: newer option in find

Please use following:

find /directory_path -xdev -type f -newer "file_name" -exec ls -lrt {} \;

where file_name the latest time stamp file you recorded last.

"There is always some scope for improvement"
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,385
Registered: ‎03-06-2006
Message 5 of 12 (99 Views)

Re: help: newer option in find

>Kumar: find /directory_path -xdev -type f -newer "file_name" -exec ls -lrt {} \;

Using -rt is usless if you use ";". You need to use "+" to get more than one file:
... -exec ll -rt {} +
Please use plain text.
Advisor
Rinky
Posts: 27
Registered: ‎05-03-2007
Message 6 of 12 (99 Views)

Re: help: newer option in find

Hi all
Thanks for the response.
Iam afraid i cannot touch the files as they are in production. Please let me know if i can save the time stamp in some temporary file and make use of that time stamp in finding the newer file. Also if there are files with same time stamp, will they be considered when newer is useD?
Please use plain text.
Respected Contributor
Deepak Kr
Posts: 424
Registered: ‎07-11-2008
Message 7 of 12 (99 Views)

Re: help: newer option in find

Thanks Dennis,

Noted.

"There is always some scope for improvement"
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 12 (99 Views)

Re: help: newer option in find

Hi (again) Rinky:

> Iam afraid i cannot touch the files as they are in production. Please let me know if i can save the time stamp in some temporary file and make use of that time stamp in finding the newer file.

You are not altering the data or the metadata (ownership, permissions or timestamps) of any production files, unless you consider updating the lastaccess timestamp of directories as they are searched (which would happen anytime someone did an 'ls' anyway!).

You simply create a reference file in a directory of your choice:

# touch -mt 200807310000 /var/tmp/myref
# find /apps -xdev -type f -newer /var/tmp/myref

Another variation as Dennis suggested is to create your reference file with the same timestamp as a production one. Here again, the production file is not altered in any way:

# touch -r /etc/hosts /var/tmp/myref

...which creates '/var/tmp/myref' with a modification timestamp ('mtime') equal to that of '/etc/hosts'.

> Also if there are files with same time stamp, will they be considered when newer is useD?

If the 'mtime' of a file is exactly equal to the second, no. "Newer" means newer.

Notice that the 'touch' can include seconds of time:

# touch -mt 200807310001.00 /var/tmp/myref

You can find files within ranges of time by doing:

# touch -mt 200807010000.00 /var/tmp/myref1
# touch -mt 200807312359.59 /var/tmp/myref2
# find /path -xdev -type f -newer /var/tmp/myref1 -a ! -newer /var/tmp/myref2 -exec ls -l {} +

This finds files in '/path' (your choice) that have been modified sometime during the month of July 2008. The '!' means "not" while the '-a' means "and".

Regards!

...JRF...
Please use plain text.
Advisor
Rinky
Posts: 27
Registered: ‎05-03-2007
Message 9 of 12 (99 Views)

Re: help: newer option in find

Hi JRF ,
Thanks for your inputs. Iam using the command as follows
find . -xdev -type f -newer *.$tim

where tim like 20080216110333

Iam storing that tim variable in a file and using that in my query. My files are of the format ?????.TN??????.ABC.200808040930. But if two files have the same time stamp , iam facing an error
find: missing conjunction
Is there any way to over come this error?
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,385
Registered: ‎03-06-2006
Message 10 of 12 (99 Views)

Re: help: newer option in find

>I am storing that tim variable in a file and using that in my query.

Instead of storing a tim variable, store the complete file name. Or as I said, create a reference file, then you don't need to store anything.

>But if two files have the same time stamp, I am facing an error
>Is there any way to overcome this error?

Try using head(1) to pick the first:
find . -xdev -type f -newer $(ls *.$tim | head -1)
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 11 of 12 (99 Views)

Re: help: newer option in find

Hi (again) Rinky:

> Iam storing that tim variable in a file and using that in my query

This isn't going to work the way you are doing it. Re-read my last post. You need to create a dummy "reference" file (with 'touch') to supply as the argument to '-newer' in your 'find'. When you do an 'ls -l' of the reference file, its timestamp (not its name!) should represent the time and date of interest.

Regards!

...JRF...
Please use plain text.
Advisor
Rinky
Posts: 27
Registered: ‎05-03-2007
Message 12 of 12 (99 Views)

Re: help: newer option in find

Thanks all :)
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation