Search for a file with Current Date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for a file with Current Date
# 1  
Old 05-20-2009
Search for a file with Current Date

Hi,

How can we search for a file with current date(sysdate).

Actually i want the file name from a directory which was created today(current date).

For example i have a dir /home/arch and i have files with name aglito03axyz.datetimestamp in this directory, but all these files were created on different dates. suppose we have 3 files :

dba May 18 00:06 aglito03axyz.20090518000506.arc
dba May 19 00:07 aglito03axyz.20090519000511.arc
dba May 20 00:07 aglito03axyz.20090520000506.arc

from the above we have 3 files with name aglito03axyz.datetimestamp and were created on different days, suppose i want the file created today(May 20th 2009) then i should get the file name as aglito03axyz.20090520000506.arc.

How can this be achieved.

Thanks
Sandeep
# 2  
Old 05-20-2009
Code:
find /home/arch -mtime 1

Padow
# 3  
Old 05-20-2009
I think Padow meant "find /home/arch -mtime 0" , though I don't think this will give you what you require either because it will still include files created yesterday.

Maybe try generating today's date and using it to match filenames:

Code:
cd /home/arch
YYYYMMDD="`date +%Y%m%d`"
ls -1 aglito03axyz.${YYYYMMDD}??????.arc 2>/dev/null | while read FILENAME
do
      echo "${FILENAME}"
done


Last edited by methyl; 05-21-2009 at 02:28 PM.. Reason: Typo
# 4  
Old 05-20-2009
methyl,

padow code works perfectly in my case.

If i use "find /home/arch -mtime 1" i am getting the file created today(May20th) and if i use "find /home/arch -mtime 0" i do not get any results.

But if we see the syntax of "find" command, if we use "-mtime 0" then it should give the files created today. I dont know why "0" is not working here.
# 5  
Old 05-20-2009
The -mtime parameter to "find" is in multiples of 24 hours. The results will depend on what time you run the command relative to when the files were last changed. I still think that find is not the right program for this job.
# 6  
Old 05-21-2009
methyl,

You are right -mtime does not always work. The solution you suggested was to search for the file name which contains date.

I do not want to go in that way.

Is there any solution where we can find for a particular file which was created today.( I do not want to go by searching the filename).

Thanks
# 7  
Old 05-21-2009
Another approach. Create a file dated exactly midnight and find files newer than that file.

Code:
#!/bin/ksh
cd /home/arch
REF_DATE=/tmp/my_reference_date.$$
YYYYMMDD="`date +%Y%m%d`"
SEARCH='aglito03axyz.??????????????.arc'
touch -t "${YYYYMMDD}0000.00" ${REF_DATE}
find . -type f -name "${SEARCH}" -newer ${REF_DATE} -print | while read FILENAME
do
      echo "${FILENAME}"
done
#
rm ${REF_DATE}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. UNIX for Beginners Questions & Answers

“sed” replace date in text file with current date

We want to call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ Code: line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to write a... (3 Replies)
Discussion started by: pradeepp
3 Replies

3. Shell Programming and Scripting

Subtract a file's modification date with current date

SunOS -s 5.10 Generic_147440-04 sun4u sparc SUNW,SPARC-Enterprise Hi, In a folder, there are files. I have a script which reads the current date and subtract the modification date of each file. How do I achieve this? Regards, Joe (2 Replies)
Discussion started by: roshanbi
2 Replies

4. Shell Programming and Scripting

Get file name which include the current date

Hi, I'm trying to get the name of a file that has the current date in the name. The filename is on the form A<current date>01.DC, for example A2012110501.DC I have this script so far date +%y%m%d D=$(date +%y%m%d) N=A20$D echo $N N2={$N}01.DC echo $N2 And gets the following... (12 Replies)
Discussion started by: Immelstorn
12 Replies

5. Debian

Search files that start with current date

hi all, i need to find same files in my directory that looks like this format 20121017145949639.xml (YYYYMMDD*.xml) thanks for help (3 Replies)
Discussion started by: merouan
3 Replies

6. Shell Programming and Scripting

current date modified file

Hi , In my directory , i have many days file but i want to see all those which are of todays date. i tried this but it gives all the files mtime -0 |ls -ltr I tried the below option as well. 19635 find -iname "*.LOG" -mtime 19636 ls -ltr *.LOG -mtime -1 19637 ls -ltr *.LOG... (7 Replies)
Discussion started by: guddu_12
7 Replies

7. Shell Programming and Scripting

Script to Search Logs Several Directories Pulling out Only Current Date

Hi All.. I'm seeking assistance with editing a script to search log files in several directories. I'm close to what I'm seeking, but need additional guidance. The log files are always listed by current date; however, inside the log file includes dates that go back to 2011. What I'm... (6 Replies)
Discussion started by: lenaf7
6 Replies

8. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

9. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies

10. Shell Programming and Scripting

File date vs Current date

I am a newbie in shell scripting, hope you can advice how can I compare the date/time of file extracted from 'll' and current system date/time. I have done the following: ll -rt > $FILE_AGE_LOG FILE_DATETIME=`more $FILE_AGE_LOG | head -02 | cut -c 45-57` It returns 'May 4 19:11'. If I... (4 Replies)
Discussion started by: trexlim
4 Replies
Login or Register to Ask a Question