Operating System - HP-UX
1753970 Members
7226 Online
108811 Solutions
New Discussion юеВ

Re: Question on searching files by date stamp.

 
SOLVED
Go to solution
Mike_305
Super Advisor

Question on searching files by date stamp.

Hi,

I am trying to search file in one of the directory by date stamp (for ex. Jun 12) and ll | grep -i and using sed it's not working. Any idea on how to do this.

Thanks in advance.

Mike
If there is problem then don't think as problem, think as opportunity.
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: Question on searching files by date stamp.

Hi Mike:

# ls -l /pathname|grep -i "Jun 12"

...works for me. Did you double quote the token for 'grep' to match upon?

Regards!

...JRF...
Ken Grabowski
Respected Contributor

Re: Question on searching files by date stamp.

The statement James used will work, but there is a completely different approach you can use but may not have considered. Try:

find ./ -type f -mtime 51 -exec ls -l {} \;

It may look ugly on the surface but it is much better for finding one or more files of a specific age.
James R. Ferguson
Acclaimed Contributor

Re: Question on searching files by date stamp.

Hi (again) Mike:

Well, using 'find's 'mtime' argument means that you have to calculate the number of days (24-hour increments) old the target date is from now. A better way, if you want to take this approach is to create two reference points (boundries):

# touch -amt 06112359 /tmp/f1
# touch -amt 06130000 /tmp/f2
# find /pathname -xdev -type f -newer /tmp/f1 -a ! -newer /tmp/f2

This finds all *files* in '/pathname' that are newer than boundry-1 (based on modification time) and younger (not newer) than boundry-2 (file #2's modification time).

The '-xdev' switch prevents crossing mountpoints.

See the manpages for 'touch' and 'find' for more information.

Regards!

...JRF...
Mike_305
Super Advisor

Re: Question on searching files by date stamp.

Hi,

I know why the ll| grep -i "Jun 1" did not work for me because the space was incorrect between "Jun & the 1" and now I know what I was doing was right. If you have any other way to search file in directory by date stamp that will be great.

For now the ll | grep -i "Jun 1" work. Any other ideas will help.

Basically I am just trying to find out how to search files in directory that has the date stamp of (for example "Jun 1").


Thanks,

Mike
If there is problem then don't think as problem, think as opportunity.
Bill Hassell
Honored Contributor

Re: Question on searching files by date stamp.

And a very important consideration: the ll command switches to a different date format for files older than about 6 months. Then your script will completely fail. Only the find command will work for any date.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: Question on searching files by date stamp.

Hi MIke:

As Bill and I have emphasized, using 'find' is a more exact approach. For example, do you want "Jun 12" only if it is within the current 6-months or do you want any "Jun 12" from any year.

If you don't care, and you want to use 'grep', then a way to find matches for dates like "Jun1" OR "Jun1" would be:

grep "Jun[ ]*1[ ]"

Regards!

...JRF...
Mike_305
Super Advisor

Re: Question on searching files by date stamp.

Thanks for all your help.

Mike
If there is problem then don't think as problem, think as opportunity.