Linux命令查找最近n秒内更改的文件

21 linux bash shell find

我想要一个 Linux 命令来查找在最后n几秒钟更改的文件。

是否有可以从命令行界面或 GUI 运行的 shell 脚本或其他工具?

dr *_*bob 16

mtime 指定秒的解决方案在我使用find --version== 的linux系统上不起作用find (GNU findutils) 4.4.2

我收到以下错误:

mycomputer:~/new$ find . -mtime -60s
find: missing argument to `-mtime'
mycomputer:~/new$ find . -mtime -60seconds
find: missing argument to `-mtime'
Run Code Online (Sandbox Code Playgroud)

但是,我可以使用-mmin(用于在最后 m 分钟内修改),并且可以接受十进制参数;例如,以下查找在过去 30 秒内修改的文件。

find . -mmin 0.5
Run Code Online (Sandbox Code Playgroud)

例如;在过去 120 秒内创建上次修改的文件 1s、6s、11s、... ago,此命令查找:

mycomputer:~/new$ for i in $(seq 1 5 120); do touch -d "-$i seconds" last_modified_${i}_seconds_ago ; done
mycomputer:~/new$ find . -mmin 0.5
.
./last_modified_1_seconds_ago
./last_modified_26_seconds_ago
./last_modified_11_seconds_ago
./last_modified_16_seconds_ago
./last_modified_21_seconds_ago
./last_modified_6_seconds_ago
Run Code Online (Sandbox Code Playgroud)

因此,如果您真的需要在几秒钟内使用它,您可以执行以下操作:

localhost:~/new$ for i in $(seq 1 1 120); do touch -d "-$i seconds" last_modified_${i}_seconds_ago ; done
localhost:~/new$ N=18; find . -mmin $(echo "$N/60"|bc -l)
./last_modified_1_seconds_ago
./last_modified_9_seconds_ago
./last_modified_14_seconds_ago
./last_modified_4_seconds_ago
./last_modified_12_seconds_ago
./last_modified_13_seconds_ago
./last_modified_8_seconds_ago
./last_modified_3_seconds_ago
./last_modified_5_seconds_ago
./last_modified_11_seconds_ago
./last_modified_17_seconds_ago
./last_modified_16_seconds_ago
./last_modified_7_seconds_ago
./last_modified_15_seconds_ago
./last_modified_10_seconds_ago
./last_modified_6_seconds_ago
./last_modified_2_seconds_ago
Run Code Online (Sandbox Code Playgroud)


小智 14

像这样使用 find 命令:

find . -name "*.txt" -mtime -60s

查找*.txt过去 60 秒内修改的所有文件。

  • 在 linux 中,使用 find(来自 GNU findutils 4.4.2),我收到以下命令的错误:`find: missing argument to \`-mtime'`。但是,我可以使用 -mmin 和小数参数来获得所需的行为。我在联机帮助页中找不到任何关于使用 `s` 作为参数的参考。 (17认同)
  • -60s 不是 `-mtime` 的有效参数。“60s”甚至不是 POSIX 或 GNU find 中的有效选项。`-mtime` 的参数是一个数字,它指定了 24 小时前修改文件的时间。 (6认同)

dan*_*uer 10

与 glenn 建议的类似,如果您想找到所有已修改的内容,例如,在安装程序运行期间,执行以下操作可能会更容易:

touch /tmp/checkpoint
<do installer stuff>
find / -newer /tmp/checkpoint
Run Code Online (Sandbox Code Playgroud)

那你就不用做时间计算了;你只是发现在检查点文件之后发生了变化。


shi*_*ams 8

最简单的方法是:

find . -name "*.txt" -newermt '6 seconds ago'
Run Code Online (Sandbox Code Playgroud)

-mtime -60s答案中提到的选项不适用于 的许多版本find,即使在 2016 年-newermt也是如此。对我们来说是一个更好的选择。它可以解析许多不同的日期和时间格式。

另一种使用方法mmin是:

find . -name "*.txt" -mmin -0.5

# Finds files modified within the last 0.5 minute, i.e. last 30 seconds
Run Code Online (Sandbox Code Playgroud)

此选项可能不适用于所有find版本。

  • 这显然是最好的解决方案。 (2认同)

小智 7

如果您有一个不支持的 find 版本,-mtime -60s那么更好的解决方案是

touch -d '-60 seconds' /tmp/newerthan
find . -name "*.txt" -newer /tmp/newerthan
Run Code Online (Sandbox Code Playgroud)