导读 有时候可能需要检查有关文件的详细信息,例如文件的修改日期。当你要检查文件的最后编辑时间时,本文可能会派上用场。在本文将学习4种方法查看文件的修改日期。
使用stat命令

stat命令可以显示文件属性的详细信息,比如最近一次访问和修改文件的时间、文件大小等信息,使用起来比较简单,命令后面只需要加上文件名就可以:

[root@localhost ~]# stat hello_script.sh 
  File: ‘hello_script.sh’
  Size: 31        	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d Inode: 67169379    Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-10-15 19:13:24.628009932 +0800
Modify: 2020-10-15 19:07:18.266426499 +0800
Change: 2020-10-15 19:11:48.227856412 +0800
 Birth: -


从上面的输出中,我们可以看到文件的访问日期、文件的修改日期、文件权限的修改日期以及其他参数。

如果只希望查看文件的修改日期,而不考虑所有其他信息,运行以下命令:

[root@localhost ~]# stat -c %y hello_script.sh 
2020-10-15 19:07:18.266426499 +0800


-c选项用于指定自定义格式代替默认的输出,而'%y'标志显示上次修改时间。对于文件夹,语法保持不变。只需将文件名替换为文件夹名称即可。

使用date命令

date命令的用法是显示当前日期。但是当与-r选项一起使用时,可以显示文件的最后修改日期,如下所示:

[root@localhost ~]# date -r hello_script.sh 
Thu Oct 15 19:07:18 CST 2020

使用ls -l命令

ls -l命令通常用于使用长列表显示有关文件的其他信息,例如文件权限和所有者,大小和创建日期。可以添加-t选项,这样就可以按照文件的修改时间来排列:

[root@localhost ~]# ls -lt
或者
[root@localhost ~]# ll -t

total 288
drwxr-xr-x. 2 root root    177 Oct 16 14:36 b
drwxr-xr-x. 2 root root    177 Oct 16 14:36 a
-rwxr-xr-x. 1 root root    119 Oct 15 19:20 backup_script.sh
-rwxr-xr-x. 1 root root     31 Oct 15 19:07 hello_script.sh
-rw-r--r--. 1 root root    227 Oct 13 16:39 content.txt
-rw-r--r--. 1 root root 277159 Oct 12 14:37 a.txt
drwxr-xr-x. 2 root root    195 Aug  6 14:12 Files
-rw-------. 1 root root   1284 Dec 29  2019 anaconda-ks.cfg

使用httpie工具

另一种检查文件的修改日期的方法是使用httpie ,是HTTP命令行客户端工具。该工具通常用于与HTTP服务器和API交互,还可以检查驻留在web服务器上文件的修改时间。

首先需要确保安装了python的pip包管理工具,然后安装httpie工具:

Centos7/RHEL7中,运行以下命令安装httpie:

[root@localhost ~]# yum -y install python-pip
[root@localhost ~]# pip install --upgrade pip
[root@localhost ~]# pip install httpie


在Ubuntu / Deepin / Debian中运行以下命令安装httpie:

$ sudo apt install httpie

安装完成之后,那么如何查看web服务器上文件的修改时间呢?语法如下:

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

例如,从www.linuxprobe.com网站中,查看一张.png格式的图片修改时间:

[root@localhost ~]# http -h https://www.linuxprobe.com/wp-content/uploads/2020/06/6-1.png | grep -i 'Last-Modified'
Last-Modified: Fri, 05 Jun 2020 14:26:11 GMT

总结

在本文中,介绍了各种方法,可以使用这些方法列出文件的最后修改日期,甚至可以使用httpie工具列出web服务器上的文件的最后修改日期。

本文原创地址:https://www.linuxprobe.com/linux-file-date.html编辑:逄增宝,审核员:逄增宝