小編給大家分享一下Linux中怎樣查看文件的創(chuàng)建時(shí)間,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)專注于景德鎮(zhèn)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供景德鎮(zhèn)營(yíng)銷型網(wǎng)站建設(shè),景德鎮(zhèn)網(wǎng)站制作、景德鎮(zhèn)網(wǎng)頁(yè)設(shè)計(jì)、景德鎮(zhèn)網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造景德鎮(zhèn)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供景德鎮(zhèn)網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
一、簡(jiǎn)介
Linux的文件能否找到文件的創(chuàng)建時(shí)間取決于文件系統(tǒng)類型,在ext4之前的早期文件系統(tǒng)中(ext、ext2、ext3),文件的元數(shù)據(jù)不會(huì)記錄文件的創(chuàng)建時(shí)間,它只會(huì)記錄訪問(wèn)時(shí)間、修改時(shí)間、更改時(shí)間(狀態(tài)更改時(shí)間)。典型的文件的基礎(chǔ)信息如下所示:
[root@bugwz ~]# stat test.file File: ‘test.file' Size: 2 Blocks: 8 IO Block: 4096 regular file Device: 807h/2055d Inode: 5255117 Links: 1 Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-12-12 19:11:33.175841399 +0800 Modify: 2019-12-12 19:11:37.564970487 +0800 Change: 2019-12-12 19:11:43.079132663 +0800 Birth: -
Access:訪問(wèn)時(shí)間,文件數(shù)據(jù)的最后訪問(wèn)時(shí)間(例如:讀文件內(nèi)容);
Modify:修改時(shí)間,文件數(shù)據(jù)的最后修改時(shí)間。(例如:修改文件內(nèi)容);
Change:狀態(tài)更改時(shí)間,這個(gè)跟 Modify 時(shí)間很容易混淆,文件的屬性(權(quán)限,大小等)的變更時(shí)間;
二、實(shí)踐
2.1、獲取文件的創(chuàng)建時(shí)間
獲取文件inode號(hào),如下所示,拿到inode號(hào)為:5255117;
[root@bugwz data]# stat /data/test.file File: ‘/data/test.file' Size: 2 Blocks: 8 IO Block: 4096 regular file Device: 807h/2055d Inode: 5255117 Links: 1 Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-12-12 19:11:33.175841399 +0800 Modify: 2019-12-12 19:11:37.564970487 +0800 Change: 2019-12-12 19:11:43.079132663 +0800 Birth: -
查找文件所在的磁盤路徑,如下所示,拿到磁盤路徑為:/dev/sda7
[root@bugwz data]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 3.9G 2.5G 1.2G 70% / devtmpfs 16G 0 16G 0% /dev tmpfs 16G 0 16G 0% /dev/shm tmpfs 16G 1.7G 14G 11% /run tmpfs 16G 0 16G 0% /sys/fs/cgroup /dev/sda1 12G 11G 787M 94% /usr /dev/sda5 7.8G 4.2G 3.2G 57% /tmp /dev/sda7 235G 180G 44G 81% /data /dev/sda6 7.8G 2.1G 5.3G 29% /var
使用debugfs查看文件的創(chuàng)建時(shí)間,發(fā)現(xiàn)創(chuàng)建時(shí)間crtime為:Thu Dec 12 19:05:23 2019
[root@bugwz data1]# debugfs -R 'stat <5255117>' /dev/sda7 debugfs 1.42.9 (28-Dec-2013) Inode: 5255117 Type: regular Mode: 0755 Flags: 0x80000 Generation: 758605841 Version: 0x00000000:00000001 User: 0 Group: 0 Size: 2 File ACL: 0 Directory ACL: 0 Links: 1 Blockcount: 8 Fragment: Address: 0 Number: 0 Size: 0 ctime: 0x5df2206f:12dddfdc -- Thu Dec 12 19:11:43 2019 atime: 0x5df22065:29ec81dc -- Thu Dec 12 19:11:33 2019 mtime: 0x5df22069:86b30fdc -- Thu Dec 12 19:11:37 2019 crtime: 0x5df21ef3:d586ca44 -- Thu Dec 12 19:05:23 2019 Size of extra inode fields: 28 EXTENTS: (0):16949121
2.2、集成腳本:
#!/bin/sh [ $# -ne 1 ] && echo "Usage: $0 {FILENAME}" && exit 1 INODE=`ls -i $1 |awk '{print $1}'` FILENAME=$1 # 如果傳入?yún)?shù)帶/,則獲取這個(gè)傳入?yún)?shù)的目錄路徑并進(jìn)入目錄 `echo $FILENAME | grep / 1> /dev/null` && { FPWD=${FILENAME%/*};FPWD=${FPWD:=/};cd ${FPWD};FPWD=`pwd`; } || FPWD=`pwd` array=(`echo ${FPWD} | sed 's@/@ @g'`) array_length=${#array[@]} for ((i=${array_length};i>=0;i--)); do unset array[$i] SUBPWD=`echo " "${array[@]} | sed 's@ @/@g'` DISK=`df -h |grep ${SUBPWD}$ |awk '{print $1}'` [[ -n $DISK ]] && break done # 文件系統(tǒng)非ext4則退出 [[ "`df -T | grep ${DISK} |awk '{print $2}'`" != "ext4" ]] && { echo ${DISK} is not mount on type ext4! Only ext4 file system support!;exit 2; } debugfs -R "stat <${INODE}>" ${DISK}
以上是“Linux中怎樣查看文件的創(chuàng)建時(shí)間”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!