Operating System - HP-UX
1748022 Members
4832 Online
108757 Solutions
New Discussion юеВ

Re: help: newer option in find

 
SOLVED
Go to solution
Rinky
Advisor

help: newer option in find

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

11 REPLIES 11
Dennis Handly
Acclaimed Contributor

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
James R. Ferguson
Acclaimed Contributor

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...
Deepak Kr
Respected Contributor

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"
Dennis Handly
Acclaimed Contributor
Solution

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 {} +
Rinky
Advisor

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?
Deepak Kr
Respected Contributor

Re: help: newer option in find

Thanks Dennis,

Noted.

"There is always some scope for improvement"
James R. Ferguson
Acclaimed Contributor

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...
Rinky
Advisor

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?
Dennis Handly
Acclaimed Contributor

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)