天天看點

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 ~]# 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@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 查找出來了檔案内容

\; {} 和 \;之間要有空格。 固定文法,就是以這個結尾

本文轉自信自己belive51CTO部落格,原文連結: http://blog.51cto.com/11638205/2048830,如需轉載請自行聯系原作者

繼續閱讀