getting files between specific date ranges in solaris

hi !
how can i get files in a directory between certain date ranges ?
say all files created/modified between Jan24 - Jan31

thanks

you can use the find command with the mtime option, you'll just have to do some calculations of how many days ago that is.

find /directorypath -mtime +1 -mtime -7

What's your system? What's your shell? Different systems have different ways of dealing with dates.

my shell is Ksh and iam on sun solaris 10.

is there a way to use dates instead of +/- days ?

If you were on Linux I'd use GNU date to get epoch times from dates, find to print them, and awk to compare them, but Solaris doesn't have the first two.

Do you have Perl?

Yes.
One Shell method is to create two reference files with "touch". One dated the last second before the range, and one dated the first second after the range. Then use "find".

find /dir -type f \( -newer start_file -a ! -newer end_file \) -print

There is a similar approach using reference files and the little-known "ksh" Conditional Expressions "-nt" and "-ot" but it tends to be slower than "find".

1 Like

so if i want to search between 24th and 30th jan , I create two files one dated 23rh jan and one dated 31st jan n search ?
how do i created these files with touch ?

Filenames are for example only. You will probably want to place them somewhere outside of the tree you will be searching!
Not tested on Solaris 10, but "touch" is pretty consistent across unix platforms.

touch -t 201201232359.59 start_file
touch -t 201201310000.00 end_file

ls -la *file
-rw-r--r--   1 root       sys              0 Jan 31 00:00 end_file
-rw-r--r--   1 root       sys              0 Jan 23 23:59 start_file

These file dates match you most recent post, not post #1.
You can of course create test files with assorted dates to enable you to test your search script.

I found this on web simliar for what you guys are telling me and it worked pefect

#!/usr/bin/bash
printf "Enter start date( YYYYMMDD ):" 
read startdate 
printf "Enter end date( YYYYMMDD ):" 
read enddate 
touch -t "${startdate}0000.00" sdummy 
touch -t "${enddate}0000.00" edummy 
for fi in * 
do     
   if [ $fi -nt "sdummy" -a ! $fi -nt "edummy" ] ;then         
     echo ls -al $fi         
   fi 
done
#! /bin/bash

st_dt=$1
nd_dt=$2

for file in `ls | grep -v "^d"`
do
    f_mod_tm=`stat -c%y $file | cut -d. -f1 | sed 's/[ -:]//g'`
    if [ $f_mod_tm -ge $st_dt ]  && [ $f_mod_tm -le $nd_dt ]
    then
        echo $file
    fi
done

./test.sh <start_dt_ddmmyyhhmmss> <end_dt_ddmmyyhhmmss>

$ ls -lrt
total 40
drwxrwxr-x 2 linux linux 4096 2012-01-30 05:28 test
-rw-rw-r-- 1 linux linux   49 2012-01-31 06:23 file1~
-rw-rw-r-- 1 linux linux   20 2012-02-01 06:13 file1
-rw-rw-r-- 1 linux linux   45 2012-02-01 06:13 file2
-rwxr--r-- 1 linux linux   67 2012-02-01 21:21 pinger.sh
-rw-rw-r-- 1 linux linux  299 2012-02-01 22:25 input
-rwxr--r-- 1 linux linux  330 2012-02-01 22:27 test.pl~
-rwxr--r-- 1 linux linux  335 2012-02-01 22:30 test.pl
-rwxr--r-- 1 linux linux  239 2012-02-02 05:55 test.sh~
-rwxr--r-- 1 linux linux  240 2012-02-02 05:55 test.sh
$
$ ./test.sh 20120130000000 20120201000000
file1~
test

@aliyesami
The script you found on the web contains basic scripting errors.

Never use this construct for open-ended lists of files. It breaks if any filename contains a space character and it breaks the Shell if there are too many files.
The shebang line is for "bash" when you have "ksh".
The script does not check the type of inode. If it comes across a directory it will break.
The script logic will ignore anything created on the end date.
The script creates the reference files in the directory you are searching.
The line starting "echo" stops the "ls" executing.

The "find" approach is best.

@balajesuri
The script you post is interesting but the O/P has "ksh" not "bash" and probably won't have a Linux "stat" command.
The script contains similar errors to that posted by the O/P.

This will ignore every file with a name starting with a lower case "d". It will not ignore directories. Never use "for" to process a directory list.

I'm still searching for any course, book or manual which advocates using "for" with an open-ended list of filenames. This construct must have come from somewhere!

1 Like