find命令選項
-name 按照文件名查找
-type 查找某一類型的文件(b 表明設備塊;d 目錄;c 字符設備文件;l 符號(軟)連接文件;f 普通文件)
-size 查找文件長度或者大小
-prune 查找文件時排除當前文件夾,不查找
-path
-depth 查找文件時,首先查找當前文件、當前目錄中的文件,而後再在其子目錄當中查找
-maxdepth 後面跟數字num,表示在當前目錄下往下num層深度(默認爲1層)
-perm 按照文件權限來查找
-user 能夠按照文件屬主來查找
-group 能夠按照文件數組來查找
-nouser 查找無效所屬主的文件
-nogroup 查找無效所屬組的文件
-newer:查找比文件更新時間更新的文件
-mtime:按對象內容修改時間查找(天數)
-atime:按對象被訪問的時間查找(天數)
-ctime:按對象狀態被修改的時間去查找(天數)
-mmin/-amin/-cmin:(分鐘數)
「+ n」 n天/分鐘之前的
「- n」 n天/分鐘之內的
-empty 查找大小爲0的文件或空目錄
-mount 在查找文件系統時不跨越mount的文件系統
-follow 若是find命令遇到符號連接文件,就跟蹤連接文件指向的源文件
-a 且
-o 或
! 非
-ok 提醒你是否要執行後面的命令python
(1) -namelinux
查找/test下面全部以.txt結尾的文件 # find /test -name "*.txt" 查找/test下面全部以數字開頭的txt文件 # find /test -name "[0-9].txt" 查找/目錄下面的"passwd"文件和"profile" 文件 # find / -name "profile" -o -name "passwd"
(2) -typeshell
查找/test下面全部的目錄 # find /test -type d | xargs file 查找/etc 下面的全部l 鏈接文件 # find /etc -type l | xargs ls -l | more
(3) -size數組
查找當前目錄文件大於10M的文件 # find . -size +10M | xargs du –lh 查找當前目錄文件小於10M的文件 # find . -size -10M | xargs du –lh 查找/etc/目錄下面大於1M的文件,而且把 文件大小信息列出來 # find /etc/ -size +1M |xargs du -sh
(4) -prune -path -depth安全
當前目錄下有1.doc、2.doc,其子目錄/test下有3.doc、4.doc 查找當前目錄下全部的.doc文件 # find . -name "*.doc" ./test/4.doc ./test/3.doc ./2.doc ./1.doc 忽略子目錄/test,只查詢當前目錄的.doc文件 # find . -path "./test" -prune -o -name "*.doc" ./test ./2.doc ./1.doc 進入(或者說是指定)子目錄/test查找.doc文件 # find ./test -depth -name "*.doc" ./test/4.doc ./test/3.doc
(5) -perm
bash
查找/test下權限爲755的文件 # find /test -perm 755
(6) -user -group -nouser -nogroupspa
修改gongda.txt文件的屬主屬組爲gongda # useradd gongda # touch gongda.txt # chown gongda:gongda gongda.txt 查找當前目錄下屬主爲gongda的文件 # find . -user gongda | xargs ls -l -rw-r--r-- 1 gongda gongda 0 5月 13 20:50 ./gongda.txt 查找當前目錄下屬組爲gongda的文件 # find . -group gongda | xargs ls -l -rw-r--r-- 1 gongda gongda 0 5月 13 20:50 ./gongda.txt 刪除gongda用戶和組 # userdel -r gongda 查找當前目錄下無效屬主的文件 # find . -nouser | xargs ls -l -rw-r--r-- 1 509 509 0 5月 13 20:50 ./gongda.txt 查找當前目錄下無效屬組的文件 # find -nogroup | xargs ls -l -rw-r--r-- 1 509 509 0 5月 13 20:50 ./gongda.txt
(7) -newerxml
查詢比1.txt新的文件 # find . -newer 1.txt 查詢比1.txt新可是比alvin.txt舊的文件 # find . -newer 1.txt ! -newer alvin.txt
(8) -mtime對象
查找在/目錄下面更改時間在5天之內的文件 # find / -mtime -5 查找在/目錄下面更改時間在3天之前的目錄 # find / -mtime +3
(9) -mountblog
表示不能垮文件系統查找,本身當前mount的文件系統查找 # find / -mount -name "alvinzeng.txt"
(10)使用exec或者ok來執行shell命令
find . -type f -exec ls -l {} \; 不提示
find . -type f –ok ls -l {} \; 安全提示
-ok 後面通常根刪除命令rml
find /test -type f –ok rm {} \;
(11) xargs命令
xargs比exec更加方便
find /test –name 「*.*」 | xargs ls –l
find練習題
一、查找/目錄下面的"passwd"文件和"profile" 文件
# find / -name "profile" -o -name "passwd" /usr/local/python3.5.1/lib/python3.5/site-packages/IPython/core/profile /usr/bin/passwd /usr/libexec/emacs/23.1/x86_64-redhat-linux-gnu/profile /usr/src/kernels/2.6.32-279.el6.x86_64/include/config/branch/profile /root/.vnc/passwd /root/apr-1.5.2/passwd /root/passwd /root/home/passwd /etc/pam.d/passwd /etc/passwd /etc/profile /passwd /home/passwd
二、查找/tmp下面權限爲755的文件
# find /tmp -perm 755
三、在/test/aa 下面建立一個名字爲 gongda.txt的文件,用find命令把/test/aa目 錄給忽略掉是否還能夠查找的到?
四、建立一個用戶和組 名爲gongda,而後 gongda.txt所屬用戶和所屬組所有修改爲 gongda,使用user和group查找gongda的用戶組,是否可 以查找到?而後刪除掉gongda用戶和組,再用nouser 和 nogroup查找沒有所屬和說是組的文件是否能夠 查到?
# useradd gongda # touch gongda.txt # chown gongda:gongda gongda.txt # find . -user gongda |xargs ls -l -rw-r--r-- 1 gongda gongda 0 5月 13 20:50 ./gongda.txt # find . -group gongda | xargs ls -l -rw-r--r-- 1 gongda gongda 0 5月 13 20:50 ./gongda.txt # userdel -r gongda # find . -nouser | xargs ls -l -rw-r--r-- 1 509 509 0 5月 13 20:50 ./gongda.txt # find -nogroup | xargs ls -l -rw-r--r-- 1 509 509 0 5月 13 20:50 ./gongda.txt
五、查找在/目錄下面更改時間在5天之內的文件
# find / -mtime -5
六、查找在/目錄下面更改時間在3天之前的目錄
# find / -mtime +3
七、在/opt/test/下面建立一個名字爲new.txt 文件。等5分鐘後再建立一個ok.txt的文件。使 用find命令查找比gongda.txt新比ok.txt舊的 文件。
八、使用find查找/home下面全部的目錄
# find /home -type d |xargs file /home: directory /home/user25: directory /home/user25/.mozilla: directory
九、使用find查找/home下面全部的文件,非目 錄
# find /home ! -type d |xargs file
十、使用find查找/etc/ 下面全部的鏈接文件
# find /etc -type l
十一、查找/etc/目錄下面大於1M的文件,而且把 文件大小信息列出來。
# find /etc/ -size +1M |xargs du -sh 2.0M /etc/gconf/gconf.xml.defaults/%gconf-tree.xml 6.3M /etc/selinux/targeted/policy/policy.24 6.3M /etc/selinux/targeted/modules/active/policy.kern
十二、查找/etc/目錄下面小於500K的文件,而且 把文件大小信息列出來
# find /etc/ -size -500k |xargs du -sh
1三、查找/opt/test 子目錄下面的gongda.txt 文件
# find "./test" -depth -name gongda.txt ./test/gongda.txt
1四、檢查系統有幾個掛在的文件系統,好比/ 和/home是分開的,那麼在/home/建立一個sxgongda.txt文件。使用find 參數mount查找/目錄是否能夠查到sxgongda.txt文件?
1五、查詢/opt/下面的gongda.txt文件,而且使 用exec 列出它的詳細信息。
# find /opt/ -name gongda.txt -exec ls -l {} \; -rw-r--r-- 1 509 509 0 5月 13 20:50 /opt/rh/gongda.txt -rw-r--r-- 1 root root 0 5月 13 21:31 /opt/rh/test/gongda.txt
1六、使用什麼參數能夠每次find後跟系統命令 時給出安全提示?# -ok