2014-04-03 12:45:56 +0000 2014-04-03 12:45:56 +0000
166
166

在Linux中获取文件的最后修改日期

我是Linux的新手。我正在使用命令行。我想查看一个文件的最后修改日期。我如何在Linux中从命令行中查看文件的最后修改日期?

答案 (7)

147
147
147
2015-09-21 10:22:09 +0000

正如@edvinas.me所提到的,stat告诉你关于文件的各种信息,包括最后一次修改的日期。

一开始,我把ModifyChange搞混了,只是为了澄清一下,stat输出列表:

-Access显示最后一次数据访问的时间(如:读。 -Modify显示最后一次修改数据的时间 -Change显示最后一次改变文件状态的时间

例如:

~ $ touch foo
~ $ stat foo
File: ‘foo’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fc01h/64513d Inode: 410397 Links: 1
Access: (0644/-rw-r--r--) Uid: (80972/ etomort) Gid: (18429/ eem_tw)
Access: 2015-09-21 12:06:11.343616258 +0200
Modify: 2015-09-21 12:06:11.343616258 +0200
Change: 2015-09-21 12:06:11.343616258 +0200
Birth: -

~ $ echo "Added bar to foo file" >> foo
~ $ stat foo
File: ‘foo’
Size: 42 Blocks: 8 IO Block: 4096 regular file
Device: fc01h/64513d Inode: 410654 Links: 1
Access: (0644/-rw-r--r--) Uid: (80972/ etomort) Gid: (18429/ eem_tw)
Access: 2015-09-21 12:09:31.298712951 +0200
Modify: 2015-09-21 12:09:31.298712951 +0200
Change: 2015-09-21 12:09:31.302713093 +0200
Birth: -

~ $ chmod 444 foo
~ $ stat foo
File: ‘foo’
Size: 42 Blocks: 8 IO Block: 4096 regular file
Device: fc01h/64513d Inode: 410654 Links: 1
Access: (0444/-r--r--r--) Uid: (80972/ etomort) Gid: (18429/ eem_tw)
Access: 2015-09-21 12:09:31.298712951 +0200
Modify: 2015-09-21 12:09:31.298712951 +0200
Change: 2015-09-21 12:10:16.040310543 +0200
Birth: -
69
69
69
2014-04-03 12:47:41 +0000

使用stat命令:

$ stat file
40
40
40
2017-08-31 01:04:43 +0000

另一种比较灵活的方法是使用date -r。从man date

-r, --reference=FILE
       display the last modification time of FILE

这样做的好处是可以指定输出格式,例如:

$ date -r foo
Thu Aug 31 10:36:28 AEST 2017
$ date -r foo -R
Thu, 31 Aug 2017 10:36:28 +1000
$ date -r foo -u
Thu Aug 31 00:36:28 UTC 2017
$ date -r foo +%s
1504139788
17
17
17
2015-11-16 05:43:54 +0000

ls -l应该做的工作。

例:

#> ls -l /home/TEST/
total 16

-rw-r--r-- 1 rfmas1 nms 949 Nov 16 12:21 create_nd_lists.py

-rw-r--r-- 1 rfmas1 nms 0 Nov 16 12:35 enb_list

-rw-r--r-- 1 rfmas1 nms 0 Nov 16 12:35 nb_list

-rw-r--r-- 1 rfmas1 nms 0 Nov 16 12:35 nodes_ip.txt

-rw-r--r-- 1 rfmas1 nms 0 Nov 16 12:35 rnc_list
3
3
3
2019-08-14 16:24:39 +0000

Building off of@Adam Taylor’s comment in @phoops’s answer and @Sparhawk’s answer.

具体来说,只是得到日期(以2019年10月3日为例,因为这是我最后一个生日,如果你觉得我的Venmo,如果你觉得我的生日是最后一个生日,这是我的Venmo,如果你觉得我的生日是我的生日,那就给你一个祝福。@levi_uzodike)

  • stat -c %y file | cut -d' ' -f1会给你
  • 2019-10-03也会给你
  • date +%F -r file也会给你
  • 2019-10-03也会给你
  • date +%D -r file也会给你
  • 10/03/19也会给你
  • date +%x -r file也会给你
  • 10/03/2019也会probably给你10/03/19,如果你在美国,或者03/10/2019也会给你03/10/19,如果你在英国,或者date也会给你`%‘: > > - (hyphen) do not pad the field
    > _ (underscore) pad with spaces
    > 0 (zero) pad with zeros
    > ^ use upper case if possible
    > # use opposite case if possible

N.B.: These flags don’t work on the “combo formats” like `。仅举几个例子(当然还有更多的可能性)

这些,格式选项,据我的理解,是其他格式选项的组合。以下是 man page 的一些解释:

%b locale的月名缩写 (例如:Jan) %B locale的全月名 (例如:Jan) %B locale的全月名 (例如:Jan, 1月) …. %d月份的日期 (例如,01) %D日期;与%m/%d/%y相同 %e月份的日期,空格填充;与%d相同 %F完整的日期;与%Y-%m-%d相同 … %m月份 (01…12) … %x locale的日期表示 (例如, 12/31/99) …. %y年的最后两位数字 (00…99) %Y年 …. 默认情况下,日期填充数字字段为零。
下面的可选标志可以跟随and%F`. They are for the “
singular field formats_”.

Apparently this last flag ( # ) does not work as I’d expect (e.g., if %D gives %D, %x gives date +%b as opposed to Oct) I guess this would be useless, but I’d think a lower case option would be more useful. date +%#bdoes turn OCT which might give oCT or date +%#P into 日期+%P or PM, respectively. So I guess it’s not a 'per-character’ case switch but sets the case of all the characters in the string to the opposite case of the majority of the characters? Also AM gives AM or 日期+%P, but neither PM nor AM change its output. My guess for this case is that 日期+%^P is just an alias for 日期+%#P, and it seems that whenever you add more than one flag, the behavior is undefined/unpredictable ( e.g., %#P gives the same as %#P: 日期+%0- e and date +%-e gives the same as 3: date +%-0e, which makes you think that only the flag next to the letter works or that it goes left to right, but both date +%0e and 03 give date +%#^p or date +%^#p, [depending on the time of course] ) unless there’s some hidden order of operations? Sorry for digressing…

Also, if you run the command pm, you can see the combo for the specific locale of your system (e.g., am).

And you can make your own combo. For example,

  • locale -k LC_TIME | grep ^d_fmt will give you d_fmt="%m/%d/%Y"&007date +%^b %-e %Y -r file&007OCT 3 2019
2
2
2
2017-01-06 10:08:23 +0000

如果文件在另一个webserver上,我喜欢httpie docs)。

安装

pip install httpie --user

使用方法

-h命令只给出了头。模式是

http -h [url] | grep 'Last-Modified\|Date'

示例:

$ http -h https://martin-thoma.com/author/martin-thoma/ | grep 'Last-Modified\|Date'
Date: Fri, 06 Jan 2017 10:06:43 GMT
Last-Modified: Fri, 06 Jan 2017 07:42:34 GMT

Date 这个命令很重要,因为它报告的是服务器时间,而不是你的本地时间。另外,不是每个服务器都会发送Last-Modified(例如,超级用户似乎不会发送&007)。

2
2
2
2018-11-14 04:22:35 +0000

1) 列出文件目录中的最后修改日期/时间

要列出文件,并在顶部显示最后修改的文件,我们将使用-lt选项和ls命令。