linux基礎⑨-find命令

以名稱查找

[root@lsy study]# find ./ -name "*eth0"		#查找當前目錄下名稱爲  *eth0
[root@lsy study]# find ./ -iname "*eth0"		#i忽略大小寫的查詢方式

以大小查找

#查找/etc目錄下文件大於5M,然後使用-ls參數以長格式顯示(-ls和系統的ls不是一個命令)
[root@lsy study]# find /etc -size +5M -ls
#查找/etc目錄下文件大於5M,使用系統的ls來以長格式顯示
[root@lsy study]# find /etc -size +5M |xargs ls -lh

以類型查找


查找當前目錄下類型是文件的,並忽略大小寫的名稱查找爲 *-eth0,都以長格式顯示
[root@lsy study]# find ./ -type f -iname "*-eth0" -ls
查找當前目錄下類型是目錄的,並忽略大小寫的名稱查找爲 *-eth0,都以長格式顯示
[root@lsy study]# find ./ -type d -iname "*-eth0" -ls

查找/bin下類型是鏈接文件的,忽略大小寫查找名稱爲b*的,以長格式顯示
[root@lsy study]# find /bin/ -type l -iname "b*" -ls
[root@lsy study]# find /dev/ -type b -ls
[root@lsy study]# find /dev/ -type c -ls
[root@lsy study]# find /etc/ -type f -size +3M -name "hw*"

以時間查找

+7,以當前時間爲主,查找7天以前的內容
[root@lsy study]# find ./ -type f -name "file*" -mtime +7
[root@lsy study]# find ./ -type f -name "file*" -mtime +7 -delete

-7,查找最近7天的文件,會打印當天的文件
[root@lsy study]# find ./ -type f -name "file*" -mtime -7

找第7天文件
[root@lsy study]# find ./ -type f -mtime 7

以用戶查找

#查找home目錄下,類型是目錄的並且屬主是jack的,同時只查找一層
[root@lsy study]# find /home/ -maxdepth 1  -type d -user jack

#查找home目錄下,類型是目錄的並且屬組是hr的,同時只查找一層
[root@lsy study]# find /home/ -maxdepth 1  -type d -group hr -ls

#查找home目錄下,類型是目錄的並且屬主是jack屬組是hr的,同時只查找一層
[root@lsy study]# find /home/ -maxdepth 1 -type d -user jack -group hr -ls

#查找home目錄下,類型是目錄的要麼屬主是jack,要麼屬組是hr
[root@lsy study]# find /home/ -maxdepth 1 -type d -user jack -o -group hr|xargs ls -ld

#查找home目錄下,類型是目錄沒有屬主的
[root@lsy study]# find /home/ -maxdepth 1 -type d -nouser -ls
#查找home目錄下,類型是目錄沒有屬組的
[root@lsy study]# find /home/ -maxdepth 1 -type d -nogroup -ls

#查找home目錄下,類型是目錄沒有屬主或沒有屬組
[root@lsy study]# find /home/ -maxdepth 1 -type d -nouser -o  -nogroup |xargs ls -ld

以權限查找

精確查找文件的權限爲644
[root@lsy study]# find ./ -type f -perm 644
#包含444權限即可   即大於等於444
[root@lsy study]# find ./ -type f -name "file*" -perm -444
#查找全局可寫(每位權限必須包含w)
[root@lsy study]# find . -perm -222 -ls
#包含set uid
[root@lsy study]# find  /usr/sbin -perm -4000 -ls
#包含set gid
[root@lsy study]# find  /usr/sbin -perm -2000 -ls
#包含sticky
[root@lsy study]# find  /usr/sbin -perm -1000 -ls

包含動作

-delte,只能刪除文件,如果要刪除目錄,需要保證目錄爲空,否則無法刪除
	[root@lsy study]# find ./log/ -type f -name "*.log" -delete

-ok,可以執行任何自定義命令,但是會提示是否確認.
	[root@lsy study]# find /etc/ -name "ifcfg*" -ok cp -vp {} /tmp \;

-exec
	[root@lsy study]# find /etc/ -name "ifcfg*" -exec cp -vp {} /tmp \;
	[root@lsy study]# find log/ -type d -exec cp -rpv {} /tmp \;
	[root@lsy study]# find test/ -type f -exec rm -f {} \;

更多例子


1.查找/tmp目錄下,屬主不是root,且文件名不以c開頭的文件
[root@lsy study]# find /tmp/ ! -user root  ! -name "c*"
/tmp/ttt

2.查找/var目錄下屬主爲root,且屬組爲mail的所有文件
[root@lsy study]# find /var/ -type f  -user root -a -group mail -ls

3.查找/var目錄下不屬於root、lp、gdm組  的所有文件
[root@lsy study]# find /var/ -type f  ! \( -group root -o  -group lp -o  -group gdm \) |xargs ls -ld

4.查找/var目錄下最近一週修改過的文件,同時屬主不爲root,也不是postfix的文件
[root@lsy study]# find /var/ -type f -mtime -7  ! -user root ! -name "postfix"

5.查找/etc目錄下大於1M且類型爲普通文件的所有文件
[root@lsy study]# find /etc/ -type f -size +1M

6.將/etc/中的所有目錄(僅目錄)複製到/tmp下,目錄結構不變
[root@lsy study]# find /etc/ -type d -exec mkdir -p /tmp/{} \;

7.將/etc目錄複製到/var/tmp/,/var/tmp/etc的所有目錄權限777/var/tmp/etc目錄中所有文件權限666
[root@lsy study]# cp -rp /etc/ /var/tmp/
[root@lsy study]# find /var/tmp/etc/ -type d -exec chmod 777 {} \;
[[root@lsy study]# find /var/tmp/etc/ -type f -exec chmod 666 {} \;
[root@lsy study]# find /var/tmp/etc/ ! -type d -exec chmod 666 {} \;

8.保留/var/log/下最近7天的日誌文件,其他全部刪除
[root@lsy study]# find /var/log/ -type f -mtime +7 -delete

9.創建touch file{1..10}10個文件, 保留file9,其他一次全部刪除
[root@lsy study]# find ./ -type f ! -name "file9" -delete

10.
mkdir /root/dir1
touch /root/dir1/file{1..10}
find /root/dir1 -type f -name "file5"
find /root/dir1 ! -name "file5"
find /root/dir1 -name "file5" -o -name "file9"
find /root/dir1 -name "file5" -o -name "file9" -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;
find /root/dir1  ! \( -name "file4" -o -name "file8" \) -exec rm -vf {}  \; 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章