在Linux中如何檢視檔案的修改日期

夢共裡醉發表於2021-09-06
導讀有時候可能需要檢查有關檔案的詳細資訊,例如檔案的修改日期。當你要檢查檔案的最後編輯時間時,本文可能會派上用場。在本文將學習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: -

在Linux中如何檢視檔案的修改日期在Linux中如何檢視檔案的修改日期
從上面的輸出中,我們可以看到檔案的訪問日期、檔案的修改日期、檔案許可權的修改日期以及其他引數。

如果只希望檢視檔案的修改日期,而不考慮所有其他資訊,執行以下命令:

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

在Linux中如何檢視檔案的修改日期在Linux中如何檢視檔案的修改日期
-c選項用於指定自定義格式代替預設的輸出,而'%y'標誌顯示上次修改時間。對於資料夾,語法保持不變。只需將檔名替換為資料夾名稱即可。

使用date命令

date命令的用法是顯示當前日期。但是當與-r選項一起使用時,可以顯示檔案的最後修改日期,如下所示:

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

在Linux中如何檢視檔案的修改日期在Linux中如何檢視檔案的修改日期

使用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

在Linux中如何檢視檔案的修改日期在Linux中如何檢視檔案的修改日期

使用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

在Linux中如何檢視檔案的修改日期在Linux中如何檢視檔案的修改日期
在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

在Linux中如何檢視檔案的修改日期在Linux中如何檢視檔案的修改日期

總結

在本文中,介紹了各種方法,可以使用這些方法列出檔案的最後修改日期,甚至可以使用httpie工具列出web伺服器上的檔案的最後修改日期。Linux就該這麼學

相關文章