Search files between date ranges - Ctime usage

Hello,
I am a noob and need some help.
I am trying to find files created between a date range.

For Example:
These are files in directory.

-rw-r--r--    1 user staff             6 May  8 09:43 file1.txt
-rw-r--r--    1 user staff             6 May  8 09:43 file2.txt
-rw-r--r--    1 user staff             6 May  8 15:52 file3.txt
-rw-r--r--    1 user staff             6 May  8 15:52 file4.txt

sysdate: 05/12/2017 11:50 AM
I need all the files that got created on 05/08/2017 (irrespective of what time they got created on that date).
I am using below code

find . -ctime +4 -ctime -5

and this gives me only files file1 and file2.

My requirement is to get all 4 files in this case.
Appreciate your help.

Maybe

LC_ALL=C ls -lo |
while read x x x x month day x file
do
  if [ "$month" = "May" ] && [ "$day" = "8" ]
  then
    printf "%s\n" "$file"
  fi
done

You have a few issues that keep you from reaching your goal:

  1. Most filesystems do not keep track of when a file was created; the struct stat st_ctime field stores the timestamp of the last file status change. A file's status changes when a file is created, when a file's size changes (by truncating the contents of the file or by writing data to a position greater than the file's current size), or by changing the file's mode or access permissions.
  2. The find -ctime primary doesn't work on calendar dates; it works on multiple of 24 hours from the time you invoke find .

To find files whose last file contents modification time occurs on a given calendar date (or other period of time unrelated to when you invoke find ), use touch to create one file with timestamps corresponding to the start time of your desired time period ( startTimeFile in this example) and create another file with timestamps corresponding to the end time of your desired time period ( endTimeFile ) and then use:

find . -newer startTimeFile ! -newer endTimeFile
1 Like

Does the find command on your system (the details of which you fail to mention) offer the -daystart option?

Hi Don, Thanks for the reply.
The solution you provided is working perfectly for this scenario.

We also have a requirement where the files are placed in our directory on a daily basis.
In this case, we want the choose and process the file based on a date (placed in our directory) range. The solution you provided is not working in this case as it looking at the file creation date.

As I said before, most filesystems DO NOT STORE a file creation date for files and there is no way to determine the date on which a file was created using standard interfaces. If the last modification timestamp of a file is insufficient for you needs, you need to:

  1. make sure that you are storing these files on a filesystem type that keeps track of file creation dates, find an operating system specific way to look at the file's creation date for files on that filesystem, and find a way to use that operating system specific way of examining files to select the files you want to process,
  2. include the file creation date in the pathnames of your files and select files to process based on the date in each file's pathname, or
  3. put unprocessed files in a separate directory and move them to a different directory after they have been processed.

Since you haven't told us what operating system AND filesystem type you're using, we have no way to guess whether or not what you want is possible with method 1. You can use method 2 or 3 on any operating system.

1 Like

I am not sure of the filesystem type but we are using AIX OS.

I see the below dates available for a file. Is there a way we can use Find command to check by the date of last modification of i-node (May 12 in below).

$ ls -l file1.dat
-rw-r--r--    1 user staff             6 May  8 09:40 file1.dat
$ ls -lc file1.dat
-rw-r--r--    1 user staff             6 May 12 14:34 file1.dat

As

find . -newer startTimeFile ! -newer endTimeFile

is looking at May 8 in above example

Yes. Do exactly what I suggested in post #3:

#!/bin/ksh
trap 'rm -f /tmp/startTimeFile.$$ /tmp/endTimeFile.$$' INT QUIT TERM HUP EXIT
touch -t 201705120000 /tmp/startTimeFile.$$
touch -t 201705122359.59 /tmp/endTimeFile.$$
find . -newer /tmp/startTimeFile.$$ ! -newer /tmp/endTimeFile.$$

The above code assumes that your current working directory is not /tmp . You want to create your timestamp range files somewhere that will not be searched under the list of file hierarchy roots given as operands to find .

I tried the exact code you gave (20170512) but it did not find file1.dat.

But if I create the files like below (20170508) then it is able to find file1.dat

touch -t 201705080000 /tmp/startTimeFile.$$
touch -t 201705082359.59 /tmp/endTimeFile.$$