在查找选项 -amin、-atime、-cmin、-ctime 中使用小数 +n、-n 或 n 时出现混淆

在查找选项 -amin、-atime、-cmin、-ctime 中使用小数 +n、-n 或 n 时出现混淆

我不确定我是否理解了find在使用这些谓词进行查找时忽略小数部分的方法。例如,我使用了

$ find . -ctime .2 # files status changed exactly 4.8 hours ago ? 
$ find . -ctime +.2 # files status changed more than 4.8 hours ago ?
$ find . -ctime -.2 # files status changed less than 4.8 hours ago ?

并且总是得到不同的输出。这里是如何计算小时数的?

答案1

你可能更适合使用-cminand-mmin谓词


是的,它将进行子日和子小时的插值:

parser.c,第 3243 行

/* Get a timestamp and comparison type.

   STR is the ASCII representation.
   Set *NUM_DAYS to the number of days/minutes/whatever, taken as being
   relative to ORIGIN (usually the current moment or midnight).
   Thus the sense of the comparison type appears to be reversed.
   Set *COMP_TYPE to the kind of comparison that is requested.
   Issue OVERFLOWMESSAGE if overflow occurs.
   Return true if all okay, false if input error.

   Used by -atime, -ctime and -mtime (parsers) to
   get the appropriate information for a time predicate processor. */

static boolean
get_relative_timestamp (const char *str,
            struct time_val *result,
            struct timespec origin,
            double sec_per_unit,
            const char *overflowmessage)
{
  double offset, seconds, nanosec;
  static const long nanosec_per_sec = 1000000000;

  if (get_comp_type(&str, &result->kind))
    {
      /* Invert the sense of the comparison */
      switch (result->kind)
    {
    case COMP_LT: result->kind = COMP_GT; break;
    case COMP_GT: result->kind = COMP_LT; break;
    default: break;
    }

      /* Convert the ASCII number into floating-point. */
      if (xstrtod(str, NULL, &offset, strtod))
    {
      /* Separate the floating point number the user specified
       * (which is a number of days, or minutes, etc) into an
       * integral number of seconds (SECONDS) and a fraction (NANOSEC).
       */
      nanosec = modf(offset * sec_per_unit, &seconds);
      nanosec *= 1.0e9; /* convert from fractional seconds to ns. */
      assert (nanosec < nanosec_per_sec);

      /* Perform the subtraction, and then check for overflow.
       * On systems where signed aritmetic overflow does not
       * wrap, this check may be unreliable.   The C standard
       * does not require this approach to work, but I am aware
       * of no platforms where it fails.
       */
      result->ts.tv_sec  = origin.tv_sec - seconds;
      if ((origin.tv_sec < result->ts.tv_sec) != (seconds < 0))
        {
          /* an overflow has occurred. */
          error (1, 0, overflowmessage, str);
        }

      result->ts.tv_nsec = origin.tv_nsec - nanosec;
      if (origin.tv_nsec < nanosec)
        {
          /* Perform a carry operation */
          result->ts.tv_nsec += nanosec_per_sec;
          result->ts.tv_sec  -= 1;
        }
      return true;
    }
      else
    {
      /* Conversion from ASCII to double failed. */
      return false;
    }
    }
  else
    {
      return false;
    }
}

pred.c,第 237 行:

/* 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;
}

相关内容