How to extract latest file by looking at date time stamp from a directory?

hi,

i have a Archive directory in which files are archived or stored with date and time stamp to prevent over writing.

example:
there are 5 files

s1.txt
s2.txt
s3.txt
s4.txt
s5.txt

while moving these files to archive directory, date and time stamp is added.
of format `date +%Y%m%d`.`date +%H%M%S%N`

s1.txt.20130430.1412267000      
s2.txt.20130430.1412261233
s3.txt.20130430.1412261322
s4.txt.20130430.1412263211
s5.txt.20130430.1412263244

i want to extract the latest files which came in the last 5 mins. can any1 help me with a script.

please let me know if you need more clarification

Thanks

If you simply want to find all the files which arrived in a directory in last 5 minutes or modified in last 5 minutes you can use find.

 
find . -type f -mmin -5
1 Like

thanks..

till now this command works fine. will ask again if some problem comes.

but if i want the latest files that came in last 1 hour or last 2 hours. then what i have to write in -mmin parameter?

-mmin evalutes in minutes so you can use either -60 (1 hour) or -120 (2 hour) etc..

can tou explain me the above command?

what does (dot and f) represents?

Is it like dot represents the current directory??

dot - represents current directory
f - regular file

Yes, . means the current directory, and means "find in current directory and subdirectories".

The f is part of "-type f" and means "find regular files".

The -mmin -5 part was covered before.

the above command will find all the files. but if the directory contains two types of files

abc.txt.pdf
def.txt.pdf

abc.txt.pg
def.txt.pg

and i want to find only .pdf extension files. then where should i specify the *.pdf pattern?

then give the file extn also

 
find . -type f -name *.pdf -mmin 5
1 Like

The correct form is:

find . -type f -name "*.pdf" -mmin -5

The other way with just *.pdf (no quotes) will not work, because *.pdf will be expanded by the shell.

1 Like