上一次我們學習了關於linux的用戶權限方面的管理:
在使用linux系統,有時候需要對文件進行查找,而find命令比較全面:支持使用文件名、文件大小、所屬組/主、是否爲空、訪問時間、修改時間等:
1下面在介紹find命令前,先介紹幾個系統的查找命令:which\localte\whereis:
1.1: which:用來搜索命令(會在PATH裏面來尋找路徑)-所以只能查找命令類:
1 2 3 4 5 | [root@localhost ~] # which cp #用which來查找cp的路徑: alias cp = 'cp -i' /usr/bin/cp [root@localhost ~] # echo $PATH #會提前在生成的PATH路徑裏來查找: /usr/local/sbin : /usr/local/bin : /usr/sbin : /usr/bin : /root/bin |
1.2 whereis:搜索命令,會列出配置文件的位置,輸出很模糊:
[root@localhost ~]# whereis cp
cp: /usr/bin/cp /usr/share/man/man1/cp.1.gz
1.3:mlocalte:用來搜索文件/目錄和命令,但是搜索的結果比較模糊:
第一次使用時:會提示此命令存在:可以用mlocate來安裝:
yum install -y mlocate
工作原理:locate會在事先生成的一個數據庫中去尋找(數據庫用來統計所有命令和文件),據說會在每天凌晨4點來自動更新統計:
第一次使用則需要手動生成一下:用updatedb命令:
用法:localte查找會列出所有相關的文件/目錄和命令等:
1 2 3 4 5 6 | [root@localhost ~] # locate ls #會匹配到很多選項: /boot/grub2/i386-pc/blscfg .mod /boot/grub2/i386-pc/cbls .mod /boot/grub2/i386-pc/command .lst /boot/grub2/i386-pc/crypto .lst /boot/grub2/i386-pc/fs .lst |
2、linux系統的快捷鍵使用統計:
ctrl+l(小寫L) #清除屏幕內容,並將光標定位到第一行:
ctrl+d #退出當前終端,相當於exit或者logout.
ctrl+c #暫停當前輸入,並跳到下一行:
ctrl+u #刪除光標前的內容:
ctrl+d #刪除光標後的內容(一個字符一個字符的刪除):
ctrl+k #刪除光標後的內容(全部刪除)
ctrl+e #光標移動到行尾:end
ctrl+a #光標移動到行首:
3、find命令介紹:具體使用格式如下:
find 路徑 條件 ==== find /etc/ -type d
用法一:find基於名稱搜索:可以支持通配符*。
1 2 3 4 | [root@localhost ~] # find /etc/ -name "ssh*" #支持名稱來搜索: /etc/ssh /etc/ssh/ssh_config /etc/ssh/ssh_host_rsa_key |
用法二:find基於文件類型搜索: find /etc/ -type 類型
d(目錄) f(文件) l(連接文件) s(socket文件) b(塊設備文件) c(串口設備文件)
1 2 3 | [root@localhost ~] # find /etc/ -type d -name ssh* #查到是目錄的並且是sshd. /etc/ssh /etc/selinux/targeted/active/modules/100/ssh |
find支持 -o 選項,是或者的意思:多個參數之間要用 -o 選項來隔開:
1 2 3 4 5 | [root@localhost ~] # find /etc/ -type d -o -name ssh* #目錄或者是ssh文件都會被打印出來 /etc/ /etc/grub .d /etc/pki /etc/pki/rpm-gpg |
並且: find /etc/ -type d -name "ssh*" 不加-o則是並且的意思:
3.2 find的類型還有三個time: atime mtime ctime
那麼我們如何查看着三個time: stat命令
1 2 3 4 5 6 7 8 | [root@localhost ~] # stat 2.txt #用stat後加文件名稱來查看: 文件: "2.txt" 大小:0 塊:0 IO 塊:4096 普通空文件 設備:803h /2051d Inode:33574980 硬鏈接:1 權限:(0644 /-rw-r--r-- ) Uid:( 0/ root) Gid:( 0/ root) 最近訪問:2017-10-28 01:58:56.757868054 +0800 #atime(aacces time) modify:2017-10-28 01:58:56.757868054 +0800 #mtime(modify time) change:2017-10-28 01:58:56.757868054 +0800 #ctime (change time) |
atime:訪問時間:讀取和執行文件時修改: #ls -lu filename 列出文件的atime
mtime:更改文件內容時間,如寫入內容: #ls -l filename 列出文件的mtime
ctime:更改inode時間,(包含屬性、權限、文件大小、鏈接等): #ls -lc filename 文件的ctime.
注意:文件內容的改動,會同時更改mtime和ctime,因爲內容的內容會造成inode信息改變:
而ctime改變,mtime不會改變,如修改了文件的權限,而不更改文件的名稱:
用法一:查找出一天以內的文本: 參數: -mtime -1
1 2 3 4 5 | [root@localhost ~] # find / -type f -mtime -1 #只查找是文件類型的,文件內容更改時間在1天內的文件: /proc/fb /proc/fs/xfs/xqm /proc/fs/xfs/xqmstat /proc/bus/pci/00/00 .0 |
用法二:查找出一天以外的文本: 參數: -mtime +1
[root@localhost ~]# find / -type f -mtime +1 #只查找是文件類型的,文件內容更改時間在1天以外的文件:
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
/boot/grub2/i386-pc/gcry_rsa.mod
ctime和atime的用法也是同mtime一樣如此:
用法三:find還有有一個常用用法:查找硬鏈接:
分析:硬鏈接是根據inode號來區分,也就是我們要找到inode號相同的,即爲鏈接文件:
1 2 3 | [root@localhost ~] # find / -inum 33574980 #查找inode號相同的文件: /root/2 .txt /tmp/2 .txt.bak |
用法四:find可以以分鐘爲來查找文件:mmin +1(一小時外) mmin -1(一小時內)
[root@localhost ~]# find /root -type f -mmin -60 #查找1小時以內的文件:
/root/2.txt
另外find支持直接打印和列出:有以下兩種方法實現:輸出是一樣的。
方法一:通過xargs來實現:如下:
[root@localhost ~]# find /root -type f -mmin -60 |xargs ls -l
-rw-r--r-- 2 root root 6 10月 28 02:26 /root/2.txt
方法二:通過-exec來實現:如下:
[root@localhost ~]# find /root/ -type f -mmin -60 -exec ls -l {} \;
-rw-r--r-- 2 root root 6 10月 28 02:26 /root/2.txt
用法五:find支持直接打印後對文件進行操作,如修改名稱和刪除文件:如下:
直接查找類型是文件並且在150分鐘內,直接重命名爲*.bak.
1 2 3 4 5 6 7 | [root@localhost ~] # find /root/ -type f -mmin -150 -exec mv {} {}.bak \; [root@localhost ~] # find /root/ -mmin -150 /root/ /root/yuanhh /root/yuanhh/1 .bak /root/2 .txt.bak /root/1 .txt.bak |
用法六:find支持以文件的大小來查找:支持k,M,G
[root@localhost ~]# find /root/ -type f -size -10k -exec ls -lh {} \;
-rw-r--r--. 1 root root 18 12月 29 2013 /root/.bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 /root/.bash_profile
-10k(則表示10k) +10M(則表示10M)
整理:find常用命令小整理:
-name:名稱搜索,可支持通配符,如"yum*"等.
-mtime:以時間天爲單位搜索,如“-5則表示5天內,+5則表示5天外”.
-type:類型搜索:文件類型:如“-type d”表示目錄,“! type d”表示取反,除目錄之外的文件
-size:文件大小搜索:如“-size +10M”表示大於10M,“-10M”則表示小於10M的。
-mmin:以分鐘爲單位,如“-mmin -60”表示60分鐘以內的文件,"+60"表示60分鐘以外的文件:
-inum:表示查找硬鏈接(後面跟inode號):如:
[root@localhost ~]# find /root/ -inum 661735
-perm:表示查找權限(後更要查找的權限):如下:
[root@localhost ~]# find /root/ -perm +0644
4.文件名的後綴名:
linux下的文件/目錄的後綴名可以自定義:
但是我們爲了方便區分,會寫成類似如下的後綴名稱:
.txt: 文本文檔:
.gz : 壓縮文件:
.conf : 配置文件: