find命令的使用

find搜索文件系統、實時搜索
find [目錄] [條件] [動作]

[目錄]
不輸入代表當前目錄
例:
find
find /boot

[條件]
用戶和組:-user -group
例:查找home目錄下所有的屬於指定的文件
[root@localhost ~]# find /home/ -user swk

類型:-type ( f 文件 , d 目錄 , l 連接 , p 管道 ,c 字符文件 ,b 塊文件 ,s socket文件 )
[root@localhost ~]# find /home/ -type f
[root@localhost ~]# find /home/ -type d

文件名:-name
[root@localhost ~]# useradd user1
[root@localhost ~]# useradd user2
[root@localhost ~]# useradd vipuser
[root@localhost ~]# touch /home/user1/userabc
[root@localhost ~]# find /home/ -name user
/home/user1
/home/user1/userabc
/home/user2
/home/vipuser

大小:-size +NM 大於N兆 -NM 小於N兆
例:找到boot目錄下大於4M文件
[root@localhost ~]# find /boot/ -size +4M

find / -amin -10 # 查找在系統中最後10分鐘訪問的文件
find / -atime -2 # 查找在系統中最後48小時訪問的文件
find / -empty # 查找在系統中爲空的文件或者文件夾
find / -group cat # 查找在系統中屬於 groupcat的文件
find / -mmin -5 # 查找在系統中最後5分鐘裏修改過的文件
find / -mtime -1 #查找在系統中最後24小時裏修改過的文件
find / -nouser #查找在系統中屬於作廢用戶的文件
find / -user fred #查找在系統中屬於FRED這個用戶的文件
時間: -mtime -atime -ctime
擴展:Linux系統中ctime , atime ,mtime 有什麼區別
[root@localhost ~]# touch a.txt
[root@localhost ~]# stat a.txt
File: ‘a.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 36433014 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-02-20 07:55:13.285056713 -0500
Modify: 2016-02-20 07:55:13.285056713 -0500
Change: 2016-02-20 07:55:13.285056713 -0500

ctime:“改變時間(change time)”
mtime :“修改時間(modification time)”
atime :“訪問時間(access time)”

例:
改變和修改之間的區別在於是改文件的屬性還是更改它的內容。
chmod a-w myfile,那麼這是一個改變;改變的ctime
echo aaa > bajie 那麼這是一個修改。mtime :“修改時間
atime,改變是文件的索引節點發生了改變;mtime 修改是文本本身的內容發生了變化。

總結:當文件的屬性發生修改時,ctime
當文件內容發生修改時,mtime,ctime
當文件被訪問時,atime

[root@localhost ~]# touch a.txt
[root@localhost ~]# chmod +x a.txt #ctime發生改變
[root@localhost ~]# echo aaa > a.txt #mtime ,ctime發生改變
[root@localhost ~]# cat a.txt #atime發生改變

ls(1) 命令可用來列出文件的 atime、ctime 和 mtime。
ls -lc filename         列出文件的 ctime ll -c
ls -lu filename         列出文件的 atime ll -u
ls -l filename          列出文件的 mtime  ll

[root@localhost ~]# date -s 2016-2-23
Tue Feb 23 00:00:00 EST 2016
Mtime +(時間之錢) -(時間之內)
[root@localhost ~]# find /root/ -mtime +1 | grep a.txt
注:查找出root目錄24小時之前創建的文件

[root@localhost ~]# find /root/ -mtime +2 | grep a.txt
注:查找出root目錄48小時之前創建的文件

[root@localhost ~]# find /root/ -mtime -3 | grep a.txt
注:查找root目錄下72小時之內創建的文件

權限:-perm
[root@localhost ~]# find /boot/ -perm 755 #等於0775權限的文件或目錄
SUID 4,SGID 2 ,sticky 1

[root@localhost ~]# find /tmp/ -perm -777 #至少有777權限的文件或目錄

例:
[root@localhost ~]# mkdir ccc
[root@localhost ~]# chmod 777 ccc
[root@localhost ~]# mkdir test
[root@localhost ~]# chmod 1777 test/
[root@localhost ~]# touch b.sh
[root@localhost ~]# chmod 4777 b.sh

[root@localhost ~]# find /root/ -perm 777
/root/ccc
[root@localhost ~]# find /root/ -perm 1777
/root/test
[root@localhost ~]# find /root/ -perm 4777
/root/b.sh
[root@localhost ~]# find /root/ -perm -777
/root/ccc
/root/test
/root/b.sh

查找的目錄深度:
[root@localhost ~]# find /boot/ -maxdepth 2
#只查找目錄第二層的文件和目錄

多條件:
-a -o ! 或 -and -or -not
[root@localhost ~]# find /boot/ -size +4M -a -size -8M
注:找出來boot目錄下文件大小在4~8M之間的文件或目錄
[root@localhost ~]# find -type f -a -perm /o+w
./b.sh

[root@localhost ~]# find ! -type f -a -perm -001

[動作]
-ls
-ok
-exec
xargs
-print
-printf
[root@localhost ~]# touch test
[root@localhost ~]# cp /etc/passwd test/
[root@localhost ~]# cp -r /boot/ test/

[root@localhost ~]# find /root/test/ -type f -exec rm {} \;
或者:
[root@localhost ~]# find /root/test/ -type f | xargs rm -rf
參數解釋:
-exec 執行命令
rm 要執行的命令
{} 表示find -type f 查找出來了文件內容
\; {} 和 \;之間要有空格。 固定語法,就是以這個結尾

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章