Linux 我如何在 bash 中检查文件是否是在 x 时间之前创建的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/552724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:00:14  来源:igfitidea点击:

how do I check in bash whether a file was created more than x time ago?

linuxbashcommand-line

提问by flybywire

I want to check in linux bash whether a file was created more than x time ago.

我想在 linux bash 中检查一个文件是否是在 x 之前创建的。

let's say the file is called text.txt and the time is 2 hours.

假设文件名为 text.txt,时间为 2 小时。

 if [ what? ]
 then
     echo "old enough"
 fi

采纳答案by kmkaplan

Only for modification time

仅用于修改时间

if test `find "text.txt" -mmin +120`
then
    echo old enough
fi

Or, the same in one line:

或者,在一行中相同:

#!/bin/bash
find text.txt -mmin +120 -exec echo "old enough" \;

You can use -cminfor change or -aminfor access time. As others pointed I don't think you can track creation time.

您可以-cmin用于更改或-amin访问时间。正如其他人指出的那样,我认为您无法跟踪创建时间。

回答by Philip Reynolds

Creation time isn't stored.

不存储创建时间。

What are stored are three timestamps (generally, they can be turned off on certain filesystems or by certain filesystem options):

存储的是三个时间戳(通常,它们可以在某些文件系统或某些文件系统选项上关闭):

  • Last access time
  • Last modification time
  • Last change time
  • 最后访问时间
  • 最后修改时间
  • 最后更改时间

a "Change" to the file is counted as permission changes, rename etc. While the modification is contents only.

对文件的“更改”计为权限更改、重命名等。而修改仅是内容。

回答by fdr

Consider the outcome of the tool 'stat':

考虑工具“stat”的结果:

  File: `infolog.txt'
  Size: 694         Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 11635578    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/     fdr)   Gid: ( 1000/     fdr)
Access: 2009-01-01 22:04:15.000000000 -0800
Modify: 2009-01-01 22:05:05.000000000 -0800
Change: 2009-01-01 22:05:05.000000000 -0800

You can see here the three dates for Access/modify/change. There is no created date. You can only really be sure when the file contents were modified (the "modify" field) or its inode changed (the "change" field).

您可以在此处看到访问/修改/更改的三个日期。没有创建日期。您只能真正确定文件内容何时被修改(“修改”字段)或其inode 更改(“更改”字段)。

Examples of when both fields get updated:

两个字段都更新的示例:

"Modify" will be updated if someone concatenated extra information to the end of the file.

如果有人将额外信息连接到文件末尾,“修改”将被更新。

"Change" will be updated if someone changed permissions via chmod.

如果有人通过 chmod 更改了权限,“更改”将被更新。

回答by Guss

Using the statto figure out the last modification date of the file, dateto figure out the current time and a liberal use of bashisms, one can do the test that you want based on the file's last modification time1.

使用stat找出文件的最后修改日期,date找出当前时间和 bashisms 的自由使用,可以根据文件的最后修改时间1进行您想要的测试。

if [ "$(( $(date +"%s") - $(stat -c "%Y" $somefile) ))" -gt "7200" ]; then
   echo "$somefile is older then 2 hours"
fi

While the code is a bit less readable then the findapproach, I think its a better approach then running findto look at a file you already "found". Also, date manipulation is fun ;-)

虽然代码的可读性不如find方法,但我认为它是一种更好的方法,然后运行find查看您已经“找到”的文件。此外,日期操作很有趣;-)



  1. As Phil correctly noted creation time is not recorded, but use %Zinstead of %Ybelow to get "change time" which may be what you want.
  1. 正如 Phil 正确指出的那样,没有记录创建时间,而是使用下面的%Z代替%Y来获得“更改时间”,这可能是您想要的。

[Update]

[更新]

For mac users, use stat -f "%m" $somefileinstead of the Linux specific syntax above

对于 mac 用户,请使用stat -f "%m" $somefile上面的 Linux 特定语法代替

回答by Maryam Jeddian

Although ctime isn't technicallythe time of creation, it quite often is.

尽管 ctime 从技术上讲不是创建时间,但它经常是。

Since ctime it isn't affected by changes to the contents of the file, it's usually only updated when the file is created. And yes - I can hear you all screaming - it's also updated if you change the access permissions or ownership... but generally that's something that's done once, usually at the same time you put the file there.

由于 ctime 不受文件内容更改的影响,因此通常仅在创建文件时更新。是的 - 我可以听到你们都在尖叫 - 如果您更改访问权限或所有权,它也会更新......但通常这是一次完成的事情,通常是在您将文件放在那里的同时。

Personally I always use mtime for everything, and I imagine that is what you want. But anyway... here's a rehash of Guss's "unattractive" bash, in an easy to use function.

就个人而言,我总是将 mtime 用于所有事情,我想这就是您想要的。但无论如何......这是Guss“没有吸引力”的bash的重述,在一个易于使用的功能中。

#!/bin/bash
function age() {
   local filename=
   local changed=`stat -c %Y "$filename"`
   local now=`date +%s`
   local elapsed

   let elapsed=now-changed
   echo $elapsed
}

file="/"
echo The age of $file is $(age "$file") seconds.

回答by nemik

I always liked using date -r /the/file +%sto find its age.

我一直喜欢用它date -r /the/file +%s来找出它的年龄。

You can also do touch --date '2015-10-10 9:55' /tmp/fileto get extremely fine-grained time on an arbitrary date/time.

您还可以touch --date '2015-10-10 9:55' /tmp/file在任意日期/时间获得极其细粒度的时间。

回答by Vide

The find one is good but I think you can use anotherway, especially if you need to now how many seconds is the file old

找到一个很好,但我认为你可以使用另一种方式,特别是如果你现在需要知道文件旧了多少秒

date -d "now - $( stat -c "%Y" $filename ) seconds" +%s

date -d "now - $( stat -c "%Y" $filename ) seconds" +%s

using GNU date

使用 GNU 日期

回答by xpmatteo

I use

我用

file_age() {
    local filename=
    echo $(( $(date +%s) - $(date -r $filename +%s) ))
}

is_stale() {
    local filename=
    local max_minutes=20
    [ $(file_age $filename) -gt $(( $max_minutes*60 )) ]
}

if is_stale /my/file; then
    ...
fi