婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > linux下find查找命令用法

linux下find查找命令用法

熱門(mén)標(biāo)簽:AI電銷(xiāo)機(jī)器人PHP源碼 沈陽(yáng)藝匯通地圖標(biāo)注 ai電銷(xiāo)機(jī)器人自動(dòng)篩選意向客戶 智能電話機(jī)器人金融 打電話機(jī)器人是什么軟件 思茅地圖標(biāo)注app 騰訊美術(shù)館地圖標(biāo)注 鄭州智能外呼系統(tǒng)招商 孝感營(yíng)銷(xiāo)電銷(xiāo)機(jī)器人源碼

Linux下find命令在目錄結(jié)構(gòu)中搜索文件,并執(zhí)行指定的操作。Linux下find命令提供了相當(dāng)多的查找條件,功能很強(qiáng)大。由于find具有強(qiáng)大的功能,所以它的選項(xiàng)也很多,其中大部分選項(xiàng)都值得我們花時(shí)間來(lái)了解一下。即使系統(tǒng)中含有網(wǎng)絡(luò)文件系統(tǒng)( NFS),find命令在該文件系統(tǒng)中同樣有效,只你具有相應(yīng)的權(quán)限。 在運(yùn)行一個(gè)非常消耗資源的find命令時(shí),很多人都傾向于把它放在后臺(tái)執(zhí)行,因?yàn)楸闅v一個(gè)大的文件系統(tǒng)可能會(huì)花費(fèi)很長(zhǎng)的時(shí)間(這里是指30G字節(jié)以上的文件系統(tǒng))。

1.命令格式:

find pathname -options [-print -exec -ok ...]

2.命令功能:

用于在文件樹(shù)種查找文件,并作出相應(yīng)的處理

3.命令參數(shù):

pathname: find命令所查找的目錄路徑。例如用.來(lái)表示當(dāng)前目錄,用/來(lái)表示系統(tǒng)根目錄。
-print: find命令將匹配的文件輸出到標(biāo)準(zhǔn)輸出。
-exec: find命令對(duì)匹配的文件執(zhí)行該參數(shù)所給出的shell命令。相應(yīng)命令的形式為'command' {  } \;,注意{   }和\;之間的空格。
-ok: 和-exec的作用相同,只不過(guò)以一種更為安全的模式來(lái)執(zhí)行該參數(shù)所給出的shell命令,在執(zhí)行每一個(gè)命令之前,都會(huì)給出提示,讓用戶來(lái)確定是否執(zhí)行。

4.命令選項(xiàng):

-name   按照文件名查找文件。
-perm   按照文件權(quán)限來(lái)查找文件。
-prune  使用這一選項(xiàng)可以使find命令不在當(dāng)前指定的目錄中查找,如果同時(shí)使用-depth選項(xiàng),那么-prune將被find命令忽略。
-user   按照文件屬主來(lái)查找文件。
-group  按照文件所屬的組來(lái)查找文件。
-mtime -n +n  按照文件的更改時(shí)間來(lái)查找文件, - n表示文件更改時(shí)間距現(xiàn)在n天以內(nèi),+ n表示文件更改時(shí)間距現(xiàn)在n天以前。find命令還有-atime和-ctime 選項(xiàng),但它們都和-m time選項(xiàng)。
-nogroup  查找無(wú)有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。
-nouser   查找無(wú)有效屬主的文件,即該文件的屬主在/etc/passwd中不存在。
-newer file1 ! file2  查找更改時(shí)間比文件file1新但比文件file2舊的文件。
-type  查找某一類(lèi)型的文件,諸如:
b - 塊設(shè)備文件。
d - 目錄。
c - 字符設(shè)備文件。
p - 管道文件。
l - 符號(hào)鏈接文件。
f - 普通文件。
-size n:[c] 查找文件長(zhǎng)度為n塊的文件,帶有c時(shí)表示文件長(zhǎng)度以字節(jié)計(jì)。-depth:在查找文件時(shí),首先查找當(dāng)前目錄中的文件,然后再在其子目錄中查找。
-fstype:查找位于某一類(lèi)型文件系統(tǒng)中的文件,這些文件系統(tǒng)類(lèi)型通??梢栽谂渲梦募?etc/fstab中找到,該配置文件中包含了本系統(tǒng)中有關(guān)文件系統(tǒng)的信息。
-mount:在查找文件時(shí)不跨越文件系統(tǒng)mount點(diǎn)。
-follow:如果find命令遇到符號(hào)鏈接文件,就跟蹤至鏈接所指向的文件。
-cpio:對(duì)匹配的文件使用cpio命令,將這些文件備份到磁帶設(shè)備中。

另外,下面三個(gè)的區(qū)別:

-amin n   查找系統(tǒng)中最后N分鐘訪問(wèn)的文件
-atime n  查找系統(tǒng)中最后n*24小時(shí)訪問(wèn)的文件
-cmin n   查找系統(tǒng)中最后N分鐘被改變文件狀態(tài)的文件
-ctime n  查找系統(tǒng)中最后n*24小時(shí)被改變文件狀態(tài)的文件
-mmin n   查找系統(tǒng)中最后N分鐘被改變文件數(shù)據(jù)的文件
-mtime n  查找系統(tǒng)中最后n*24小時(shí)被改變文件數(shù)據(jù)的文件

5.使用實(shí)例:

實(shí)例1:查找指定時(shí)間內(nèi)修改過(guò)的文件

命令:
           find -atime -2

輸出:


復(fù)制代碼
代碼如下:

[root@peidachang ~]# find -atime -2
.
./logs/monitor
./.bashrc
./.bash_profile
./.bash_history

說(shuō)明:

超找48小時(shí)內(nèi)修改過(guò)的文件

實(shí)例2:根據(jù)關(guān)鍵字查找

命令:

find . -name "*.log"

輸出:


復(fù)制代碼
代碼如下:

[root@localhost test]# find . -name "*.log"
./log_link.log
./log2014.log
./test4/log3-2.log
./test4/log3-3.log
./test4/log3-1.log
./log2013.log
./log2012.log
./log.log
./test5/log5-2.log
./test5/log5-3.log
./test5/log.log
./test5/log5-1.log
./test5/test3/log3-2.log
./test5/test3/log3-3.log
./test5/test3/log3-1.log
./test3/log3-2.log
./test3/log3-3.log
./test3/log3-1.log

說(shuō)明:

在當(dāng)前目錄查找 以.log結(jié)尾的文件。 ". "代表當(dāng)前目錄

實(shí)例3:按照目錄或文件的權(quán)限來(lái)查找文件

命令:

find /opt/soft/test/ -perm 777

輸出:


復(fù)制代碼
代碼如下:

[root@localhost test]# find /opt/soft/test/ -perm 777
/opt/soft/test/log_link.log
/opt/soft/test/test4
/opt/soft/test/test5/test3
/opt/soft/test/test3

說(shuō)明:

查找/opt/soft/test/目錄下 權(quán)限為 777的文件

實(shí)例4:按類(lèi)型查找

命令:

find . -type f -name "*.log"

輸出:


復(fù)制代碼
代碼如下:

[root@localhost test]# find . -type f -name "*.log"
./log2014.log
./test4/log3-2.log
./test4/log3-3.log
./test4/log3-1.log
./log2013.log
./log2012.log
./log.log
./test5/log5-2.log
./test5/log5-3.log
./test5/log.log
./test5/log5-1.log
./test5/test3/log3-2.log
./test5/test3/log3-3.log
./test5/test3/log3-1.log
./test3/log3-2.log
./test3/log3-3.log
./test3/log3-1.log
[root@localhost test]#

說(shuō)明:

查找當(dāng)目錄,以.log結(jié)尾的普通文件

實(shí)例5:查找當(dāng)前所有目錄并排序

命令:

find . -type d | sort

輸出:


復(fù)制代碼
代碼如下:

[root@localhost test]# find . -type d | sort
.
./scf
./scf/bin
./scf/doc
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/info
./scf/service/deploy/product
./test3
./test4
./test5
./test5/test3
[root@localhost test]#

實(shí)例6:按大小查找文件

命令:

find . -size +1000c -print

輸出:


復(fù)制代碼
代碼如下:

[root@localhost test]# find . -size +1000c -print
.
./test4
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
./log2012.log
./test5
./test5/test3
./test3
[root@localhost test]#

說(shuō)明:

查找當(dāng)前目錄大于1K的文件

一、Linux中find常見(jiàn)用法示例

·find    path    -option    [    -print ]    [ -exec    -ok    command ]    {} \;
#-print 將查找到的文件輸出到標(biāo)準(zhǔn)輸出
#-exec    command    {} \;       -----將查到的文件執(zhí)行command操作,{} 和 \;之間有空格
#-ok 和-exec相同,只不過(guò)在操作前要詢用戶 ==================================================== -name    filename               #查找名為filename的文件
-perm                         #按執(zhí)行權(quán)限來(lái)查找
-user     username              #按文件屬主來(lái)查找
-group groupname              #按組來(lái)查找
-mtime    -n +n                 #按文件更改時(shí)間來(lái)查找文件,-n指n天以內(nèi),+n指n天以前
-atime     -n +n                #按文件訪問(wèn)時(shí)間來(lái)查GIN: 0px">-perm                          #按執(zhí)行權(quán)限來(lái)查找
-user     username              #按文件屬主來(lái)查找
-group groupname              #按組來(lái)查找
-mtime    -n +n                 #按文件更改時(shí)間來(lái)查找文件,-n指n天以內(nèi),+n指n天以前
-atime     -n +n                #按文件訪問(wèn)時(shí)間來(lái)查找文件,-n指n天以內(nèi),+n指n天以前
-ctime     -n +n                #按文件創(chuàng)建時(shí)間來(lái)查找文件,-n指n天以內(nèi),+n指n天以前
-nogroup                      #查無(wú)有效屬組的文件,即文件的屬組在/etc/groups中不存在
-nouser                       #查無(wú)有效屬主的文件,即文件的屬主在/etc/passwd中不存
-newer    f1 !f2                找文件,-n指n天以內(nèi),+n指n天以前
-ctime     -n +n                #按文件創(chuàng)建時(shí)間來(lái)查找文件,-n指n天以內(nèi),+n指n天以前
-nogroup                      #查無(wú)有效屬組的文件,即文件的屬組在/etc/groups中不存在
-nouser                       #查無(wú)有效屬主的文件,即文件的屬主在/etc/passwd中不存
-newer    f1 !f2                #查更改時(shí)間比f(wàn)1新但比f(wàn)2舊的文件
-type      b/d/c/p/l/f          #查是塊設(shè)備、目錄、字符設(shè)備、管道、符號(hào)鏈接、普通文件
-size       n[c]                #查長(zhǎng)度為n塊[或n字節(jié)]的文件
-depth                        #使查找在進(jìn)入子目錄前先行查找完本目錄
-fstype                       #查更改時(shí)間比f(wàn)1新但比f(wàn)2舊的文件
-mount                        #查文件時(shí)不跨越文件系統(tǒng)mount點(diǎn)
-follow                       #如果遇到符號(hào)鏈接文件,就跟蹤鏈接所指的文件
-cpio                         #對(duì)匹配的文件使用cpio命令,將他們備份到磁帶設(shè)備中
-prune                        #忽略某個(gè)目錄 ====================================================
$find    ~    -name    "*.txt"    -print      #在$HOME中查.txt文件并顯示
$find    .     -name    "*.txt"    -print

$find    .     -name    "[A-Z]*"    -pri26nbsp;     #對(duì)匹配的文件使用cpio命令,將他們備份到磁帶設(shè)備中
-prune                                #忽略某個(gè)目錄 $find    .     -name    "[A-Z]*"    -print    #查以大寫(xiě)字母開(kāi)頭的文件
$find    /etc    -name    "host*"    -print #查以host開(kāi)頭的文件
$find    .    -name    "[a-z][a-z][0--9][0--9].txt"     -print    #查以兩個(gè)小寫(xiě)字母和兩個(gè)數(shù)字開(kāi)頭的txt文件
$find .    -perm    755    -print
$find    .    -perm -007    -exec ls -l {} \;    #查所有用戶都可讀寫(xiě)執(zhí)行的文件同-perm 777
$find    . -type d    -print   打印目錄結(jié)構(gòu)
$find    .   !    -type    d    -print  打印非目錄文件 find /usr/include -name '*.h' -exec grep AF_INEF6 {} \; 因grep無(wú)法遞歸搜索子目錄,故可以和find相結(jié)合使用。 在/usr/include 所有子目錄中的.h文件中找字串AF_INEF6
$find    .    -type l    -print $find    .    -size    +1000000c    -print         #查長(zhǎng)度大于1Mb的文件
$find    .    -size    100c          -print        # 查長(zhǎng)度為100c的文件
$find    .    -size    +10    -print               #查長(zhǎng)度超過(guò)期作廢10塊的文件(1塊=512字節(jié)) $cd /
$find    etc    home    apps     -depth    -print    | cpio    -ivcdC65536    -o    /dev/rmt0
$find    /etc -name "passwd*"    -exec grep    "cnscn"    {}    \;    #看是否存在cnscn用戶
$find . -name "yao*"    | xargs file
$find    . -name "yao*"    |    xargs    echo     "" > /tmp/core.log
$find    . -name "yao*"    | xargs    chmod    o-w ====================================================== find    -name april*                        在當(dāng)前目錄下查找以april開(kāi)始的文件
find    -name    april*    fprint file          在當(dāng)前目錄下查找以april開(kāi)始的文件,并把結(jié)果輸出到file中
find    -name ap* -o -name may*    查找以ap或may開(kāi)頭的文件
find    /mnt    -name tom.txt    -ftype vfat    在/mnt下查找名稱為tom.txt且文件系統(tǒng)類(lèi)型為vfat的文件
find    /mnt    -name t.txt ! -ftype vfat     在/mnt下查找名稱為tom.txt且文件系統(tǒng)類(lèi)型不為vfat的文件
find    /tmp    -name wa* -type l             在/tmp下查找名為wa開(kāi)頭且類(lèi)型為符號(hào)鏈接的文件
find    /home    -mtime    -2                   在/home下查最近兩天內(nèi)改動(dòng)過(guò)的文件
find /home     -atime -1                    查1天之內(nèi)被存取過(guò)的文件
find /home -mmin     +60                    在/home下查60分鐘前改動(dòng)過(guò)的文件
find /home    -amin    +30                    查最近30分鐘前被存取過(guò)的文件
find /home    -newer    tmp.txt               在/home下查更新時(shí)間比tmp.txt近的文件或目錄
find /home    -anewer    tmp.txt              在/home下查存取時(shí)間比tmp.txt近的文件或目錄
find    /home    -used    -2                    列出文件或目錄被改動(dòng)過(guò)之后,在2日內(nèi)被存取過(guò)的文件或目錄
find    /home    -user cnscn                  列出/home目錄內(nèi)屬于用戶cnscn的文件或目錄
find    /home    -uid    +501                   列出/home目錄內(nèi)用戶的識(shí)別碼大于501的文件或目錄
find    /home    -group    cnscn                列出/home內(nèi)組為cnscn的文件或目錄
find    /home    -gid 501                     列出/home內(nèi)組id為501的文件或目錄
find    /home    -nouser                      列出/home內(nèi)不屬于本地用戶的文件或目錄
find    /home    -nogroup                     列出/home內(nèi)不屬于本地組的文件或目錄
find    /home     -name tmp.txt     -maxdepth    4    列出/home內(nèi)的tmp.txt 查時(shí)深度最多為3層
find    /home    -name tmp.txt    -mindepth    3    從第2層開(kāi)始查
find    /home    -empty                       查找大小為0的文件或空目錄
find    /home    -size    +512k                 查大于512k的文件
find    /home    -size    -512k                 查小于512k的文件
find    /home    -links    +2                   查硬連接數(shù)大于2的文件或目錄
find    /home    -perm    0700                  查權(quán)限為700的文件或目錄
find    /tmp    -name tmp.txt    -exec cat {} \;
find    /tmp    -name    tmp.txt    -ok    rm {} \; find     /    -amin     -10         # 查找在系統(tǒng)中最后10分鐘訪問(wèn)的文件
find     /    -atime    -2           # 查找在系統(tǒng)中最后48小時(shí)訪問(wèn)的文件
find     /    -empty                # 查找在系統(tǒng)中為空的文件或者文件夾
find     /    -group    cat          # 查找在系統(tǒng)中屬于 groupcat的文件
find     /    -mmin    -5           # 查找在系統(tǒng)中最后5分鐘里修改過(guò)的文件
find     /    -mtime    -1          #查找在系統(tǒng)中最后24小時(shí)里修改過(guò)的文件
find     /    -nouser               #查找在系統(tǒng)中屬于作廢用戶的文件
find     /    -user     fred         #查找在系統(tǒng)中屬于FRED這個(gè)用戶的文件

查當(dāng)前目錄下的所有普通文件
-------------------------------------------------------------------------------- # find . -type f -exec ls -l {} \;
-rw-r--r--      1 root       root          34928 2003-02-25    ./conf/httpd.conf
-rw-r--r--      1 root       root          12959 2003-02-25    ./conf/magic
-rw-r--r--      1 root       root            180 2003-02-25    ./conf.d/README
查當(dāng)前目錄下的所有普通文件,并在- e x e c選項(xiàng)中使用ls -l命令將它們列出
=================================================
在/ l o g s目錄中查找更改時(shí)間在5日以前的文件并刪除它們:
$ find logs -type f -mtime +5 -exec    -ok    rm {} \;
=================================================
查詢當(dāng)天修改過(guò)的文件
[root@book class]# find    ./    -mtime    -1    -type f    -exec    ls -l    {} \;
=================================================
查詢文件并詢問(wèn)是否要顯示
[root@book class]# find    ./    -mtime    -1    -type f    -ok    ls -l    {} \;  
ls ... ./classDB.inc.php > ? y
-rw-r--r--      1 cnscn      cnscn         13709    1月 12 12:22 ./classDB.inc.php
[root@book class]# find    ./    -mtime    -1    -type f    -ok    ls -l    {} \;  
ls ... ./classDB.inc.php > ? n
[root@book class]# =================================================
查詢并交給awk去處理
[root@book class]# who    |    awk    '{print $1"\t"$2}'
cnscn     pts/0 =================================================
awk---grep---sed [root@book class]# df    -k |    awk '{print $1}' |    grep    -v    'none' |    sed    s"/\/dev\///g"
文件系統(tǒng)
sda2
sda1
[root@book class]# df    -k |    awk '{print $1}' |    grep    -v    'none'
文件系統(tǒng)
/dev/sda2
/dev/sda1


1)在/tmp中查找所有的*.h,并在這些文件中查找“SYSCALL_VECTOR",最后打印出所有包含"SYSCALL_VECTOR"的文件名 A) find    /tmp    -name    "*.h"    | xargs    -n50    grep SYSCALL_VECTOR
B) grep    SYSCALL_VECTOR    /tmp/*.h | cut     -d':'    -f1| uniq > filename
C) find    /tmp    -name "*.h"    -exec grep "SYSCALL_VECTOR"    {}    \; -print
2)find / -name filename -exec rm -rf {} \;
     find / -name filename -ok rm -rf {} \;
3)比如要查找磁盤(pán)中大于3M的文件:
find . -size +3000k -exec ls -ld {} ;
4)將find出來(lái)的東西拷到另一個(gè)地方
find *.c -exec cp '{}' /tmp ';' 如果有特殊文件,可以用cpio,也可以用這樣的語(yǔ)法:
find dir -name filename -print | cpio -pdv newdir
6)查找2004-11-30 16:36:37時(shí)更改過(guò)的文件
# A=`find ./ -name "*php"` |    ls -l --full-time $A 2>/dev/null | grep "2004-11-30 16:36:37

二、linux下find命令的用法

1. 基本用法:
      find / -name 文件名      find ver1.d ver2.d -name '*.c' -print    查找ver1.d,ver2.d *.c文件并打印      find . -type d -print 從當(dāng)前目錄查找,僅查找目錄,找到后,打印路徑名??捎糜诖蛴∧夸浗Y(jié)構(gòu)。
2. 無(wú)錯(cuò)誤查找:
      find / -name access_log 2 >/dev/null
3. 按尺寸查找:
      find / -size 1500c (查找1,500字節(jié)大小的文件,c表示字節(jié))
      find / -size +1500c (查找大于1,500字節(jié)大小的文件,+表示大于)    
      find / -size +1500c (查找小于1,500字節(jié)大小的文件,-表示小于)    
4. 按時(shí)間:
      find / -amin n 最后n分鐘
      find / -atime n 最后n天
      find / -cmin n 最后n分鐘改變狀態(tài)
      find / -ctime n 最后n天改變狀態(tài)
5. 其它:
      find / -empty 空白文件、空白文件夾、沒(méi)有子目錄的文件夾
      find / -false 查找系統(tǒng)中總是錯(cuò)誤的文件
      find / -fstype type 找存在于指定文件系統(tǒng)的文件,如type為ext2
      find / -gid n 組id為n的文件
      find / -group gname 組名為gname的文件
      find / -depth n 在某層指定目錄中優(yōu)先查找文件內(nèi)容
      find / -maxdepth levels 在某個(gè)層次目錄中按遞減方式查找
6. 邏輯
      -and 條件與 -or 條件或
7. 查找字符串
      find . -name '*.html' -exec grep 'mailto:'{}

標(biāo)簽:和田 甘孜 江蘇 淄博 延安 張掖 臨汾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux下find查找命令用法》,本文關(guān)鍵詞  linux,下,find,查找,命令,用法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《linux下find查找命令用法》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于linux下find查找命令用法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 冷水江市| 乌拉特前旗| 衡山县| 龙井市| 南华县| 乌兰察布市| 郎溪县| 洞口县| 邮箱| 遂溪县| 方城县| 汉源县| 鹤峰县| 台南市| 建阳市| 连山| 徐闻县| 云浮市| 河源市| 镇远县| 丰台区| 青川县| 镇巴县| 本溪| 锡林浩特市| 民丰县| 和田市| 安义县| 宜州市| 嘉义县| 西华县| 雷波县| 昌宁县| 南开区| 南雄市| 金乡县| 岳池县| 洛川县| 本溪| 阳原县| 靖远县|