Summary of "listing files bet 2 dates"
Manoj Joshi
manoj at hpldsla.sid.hp.com
Fri Oct 26 03:49:33 AEST 1990
I got a lot of interesting mail from people on the net which was
very helpful. To summarise, the general opinion was that `find` was the
closest way to solve this problem. I found that somehow the mtime
and ctime options of find are not to accurate. Besides, I found that
I had to write a shell script anyway which feeds the dates to
find. So I decided instead to write a shell script which scans through the
files in a dir, and uses a c program to check if the file matches the
time interval condition. The performance was great, it's lightning
fast on my 12 mip w/s. Anyway, I have included these here for people's
curiosity. I can make the script and the program more efficient
but here it is only on an experimental basis. (For instance, I think
the comparison loop should exit as soon as the first mismatch is found
since, the -t option of ls ensures the files are scanned in the
chronological order.)
With this script I could archive files between two dates. And it is
accurate about that. It will not be too hard to implement exact times.
In the long run, I would probably put the whole stuff including listing
the files in the same program.
/*********************************************************************/
/* EXAMPLE LISTING */
/*********************************************************************/
#!/bin/ksh
# Declaration of variables
hostName=$1
instName=$2
fromDate=$3
toDate=$4
selectList=""
searchDir=$DATA_DIR/$hostName/hpchem/$instName/data
currentDir=`pwd`
cd $searchDir
list=`ls -Rrt`
for currentFile in $list
do
$currentDir/selectFile $searchDir/$currentFile $fromDate $toDate
result=$?
if [ result -eq 0 ]
then
selectList="$selectList $currentFile"
fi
done
echo tar -cvf /dev/update.src $selectList
cd $currentDir
/*********************************************************************/
/* EXAMPLE LISTING */
/*********************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
int selectFile(file, fromDate, toDate)
char* file;
char* fromDate;
char* toDate;
{ /* selectFile */
struct stat *buf;
struct tm *timeptr1;
struct tm *timeptr2;
time_t ftime;
time_t ttime;
int fDate = (int)atoi(fromDate);
int tDate = (int)atoi(toDate);
buf = (struct stat *)malloc(sizeof(struct stat));
memset(buf, '\0', sizeof(struct stat));
if (stat(file, buf))
{
perror("stat");
return(1);
};
timeptr1 = (struct tm *)malloc(sizeof(struct tm));
memset(timeptr1, '\0', sizeof(struct tm));
timeptr1->tm_sec = 0;
timeptr1->tm_min = 0;
timeptr1->tm_hour = 0;
timeptr1->tm_mday = fDate%100;
timeptr1->tm_mon = (fDate/100)%100-1;
timeptr1->tm_year = (fDate/10000)%100;
timeptr1->tm_wday = 0;
timeptr1->tm_yday = 0;
timeptr1->tm_isdst = -1;
timeptr2 = (struct tm *)malloc(sizeof(struct tm));
memset(timeptr2, '\0', sizeof(struct tm));
timeptr2->tm_sec = 59;
timeptr2->tm_min = 59;
timeptr2->tm_hour = 23;
timeptr2->tm_mday = tDate%100;
timeptr2->tm_mon = (tDate/100)%100-1;
timeptr2->tm_year = (tDate/10000)%100;
timeptr2->tm_wday = 0;
timeptr2->tm_yday = 0;
timeptr2->tm_isdst = -1;
if ((ftime = mktime(timeptr1)) == (time_t)-1)
{
perror("mktime");
return(1);
};
if ((ttime = mktime(timeptr2)) == (time_t)-1)
{
perror("mktime");
return(1);
};
#ifdef TEST
printf("From Date : %s\n", asctime(timeptr1));
printf("To Date : %s\n", asctime(timeptr2));
printf("File Date : %s\n", asctime(localtime(&(buf->st_mtime))));
#endif
if ((buf->st_mtime >= ftime) && (buf->st_mtime <= ttime))
return(0);
else return(1);
} /* selectFile */
int main(argc, argv)
int argc;
char** argv;
{ /* main */
if (argc != 4)
{
fprintf(stderr,
"Usage: selectFile <file> <fromDate(YYMMDD)> <toDate(YYMMDD)>\n");
exit(1);
};
if (!(selectFile(argv[1], argv[2], argv[3])))
{
#ifdef TEST
printf("%s not selected\n", argv[1]);
#endif
return(0);
}
else return(1);
} /* main */
More information about the Comp.unix.questions
mailing list