Linux常用命令之一:find

Linux常用命令之一:find

find,find在不指定查找目錄的情況下是對整個系統進行遍歷查找

使用格式 :   find  [指定查找目錄]  [查找規則]  [查找完後執行的action]

[指定查找目錄]例如:

[root@test ~]# find/etc -name "passwd"

/etc/passwd

/etc/pam.d/passwd

[查找規則]

1)根據文件名查找

#  -name       //根據文件名查找(精確查找)

#  -iname       //根據文件名查找,但是不區分大小寫

這裏另外介紹下文件名通配的知識

*表示  通配任意的字符

[root@test ~]# find/etc -type f -name "passwd*"

/etc/passwd

/etc/passwd-

/etc/pam.d/passwd     

 ?表示  通配任意的單個字符

[root@test ~]# find/etc -type f -name "passwd?"

/etc/passwd-

2),根據文件所屬用戶和組來查找文件

#  -user         //根據屬主來查找文件

#  -group        //根據屬組來查找文件

[root@test hejp]#find /home/ -user hejp              

/home/ddd

/home/ddd/.bash_logout

/home/ddd/.bashrc

/home/ddd/.bash_profile

/home/hejp

/home/hejp/.bash_logout

/home/hejp/.bashrc

/home/hejp/.bash_profile

3),根據uid  gid來查找用戶

#find  /tmp  -uid  500  //查找uid是500 的文件

#find  /tmp  -gid  1000// 查找gid是1000的文件

4),-a  and  -o  and  –not的使用

# -a 連接兩個不同的條件(兩個條件必須同時滿足)

[root@test ~]# find/server/scripts/ -name "*.sh" -a -user root

/server/scripts/ipv_server.sh  

# -o 連接兩個不同的條件(兩個條件滿足其一即可)

[root@test ~]# find/server/scripts/ -name "*.sh" -O -user root

/server/scripts/ipv_server.sh  

# -not 對條件取反的  

[root@test hejp]#find /home/ -name "*.test" -not -user hejp

/home/hejp/hello.test

5),根據文件時間戳的相關屬性來查找文件

我們可以使用stat命令來查看一個文件的時間信息 如下:

 [root@testhejp]# stat /etc/passwd

  File: `/etc/passwd'

  Size: 1467            Blocks: 8          IO Block: 4096   regular file

Device:803h/2051d      Inode: 146713      Links: 1

Access:(0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)

Access: 2016-03-1310:36:08.809835981 +0800

Modify: 2016-03-0321:59:49.717548713 +0800

Change: 2016-03-0321:59:49.719585578 +0800

#-atime

#-mtime

#-ctime

#-amin

#-mmin

#-cmin

  所以這裏atime,mtime,ctime就是分別對應的“最近一次訪問時間”“最近一次內容修改時間”“最近一次屬性修改時間”,這裏的atime的單位指的是“天”,amin的單位是分鐘  

#find  /tmp  –atime  +5    //表示查找在五天內沒有訪問過的文件

#find  /tmp  -atime  -5       //示查找在五天內訪問過的文件      

6),根據文件類型來查找文件

-type

f     // 普通文件

d     //目錄文件

l     //鏈接文件

b     //塊設備文件

c     //字符設備文件

p     //管道文件

s     //socket文件 

[root@test hejp]#find /home/hejp/ -type f

/home/hejp/hello.test

/home/hejp/.bash_logout

/home/hejp/.bashrc

/home/hejp/.bash_profile

 

7),根據大小來查找文件

-size

#find  /tmp  -size   2M           //查找在/tmp 目錄下等於2M的文件

#find  /tmp  -size  +2M           //查找在/tmp 目錄下大於2M的文件

#find  /tmp  -size  -2M           //查找在/tmp 目錄下小於2M的文件

8),根據文件權限查找文件

-perm

#find  /tmp  -perm  755           //查找在/tmp目錄下權限是755的文件

#find  /tmp  -perm  +222          //表示只要有一類用戶(屬主,屬組,其他)的匹配寫權限就行

#find  /tmp  -perm  -222          //表示必須所有類別用戶都滿足有寫權限

9),-nouser  and  -nogroup

#find  /  -nogroup–a –nouser       //在整個系統中查找既沒有屬主又沒有屬組的文件(這樣的文件通常是很危險的,作爲系統工程師的我們應該及時清除掉)

  [查找完執行的action]

#-print                              //默認情況下的動作

#-ls                                  //查找到後用ls 顯示出來

#-ok  [commend]             //查找後執行命令的時候詢問用戶是否要執行

# -exec[commend]            //查找後執行命令的時候不詢問用戶,直接執行

[root@test hejp]#find /home/hejp/ -name "*.test" -exec chmod 777 {} \;

[root@test hejp]#ll /home/hejp/

total 4

-rwxrwxrwx 1 rootroot 3 Mar  3 21:10 hello.test

這裏要注意{}的使用:替代查找到的文件

#find  /tmp  -atime  +30  –execrm –rf  {}  \ #刪除查找到的超過30天沒有訪問過文件

我們也可以使用xargs來對查找到的文件進一步操

fnd /server/backup –name”*.tar.gz” –mtime +7 |xargs rm -rf


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