如何使用 find 命令获取昨天创建的文件?

如何使用 find 命令获取昨天创建的文件?

在 Linux 脚本中,使用 find 来获取昨天创建的文件的选项是什么?

以下尝试无效:

find /log/bssuser/CDR/Postpaid_CDR_Log/ -newer yesterday

或者

find -mtime 24

-----------------------------
the below command get files for today 10/10

[bssuser@t-bss-bill-app-01 Amany]$ find -newermt yesterday -ls
142619049    4 drwxrwxr-x   2 bssuser  bssuser      4096 Oct 10 08:40 .
142619072    4 -rwxrwxr-x   1 bssuser  bssuser      2376 Oct 10 08:40 ./test_am.sh
142619050    4 -rw-rw-r--   1 bssuser  bssuser       433 Oct 10 08:40 ./errors.csv
142619058    4 -rw-rw-r--   1 bssuser  bssuser       323 Oct 10 08:40 ./logs.csv

答案1

find命令的-[acm]time谓词很棘手。

特别是,-mtime n匹配修改时间在n24n+1小时之前的当前时间。 所以

find . -mtime 1

将查找 24 至 48 小时前的文件现在。要查找相对于日历日的文件,GNUfind-daystart选项,因此要查找在前一个日历日修改的文件,您可以使用

find . -daystart -mtime 1

也可以看看:

答案2

您可以使用:

find -newermt yesterday -ls

工作原理,详细信息来自man find

-newerXY reference
       Succeeds if timestamp X of the file being considered is newer than timestamp Y of the file reference.  The letters X and Y
       can be any of the following letters:

       a   The access time of the file reference
       B   The birth time of the file reference
       c   The inode status change time of reference
       m   The modification time of the file reference
       t   reference is interpreted directly as a time

       Some combinations are invalid; for example, it is invalid for X to be t.  Some combinations are  not  implemented  on  all
       systems;  for  example  B is not supported on all systems.  If an invalid or unsupported combination of XY is specified, a
       fatal error results.  Time specifications are interpreted as for the argument to the -d option of GNU date.  If you try to
       use  the  birth  time of a reference file, and the birth time cannot be determined, a fatal error message results.  If you
       specify a test which refers to the birth time of files being examined, this test will fail for any files where  the  birth
       time is unknown.

答案3

在 Linux 中使用 find 命令查找文件比较棘手,但您可以使用“ -ctime 1”选项查找 1 天前更改的文件。
查找昨天创建的文件的 find 命令如下:

find /path/to/search/directory -type f -newermt $(date -d yesterday '+%Y-%m-%d') ! -newermt $(date '+%Y-%m-%d')

在此命令中:

  • /path/to/search/directory是您想要开始搜索的目录。
  • -type f确保您只查找常规文件。
  • -newermt $(date -d yesterday '+%Y-%m-%d')指定修改时间必须比昨天更新。
  • ! -newermt $(date '+%Y-%m-%d')确保修改时间不晚于今天。

也可以看看:

相关内容