1

I want to find out the files which changed between some interval. Script I am using is

#!/bin/bash
find ./ -type f -newermt '2018-05-24 09:26:50' ! -newermt '2018-05-24 09:26:52'

My folder contains the following files:

-rwxr-xr-x 1 root root  219 May 24 09:26 sql_runner.sh
-rwxr-xr-x 1 root root 3.6K May 24 09:26 script.sh
-rwxr-xr-x 1 root root 3.1K May 24 09:26 script_ide.sh
-rwxr-xr-x 1 root root 8.8K May 24 09:26 q
-rw-r--r-- 1 root root   17 May 24 09:26 program.txt
-rw-r--r-- 1 root root  346 May 24 09:26 main.c
drwx------ 2 root root  12K May 24 09:26 lost+found
-rwxr-xr-x 1 root root 1.7K May 24 09:26 javaRunner.sh
-rw-r--r-- 1 root root    0 May 24 09:26 inputParams
-rw-r--r-- 1 root root    2 May 24 09:26 inputForInfinite
-rw-r--r-- 1 root root   14 May 24 09:26 inputFile

When I run the above script it does not return anything but when I change script to this

#!/bin/bash
find ./ -type f -newermt '2018-05-24 09:26:49' ! -newermt '2018-05-24 09:26:52'

i.e. decreasing time by 1 second (from 2018-05-24 09:26:50 to 2018-05-24 09:26:49), it gives me the required result:

./inputFile
./main.c
./sql_runner.sh
./script_ide.sh
./q
./inputForInfinite
./javaRunner.sh
./program.txt
./inputParams
./script.sh

Why am I having such a behaviour because the output of date -r ./sql_runner.sh gives is:

Thu May 24 09:26:50 UTC 2018

What should I change in this script to have desired behaviour?

  • 1
    The first -newermt argument ask for files newer than 09:26:50. But 09:26:50 is not newer than 09:26:50. – Sparhawk May 24 '18 at 9:59
  • And hence the change in the script to have the desired behaviour is exactly what you did, i.e. make the time one second earlier. – Sparhawk May 24 '18 at 10:09
  • Beware date -r doesn't show the nanoseconds by defaut. With GNU date, you can use date -r file --iso-8601=ns or with GNU stat: stat -c %y file. Note that for symlinks, date gives you the mtime of the target while stat (and ls -l) gives you the mtime of the symlink. – Stéphane Chazelas May 24 '18 at 10:11
  • @ Sparhawk Ok, can i mention milliseconds then to differentiate between the files which were created after like 09:26:50:200 ? – Himanshu May 24 '18 at 10:13
0

Apparently, the timestamp of sql_runner.sh (and the other files) is between 09:26:49.000 and 09:26:49.999; therefore, it is newer than ...49 but not newer than ...50. If you want to split hairs seconds, GNU find will compare timestamps down to the nanosecond:

/* Returns ts1 - ts2 */
static double ts_difference (struct timespec ts1,
                 struct timespec ts2)
{
  double d =  difftime (ts1.tv_sec, ts2.tv_sec)
    + (1.0e-9 * (ts1.tv_nsec - ts2.tv_nsec));
  return d;
}


static int
compare_ts (struct timespec ts1,
        struct timespec ts2)
{
  if ((ts1.tv_sec == ts2.tv_sec) &&
      (ts1.tv_nsec == ts2.tv_nsec))
    {
      return 0;
    }
  else
    {
      double diff = ts_difference (ts1, ts2);
      return diff < 0.0 ? -1 : +1;
    }
}

You were close with the syntax; use a period to add the fractional seconds, not another colon:

find ./ -type f -newermt '2018-05-24 09:26:50.200' ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.