UNIX: Search file between two Date stamps


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX: Search file between two Date stamps
# 1  
Old 01-24-2013
UNIX: Search file between two Date stamps

Hi,

I have a requirement to find a file based on date stamp provided by user. In one of the shared location I get data file say “datafile<dataformat>“. And User will provide the date based on which I need to select a file which is 7 days older, if a file not present within 7 days, then I need to check for a file which is 14 days old from the date provided and the process continue for 21 days than 28 days till a file is found.

More information:
OS: SunOS xxxxxxxxxxx 5.10 Generic_138889-03 i86pc i386 i86pc
Example of file available in the shared location is given below.

datafile.20121124-03h22.zip
datafile.20121201-03h16.zip
datafile.20121208-03h24.zip

Last edited by santhosh86467; 01-24-2013 at 05:43 AM.. Reason: To give more information
# 2  
Old 01-24-2013
Just to give you an idea, you can use find command with -newer option to find files between two dates.

For example, if you want to find files created between 10th Dec 2012 & 15th Dec 2012

1. Touch 2 files with starting & ending timestamps:
Code:
touch -t 201212100000 beg
touch -t 201212150000 end

2. Run find to list files between that range:
Code:
find . -name "datafile*" -newer beg ! -newer end

I hope this helps.
# 3  
Old 01-24-2013
Here is a perl program to add 7 days to a date, (save as 7days.pl)

Code:
#!/usr/bin/perl
use Time::Local 'timelocal';
use POSIX qw(strftime);
my $M, $year, $month, $day;
my @d, @f;
while (my $ln = <STDIN>) {
  if($ln =~ /(\d\d\d\d) *(\d\d) *(\d\d)/) {
    $ln =~ s/(\d\d\d\d) *(\d\d) *(\d\d)/\1 \2 \3/;
    push(@f, $ln);
    $M=$ln unless ($ln lt $M);
  }
}
($year, $month, $day) = split(/\s/, $M);
print strftime("%Y%m%d\n", localtime(timelocal(0,0,0,$day,$month-1,$year)+7*24*60*60));


You can use it in a script something like this:

Code:
START=20121124
END=$(echo $START | ./7days.pl)
 
while [ -n "$FOUND" ]
do
    for file in datfile*
    do
        DATE=$(echo $file | sed -e 's:^datfile.::' -e s':-.*::')
        [ $DATE -ge $START -a $DATE -le $END ] &&
            FOUND="$(printf "%s\n%s" $FOUND $file)"
    done
    END=$(echo $END | ./7days.pl)
done
echo "Files found:"
echo "$FOUND"

# 4  
Old 01-25-2013
Thank you so much bipinajith and Chubler_XL . I used the unix find command now and it looks fine.. here is my code
Code:
findFile () {
   INPUTDATE=$1

 
 
   YY=`echo $INPUTDATE | cut -f 1 -d "/" | cut -f 1 -d " "`
   MM=`echo $INPUTDATE | cut -f 2 -d "/" | cut -f 1 -d " "`
   DD=`echo $INPUTDATE | cut -f 3 -d "/" | cut -f 1 -d " "`
   echo "INPUT FILE ${YY}${MM}${DD}0000"
   touch -t ${YY}${MM}${DD}0000 ${DB_DUMPS_DIR}/DBREFRESHDATE
   if [[ $DD -lt 8 ]]
   then
      if [[ $MM -eq 1 ]]
      then
         (( MM -= 1 ))
         (( MM += 12 ))
         (( YY -= 1 ))
      else
         (( MM -= 1 ))
         if [[ $MM -lt 10 ]]
         then
            MM=0$MM
         fi
      fi
      (( DD -= 7 ))
      (( DD += 30 ))
   else
      (( DD -= 7 ))
      if [[ $DD -lt 10 ]]
      then
         DD=0$DD
      fi
   fi

   echo  "NEW FILE ${YY}${MM}${DD}0000"
   touch -t ${YY}${MM}${DD}0000 ${DB_DUMPS_DIR}/DBDUMPDATE
   cd ${DB_DUMPS_DIR}
   find . -newer DBDUMPDATE ! -newer DBREFRESHDATE | egrep -v DBREFRESHDATE
   if [[ $? -eq 0 ]]
   then
      echo "Found data"
      FOUND=1
   else
      echo "Could not find data"
      NEWDATE=${YY}/${MM}/${DD}
      FOUND=0
   fi
}


Last edited by Franklin52; 01-25-2013 at 05:42 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search file for a date variable?

Hello, I'm trying to write a ksh script which will allow me to to search for yesterday's date within a rtf file. I just need someway to know if the date is in the file, echo results to a text file, and then mail that out. The format of the date is mm/dd/yyyy. I had to make a variable... (2 Replies)
Discussion started by: ronan1219
2 Replies

2. Shell Programming and Scripting

date time stamps in bash

I'm looking for a way to have the "date" command output the date in a specific format. I'm not familiar with the different ways to use the date command at all. i read up on it, but i dont get how to manipulate it. i know that i can get the date format to give me a format like: 2012-10-13... (6 Replies)
Discussion started by: SkySmart
6 Replies

3. Shell Programming and Scripting

How to search log file from a date

Hi All, I want to grep through all the log files created from 20th August. How do I do that? My script will run everyday. So it will take current date as To_Date. My log file name looks like - applog-2012-08-20-000001....applog-2012-08-20-000002...etc. Thanks in advance. (5 Replies)
Discussion started by: kmajumder
5 Replies

4. Shell Programming and Scripting

How to search for file and display by date

I like "ls -ltr". I would like to search for a file in a large directory recursively and and display all the candidates in reverse order. /usr/bin/find . -name \*.txt This works. How do I display the date and sort the file names by the date in reverse order for the entire directory... (1 Reply)
Discussion started by: siegfried
1 Replies

5. Shell Programming and Scripting

Perl - Search file between two time stamps

Hi, I am on a windows system that receives files from differnet host, I have to find the count of files that we receive between specific time. The code below is what i use to count the number of file, how do i change this to count files between specfic time opendir ( DIR, $dir ) || die... (1 Reply)
Discussion started by: amit1_x
1 Replies

6. UNIX for Dummies Questions & Answers

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... (7 Replies)
Discussion started by: sandeep_1105
7 Replies

7. UNIX for Dummies Questions & Answers

Adding Date & time stamps to filename

I need to edit the file name with date and time while writing the script. please help. (1 Reply)
Discussion started by: manish.s
1 Replies

8. UNIX for Dummies Questions & Answers

List files with date and time stamps only

Hi there, I'm using terminal on mac and using the ls -l command to list all the files in a directory. However, I only want to display the date and time stamp of each file rather than permissions, owner, group etc... Is this possible? Many thanks in advance Dave (2 Replies)
Discussion started by: davewg
2 Replies

9. UNIX for Dummies Questions & Answers

Changing Creation Date to a Prespecified Date of a File In Unix

Dear Expert, Is there a command to do that in Unix? In such a way that we don't need to actually "write" or modified the content. -- monkfan (4 Replies)
Discussion started by: monkfan
4 Replies

10. UNIX for Advanced & Expert Users

find file with date and recursive search for a text

Hey Guyz I have a requirement something like this.. a part of file name, date of modification of that file and a text is entered as input. like Date : 080206 (MMDDYY format.) filename : hotel_rates text : Jim now the file hotel_rates.ZZZ.123 (creation date is Aug 02 2006) should be... (10 Replies)
Discussion started by: rosh0623
10 Replies
Login or Register to Ask a Question