一 find stat命令
find 是 linux 系統中最常用的一個搜索命令,與他類似的有:
1.which 在系統變量中搜索命令(執行權限,即有X屬性)的絕對路徑
2.whereis 在特定目錄下搜索關鍵詞對應的路徑(模糊搜索)
3.locate 搜索本地文件(除tmp目錄) yum install -y mlocate。如找不到文件需使用 #updatedb 即時生成本地索引庫
以上三個命令都有一定的侷限性。
find (查找日誌用的最多) 常用選項和參數如下
stat 命令
用於顯示文件的狀態信息。比 ls 命令的輸出更詳細
[root@centos701 ~]# stat 1_heard.txt 查看文件三個 time
文件:"1_heard.txt"
大小:12 塊:8 IO 塊:4096 普通文件
設備:803h/2051d Inode:33581760 硬鏈接:1
權限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環境:unconfined_u:object_r:admin_home_t:s0
最近訪問Access:2017-10-26 22:52:24.316546539 +0800 ----簡稱atime
最近更改Modify:2017-10-26 22:52:21.349246832 +0800 ----簡稱mtime
最近改動Change:2017-10-26 23:00:21.750619709 +0800 ----簡稱ctime
創建時間:-
mtime更改了,ctime 一定會跟着改。
實例 --使用find 利用文件time 標識實現特定目的
1.快速查找
[root@centos701 ~]# find /etc/ -type f -name “sshd_config" 精確搜索
/etc/ssh/sshd_config
[root@centos701 ~]# find /etc/ -name "sshd*” 匹配搜索
/etc/ssh/sshd_config
/etc/systemd/system/multi-user.target.wants/sshd.service
/etc/sysconfig/sshd
/etc/pam.d/sshd
2.條件查找
[root@centos701 ~]# find /etc/ -type f -mtime -1 #查找一天內發生過更改的文件
[root@centos701 ~]# find /etc/ -type f -mtime +1 #查找一天前發生過更改的文件
3.按條件查找並顯示列表或批量修改後綴名
查找/etc/中1小時內發生過更改的文件,並列出來
[root@centos701 ~]# find /etc/ -type f -mmin -60 -exec ls -l {} \;
查找/目錄下1天內發生過更改的文件,並將其加上.bak 後綴
[root@centos701 ~]# find / -type f -mtime -1 -exec mv {}{}.bak \;
4.並且 或者關係查找
查找一天內發生過更改,並且文件名中含有.con 後綴的文件
[root@centos701 ~]# find /etc/ -type f -mtime -1 -name "*.con”
查找/etc 下一天內發生過更改,或者文件名中含有.con 後綴的文件
[root@centos701 ~]find /etc/ -type f -o -mtime -1 -o -name "*.conf"
5.大小查找,並刪除
查找/目錄下,大於10M 的文件,並列出來(單位也可以用 k)
find / -type f -size +10M -exec ls -lh {} \;
找到指定目錄下30天前的文件並刪除
[root@centos701 ~]# find /var/log -type f -mtime +30 |xargs rm -rf
6.查找相同 inode 常用於硬鏈接或特殊文件
查找指定 inode 號文件
[root@centos701 ~]# find / -inum 33583395
二 文件名後綴
Linux 系統中文件的後綴只是一個爲了方便管理的標識,和 windows 的後綴完全不同。
Linux文件類型常見的有:普通文件、目錄、字符設備文件、塊設備文件、符號鏈接文件等,可通過#ls -l 查看,以屬性中第1個字符來區分文件類型。在實際使用過程中,也有一些約定俗成的後綴以方便區分、管理
.sh shell 腳本文件
.php #php 文件
.pl perl 腳本文件
.so 庫文件
.py python 源程序代碼文件
.bz2 bzip2壓縮文件;.gz gzip 壓縮文件;
.java java 源程序代碼文件
.txt 純文本
.c c語言源程序代碼文件
.conf 配置文件