linux中命令 which/whereis/locate/find 的使用總結(總結+轉載)

在linux中我們經常有查找的需求。查找的命令主要有四種,即which、whereis、locate、find。他們的主要作用如下所示:

which       查看可執行文件的位置 
       whereis    查看可執行文件、源代碼文件、幫助文件的文件的位置 
       locate       配合數據庫查看文件/文件夾目錄位置 (有文件更新,需先執行updatedb)
       find          實際搜尋硬盤查詢文件名稱 

由此可見,which和whereis命令查找的範圍是有限制的。而locate可以方便查找目錄,find可以查找文件(但是速度慢)。find命令的使用可以較爲複雜,可以根據需要進行適度的學習和使用。在工作總,可以根據自己需要,採用適當的命令類型。


以下對於以上命令的詳細介紹。


以下轉載自:http://www.cnblogs.com/peida/archive/2012/11/08/2759805.html


我們經常在linux要查找某個文件,但不知道放在哪裏了,可以使用下面的一些命令來搜索 
       which  查看可執行文件的位置
       whereis 查看文件的位置 
       locate   配合數據庫查看文件位置
       find   實際搜尋硬盤查詢文件名稱

which命令的作用是,在PATH變量指定的路徑中,搜索某個系統命令的位置,並且返回第一個搜索結果。也就是說,使用which命令,就可以看到某個系統命令是否存在,以及執行的到底是哪一個位置的命令。 

1.命令格式:

which 可執行文件名稱 

2.命令功能:

which指令會在PATH變量指定的路徑中,搜索某個系統命令的位置,並且返回第一個搜索結果

3.命令參數:

-n  指定文件名長度,指定的長度必須大於或等於所有文件中最長的文件名。

-p  與-n參數相同,但此處的包括了文件的路徑。

-w  指定輸出時欄位的寬度。

-V  顯示版本信息

4.使用實例:

實例1:查找文件、顯示命令路徑

命令:

which lsmod

輸出:

[root@localhost ~]# which pwd

/bin/pwd

[root@localhost ~]#  which adduser

/usr/sbin/adduser

[root@localhost ~]#

說明:

which 是根據使用者所配置的 PATH 變量內的目錄去搜尋可運行檔的!所以,不同的 PATH 配置內容所找到的命令當然不一樣的!

實例2:用 which 去找出 which

命令:

  which which

輸出:

[root@localhost ~]# which which

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot  --show-tilde'

     /usr/bin/which

[root@localhost ~]#

說明:

竟然會有兩個 which ,其中一個是 alias 這就是所謂的『命令別名』,意思是輸入 which 會等於後面接的那串命令!

實例3:找出 cd 這個命令

命令:

 which cd

輸出:

 

       說明:

cd 這個常用的命令竟然找不到啊!爲什麼呢?這是因爲 cd 是bash 內建的命令! 但是 which 默認是找 PATH 內所規範的目錄,所以當然一定找不到的!



轉載自:http://www.cnblogs.com/peida/archive/2012/11/09/2761928.html

whereis命令只能用於程序名的搜索,而且只搜索二進制文件(參數-b)、man說明文件(參數-m)和源代碼文件(參數-s)。如果省略參數,則返回所有信息。

和find相比,whereis查找的速度非常快,這是因爲linux系統會將 系統內的所有文件都記錄在一個數據庫文件中,當使用whereis和下面即將介紹的locate時,會從數據庫中查找數據,而不是像find命令那樣,通 過遍歷硬盤來查找,效率自然會很高。 

但是該數據庫文件並不是實時更新,默認情況下時一星期更新一次,因此,我們在用whereis和locate 查找文件時,有時會找到已經被刪除的數據,或者剛剛建立文件,卻無法查找到,原因就是因爲數據庫文件沒有被更新。 

1.命令格式:

whereis [-bmsu] [BMS 目錄名 -f ] 文件名

2.命令功能:

whereis命令是定位可執行文件、源代碼文件、幫助文件在文件系統中的位置。這些文件的屬性應屬於原始代碼,二進制文件,或是幫助文件。whereis 程序還具有搜索源代碼、指定備用搜索路徑和搜索不尋常項的能力。

3.命令參數:

-b   定位可執行文件。

-m   定位幫助文件。

-s   定位源代碼文件。

-u   搜索默認路徑下除可執行文件、源代碼文件、幫助文件以外的其它文件。

-B   指定搜索可執行文件的路徑。

-M   指定搜索幫助文件的路徑。

-S   指定搜索源代碼文件的路徑。

4.使用實例:

實例1:將和**文件相關的文件都查找出來

命令:

whereis svn

輸出:

[root@localhost ~]# whereis tomcat

tomcat:

[root@localhost ~]# whereis svn

svn: /usr/bin/svn /usr/local/svn /usr/share/man/man1/svn.1.gz

說明:

tomcat沒安裝,找不出來,svn安裝找出了很多相關文件

實例2:只將二進制文件 查找出來 

命令:

whereis -b svn

輸出: 

[root@localhost ~]# whereis -b svn

svn: /usr/bin/svn /usr/local/svn

[root@localhost ~]# whereis -m svn

svn: /usr/share/man/man1/svn.1.gz

[root@localhost ~]# whereis -s svn

svn:

[root@localhost ~]#

說明:

whereis -m svn 查出說明文檔路徑,whereis -s svn 找source源文件



以下轉載自:http://www.cnblogs.com/peida/archive/2012/11/12/2765750.html

locate 讓使用者可以很快速的搜尋檔案系統內是否有指定的檔案。其方法是先建立一個包括系統內所有檔案名稱及路徑的數據庫,之後當尋找時就只需查詢這個數據庫,而不必實際深入檔案系統之中了。在一般的 distribution 之中,數據庫的建立都被放在 crontab 中自動執行。

1.命令格式:

Locate [選擇參數] [樣式]

2.命令功能:

locate命令可以在搜尋數據庫時快速找到檔案,數據庫由updatedb程序來更新,updatedb是由cron daemon週期性建立的,locate命令在搜尋數據庫時比由整個由硬盤資料來搜尋資料來得快,但較差勁的是locate所找到的檔案若是最近才建立或 剛更名的,可能會找不到,在內定值中,updatedb每天會跑一次,可以由修改crontab來更新設定值。(etc/crontab)

locate指定用在搜尋符合條件的檔案,它會去儲存檔案與目錄名稱的數據庫內,尋找合乎範本樣式條件的檔案或目錄錄,可以使用特殊字元(如”*” 或”?”等)來指定範本樣式,如指定範本爲kcpa*ner, locate會找出所有起始字串爲kcpa且結尾爲ner的檔案或目錄,如名稱爲kcpartner若目錄錄名稱爲kcpa_ner則會列出該目錄下包括 子目錄在內的所有檔案。

locate指令和find找尋檔案的功能類似,但locate是透過update程序將硬盤中的所有檔案和目錄資料先建立一個索引數據庫,在 執行loacte時直接找該索引,查詢速度會較快,索引數據庫一般是由操作系統管理,但也可以直接下達update強迫系統立即修改索引數據庫。

3.命令參數:

-e   將排除在尋找的範圍之外。

-1  如果 是 1.則啓動安全模式。在安全模式下,使用者不會看到權限無法看到 的檔案。這會始速度減慢,因爲 locate 必須至實際的檔案系統中取得檔案的 權限資料。

-f   將特定的檔案系統排除在外,例如我們沒有到理要把 proc 檔案系統中的檔案 放在資料庫中。

-q  安靜模式,不會顯示任何錯誤訊息。

-n 至多顯示 n個輸出。

-r 使用正規運算式 做尋找的條件。

-o 指定資料庫存的名稱。

-d 指定資料庫的路徑

-h 顯示輔助訊息

-V 顯示程式的版本訊息

4.使用實例:

實例1:查找和pwd相關的所有文件

命令:

locate pwd

輸出:

peida-VirtualBox ~ # locate pwd

/bin/pwd

/etc/.pwd.lock

/sbin/unix_chkpwd

/usr/bin/pwdx

/usr/include/pwd.h

/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.py

/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.pyc

/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.py

/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyc

/usr/lib/syslinux/pwd.c32

/usr/share/help/C/empathy/irc-join-pwd.page

/usr/share/help/ca/empathy/irc-join-pwd.page

/usr/share/help/cs/empathy/irc-join-pwd.page

/usr/share/help/de/empathy/irc-join-pwd.page

/usr/share/help/el/empathy/irc-join-pwd.page

實例2: 搜索etc目錄下所有以sh開頭的文件 

命令:

locate /etc/sh

輸出:

peida-VirtualBox ~ # locate /etc/sh

/etc/shadow

/etc/shadow-

/etc/shells

peida-VirtualBox ~ #

實例3:搜索etc目錄下,所有以m開頭的文件

命令:

locate /etc/m

輸出:

peida-VirtualBox ~ # locate /etc/m

/etc/magic

/etc/magic.mime

/etc/mailcap

/etc/mailcap.order

/etc/manpath.config

/etc/mate-settings-daemon


    以下轉載自:http://www.cnblogs.com/peida/archive/2012/11/13/2767374.html
              

Linux下find命令在目錄結構中搜索文件,並執行指定的操作。Linux下find命令提供了相當多的查找條件,功能很強大。由於find具有強大的功能,所以它的選項也很多,其中大部分選項都值得我們花時間來了解一下。即使系統中含有網絡文件系統( NFS),find命令在該文件系統中同樣有效,只你具有相應的權限。 在運行一個非常消耗資源的find命令時,很多人都傾向於把它放在後臺執行,因爲遍歷一個大的文件系統可能會花費很長的時間(這裏是指30G字節以上的文件系統)。

1.命令格式:

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

2.命令功能:

用於在文件樹種查找文件,並作出相應的處理 

3.命令參數:

pathname: find命令所查找的目錄路徑。例如用.來表示當前目錄,用/來表示系統根目錄。 

-print: find命令將匹配的文件輸出到標準輸出。 

-exec: find命令對匹配的文件執行該參數所給出的shell命令。相應命令的形式爲'command' {  } \;,注意{   }和\;之間的空格。 

-ok: 和-exec的作用相同,只不過以一種更爲安全的模式來執行該參數所給出的shell命令,在執行每一個命令之前,都會給出提示,讓用戶來確定是否執行。

4.命令選項:

-name   按照文件名查找文件。

-perm   按照文件權限來查找文件。

-prune  使用這一選項可以使find命令不在當前指定的目錄中查找,如果同時使用-depth選項,那麼-prune將被find命令忽略。

-user   按照文件屬主來查找文件。

-group  按照文件所屬的組來查找文件。

-mtime -n +n  按照文件的更改時間來查找文件, - n表示文件更改時間距現在n天以內,+ n表示文件更改時間距現在n天以前。find命令還有-atime和-ctime 選項,但它們都和-m time選項。

-nogroup  查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。

-nouser   查找無有效屬主的文件,即該文件的屬主在/etc/passwd中不存在。

-newer file1 ! file2  查找更改時間比文件file1新但比文件file2舊的文件。

-type  查找某一類型的文件,諸如:

b - 塊設備文件。

d - 目錄。

c - 字符設備文件。

p - 管道文件。

l - 符號鏈接文件。

f - 普通文件。

-size n:[c] 查找文件長度爲n塊的文件,帶有c時表示文件長度以字節計。-depth:在查找文件時,首先查找當前目錄中的文件,然後再在其子目錄中查找。

-fstype:查找位於某一類型文件系統中的文件,這些文件系統類型通常可以在配置文件/etc/fstab中找到,該配置文件中包含了本系統中有關文件系統的信息。

-mount:在查找文件時不跨越文件系統mount點。

-follow:如果find命令遇到符號鏈接文件,就跟蹤至鏈接所指向的文件。

-cpio:對匹配的文件使用cpio命令,將這些文件備份到磁帶設備中。

另外,下面三個的區別:

-amin n   查找系統中最後N分鐘訪問的文件

-atime n  查找系統中最後n*24小時訪問的文件

-cmin n   查找系統中最後N分鐘被改變文件狀態的文件

-ctime n  查找系統中最後n*24小時被改變文件狀態的文件

-mmin n   查找系統中最後N分鐘被改變文件數據的文件

-mtime n  查找系統中最後n*24小時被改變文件數據的文件

5.使用實例:

實例1:查找指定時間內修改過的文件 

命令:

           find -atime -2

輸出:

[root@peidachang ~]# find -atime -2

.

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history

說明:

超找48小時內修改過的文件 

實例2:根據關鍵字查找 

命令:

find . -name "*.log"

輸出:

[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

說明:

在當前目錄查找 以.log結尾的文件。 ""代表當前目錄 

實例3:按照目錄或文件的權限來查找文件

命令:

find /opt/soft/test/ -perm 777

輸出:

[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

說明: 

查找/opt/soft/test/目錄下 權限爲 777的文件

實例4:按類型查找 

命令:

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

輸出:

[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]#

說明:

查找當目錄,以.log結尾的普通文件 

實例5:查找當前所有目錄並排序

命令:

find . -type d | sort

輸出:

[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]#

實例6:按大小查找文件

命令:

find . -size +1000c -print

輸出:

[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]#

說明:查找當前目錄大於1K的文件


find是我們很常用的一個Linux命令,但是我們一般查找出來的並不僅僅是看看而已,還會有進一步的操作,這個時候exec的作用就顯現出來了。 

exec解釋:

-exec  參數後面跟的是command命令,它的終止是以;爲結束標誌的,所以這句命令後面的分號是不可缺少的,考慮到各個系統中分號會有不同的意義,所以前面加反斜槓。

{}   花括號代表前面find查找出來的文件名。

使用find時,只要把想要的操作寫在一個文件裏,就可以用exec來配合find查找,很方便的在有些操作系統中只允許-exec選項執行諸如l s或ls -l這樣的命令。大多數用戶使用這一選項是爲了查找舊文件並刪除它們。建議在真正執行rm命令刪除文件之前,最好先用ls命令看一下,確認它們是所要刪除的文件。 exec選項後面跟隨着所要執行的命令或腳本,然後是一對兒{ },一個空格和一個\,最後是一個分號。爲了使用exec選項,必須要同時使用print選項。如果驗證一下find命令,會發現該命令只輸出從當前路徑起的相對路徑及文件名

實例1:ls -l命令放在find命令的-exec選項中 

命令:

find . -type f -exec ls -l {} \;

輸出: 

[root@localhost test]# find . -type f -exec ls -l {} \; 

-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log

-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log

-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log

-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log

-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log

[root@localhost test]#

說明: 

上面的例子中,find命令匹配到了當前目錄下的所有普通文件,並在-exec選項中使用ls -l命令將它們列出。

實例2:在目錄中查找更改時間在n日以前的文件並刪除它們

命令:

find . -type f -mtime +14 -exec rm {} \; 

輸出:

[root@localhost test]# ll

總計 328

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     33 10-28 16:54 log2013.log

-rw-r--r-- 1 root root    127 10-28 16:51 log2014.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log

-rw-r--r-- 1 root root     25 10-28 17:02 log.log

-rw-r--r-- 1 root root     37 10-28 17:07 log.txt

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 10-28 14:47 test3

drwxrwxrwx 2 root root   4096 10-28 14:47 test4

[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;

[root@localhost test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# 

說明

在shell中用任何方式刪除文件之前,應當先查看相應的文件,一定要小心!當使用諸如mv或rm命令時,可以使用-exec選項的安全模式。它將在對每個匹配到的文件進行操作之前提示你。 

實例3:在目錄中查找更改時間在n日以前的文件並刪除它們在刪除之前先給出提示

命令:

find . -name "*.log" -mtime +5 -ok rm {} \;

輸出:

[root@localhost test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} \;

< rm ... ./log_link.log > ? y

< rm ... ./log2012.log > ? n

[root@localhost test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

說明

面的例子中, find命令在當前目錄中查找所有文件名以.log結尾、更改時間在5日以上的文件,並刪除它們,只不過在刪除之前先給出提示。 按y鍵刪除文件,按n鍵不刪除。 

 

實例4:-exec使用grep命令

命令:

find /etc -name "passwd*" -exec grep "root" {} \;

輸出:

[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} \;

root:x:0:0:root:/root:/bin/bash

root:x:0:0:root:/root:/bin/bash

[root@localhost test]#

說明:

任何形式的命令都可以在-exec選項中使用。  在面的例子中我們使用grep命令。find命令首先匹配所有文件名爲“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然後執行grep命令看看在這些文件中是否存在一個root用戶。

實例5:查找文件移動到指定目錄  

命令:

find . -name "*.log" -exec mv {} .. \;

輸出:

[root@localhost test]# ll

總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-12 22:49 test3

drwxrwxr-x 2 root root 4096 11-12 19:32 test4

[root@localhost test]# cd test3/

[root@localhost test3]# ll

總計 304

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;

[root@localhost test3]# ll

總計 0[root@localhost test3]# cd ..

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:50 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

實例6:用exec選項執行cp命令  

命令:

find . -name "*.log" -exec cp {} test3 \;

輸出:

[root@localhost test3]# ll

總計 0[root@localhost test3]# cd ..

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:50 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -name "*.log" -exec cp {} test3 \;

cp: “./test3/log2014.log” 及 “test3/log2014.log” 爲同一文件

cp: “./test3/log2013.log” 及 “test3/log2013.log” 爲同一文件

cp: “./test3/log2012.log” 及 “test3/log2012.log” 爲同一文件

[root@localhost test]# cd test3

[root@localhost test3]# ll

總計 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log


在使用 find命令的-exec選項處理匹配到的文件時, find命令將所有匹配到的文件一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鐘之後,就會出現溢出錯誤。錯誤信息通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。  

find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然後是下一批,並如此繼續下去。  

在有些系統中,使用-exec選項會爲處理每一個匹配到的文件而發起一個相應的進程,並非將匹配到的文件全部作爲參數一次執行;這樣在有些情況下就會出現進程過多,系統性能下降的問題,因而效率不高; 而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數,還是分批取得參數,以及每一次獲取參數的數目都會根據該命令的選項及系統內核中相應的可調參數來確定。

使用實例:

實例1: 查找系統中的每一個普通文件,然後使用xargs命令來測試它們分別屬於哪類文件 

命令:

find . -type f -print | xargs file

輸出:

[root@localhost test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -type f -print | xargs file

./log2014.log: empty

./log2013.log: empty

./log2012.log: ASCII text

[root@localhost test]#

實例2:在整個系統中查找內存信息轉儲文件(core dump) ,然後把結果保存到/tmp/core.log 文件中

命令:

 find / -name "core" -print | xargs echo "" >/tmp/core.log

輸出:

[root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log

[root@localhost test]# cd /tmp

[root@localhost tmp]# ll

總計 16

-rw-r--r-- 1 root root 1524 11-12 22:29 core.log

drwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766

drwx------ 2 root root 4096 11-12 22:28 ssh-ykiRPk1815

drwx------ 2 root root 4096 11-03 07:11 vmware-root

實例3:在當前目錄下查找所有用戶具有讀、寫和執行權限的文件,並收回相應的寫權限

命令:

find . -perm -7 -print | xargs chmod o-w

輸出:

[root@localhost test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -perm -7 -print | xargs chmod o-w

[root@localhost test]# ll

總計 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 19:32 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

說明:

執行命令後,文件夾scf、test3和test4的權限都發生改變

實例4:用grep命令在所有的普通文件中搜索hostname這個詞

命令:

find . -type f -print | xargs grep "hostname"

輸出:

[root@localhost test]# find . -type f -print | xargs grep "hostname"

./log2013.log:hostnamebaidu=baidu.com

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

實例5:用grep命令在當前目錄下的所有普通文件中搜索hostnames這個詞

命令

find . -name \* -type f -print | xargs grep "hostnames"

輸出:

[root@peida test]# find . -name \* -type f -print | xargs grep "hostnames"

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

說明:

注意,在上面的例子中, \用來取消find命令中的*在shell中的特殊含義。  

實例6:使用xargs執行mv 

命令:

find . -name "*.log" | xargs -i mv {} test4

輸出:

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 22:54 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]# cd test4/

[root@localhost test4]# ll

總計 0[root@localhost test4]# cd ..

[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4

[root@localhost test]# ll

總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-13 05:50 test3

drwxrwxr-x 2 root root 4096 11-13 05:50 test4

[root@localhost test]# cd test4/

[root@localhost test4]# ll

總計 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test4]#

實例7:find後執行xargs提示xargs: argument line too long解決方法:

命令:

find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

輸出:

[root@pd test4]#  find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

rm -f 

[root@pdtest4]#

說明:

-l1是一次處理一個-t是處理之前打印出命令

 

實例8:使用-i參數默認的前面輸出用{}代替,-I參數可以指定其他代替字符,如例子中的[] 

命令:

輸出:

[root@localhost test]# ll

總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-13 05:50 test3

drwxrwxr-x 2 root root 4096 11-13 05:50 test4

[root@localhost test]# cd test4

[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..

[root@localhost test4]# ll

總計 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test4]# cd ..

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 05:50 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]#

說明:

使用-i參數默認的前面輸出用{}代替,-I參數可以指定其他代替字符,如例子中的[] 

實例9:xargs的-p參數的使用 

命令:

find . -name "*.log" | xargs -p -i mv {} ..

輸出:

[root@localhost test3]# ll

總計 0

-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

[root@localhost test3]# cd ..

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:06 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# cd test3

[root@localhost test3]#  find . -name "*.log" | xargs -p -i mv {} ..

mv ./log2015.log .. ?...y

[root@localhost test3]# ll

總計 0[root@localhost test3]# cd ..

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]#

說明:-p參數會提示讓你確認是否執行後面的命令,y執行,n不執行。


find一些常用參數的一些常用實例和一些具體用法和注意事項。

1.使用name選項:

文件名選項是find命令最常用的選項,要麼單獨使用該選項,要麼和其他選項一起使用。  可以使用某種文件名模式來匹配文件,記住要用引號將文件名模式引起來。  不管當前路徑是什麼,如果想要在自己的根目錄$HOME中查找文件名符合*.log的文件,使用~作爲 'pathname'參數,波浪號~代表了你的$HOME目錄。

find ~ -name "*.log" -print  

想要在當前目錄及子目錄中查找所有的‘ *.log‘文件,可以用: 

find . -name "*.log" -print  

想要的當前目錄及子目錄中查找文件名以一個大寫字母開頭的文件,可以用:  

find . -name "[A-Z]*" -print  

想要在/etc目錄中查找文件名以host開頭的文件,可以用:  

find /etc -name "host*" -print  

想要查找$HOME目錄中的文件,可以用:  

find ~ -name "*" -print 或find . -print  

要想讓系統高負荷運行,就從根目錄開始查找所有的文件。  

find / -name "*" -print  

如果想在當前目錄查找文件名以一個個小寫字母開頭,最後是4到9加上.log結束的文件:  

命令:

find . -name "[a-z]*[4-9].log" -print

輸出:

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# find . -name "[a-z]*[4-9].log" -print

./log2014.log

./log2015.log

./test4/log2014.log

[root@localhost test]#

2.用perm選項:

按照文件權限模式用-perm選項,按文件權限模式來查找文件的話。最好使用八進制的權限表示法。  

如在當前目錄下查找文件權限位爲755的文件,即文件屬主可以讀、寫、執行,其他用戶可以讀、執行的文件,可以用:  

[root@localhost test]# find . -perm 755 -print

.

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

[root@localhost test]#

 

還有一種表達方法:在八進制數字前面要加一個橫槓-,表示都匹配,如-007就相當於777,-005相當於555,

命令:

find . -perm -005

輸出:

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# find . -perm -005

.

./test4

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./test3

[root@localhost test]#

3.忽略某個目錄:

如果在查找文件時希望忽略某個目錄,因爲你知道那個目錄中沒有你所要查找的文件,那麼可以使用-prune選項來指出需要忽略的目錄。在使用-prune選項時要當心,因爲如果你同時使用了-depth選項,那麼-prune選項就會被find命令忽略。如果希望在test目錄下查找文件,但不希望在test/test3目錄下查找,可以用:  

命令:

find test -path "test/test3" -prune -o -print

輸出:

[root@localhost soft]# find test -path "test/test3" -prune -o -print

test

test/log2014.log

test/log2015.log

test/test4

test/test4/log2014.log

test/test4/log2013.log

test/test4/log2012.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

[root@localhost soft]#

4.使用find查找文件的時候怎麼避開某個文件目錄: 

實例1:test 目錄下查找不在test4子目錄之內的所有文件

命令:

find test -path "test/test4" -prune -o -print

輸出:

[root@localhost soft]# find test

test

test/log2014.log

test/log2015.log

test/test4

test/test4/log2014.log

test/test4/log2013.log

test/test4/log2012.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

test/test3

[root@localhost soft]# find test -path "test/test4" -prune -o -print

test

test/log2014.log

test/log2015.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

test/test3

[root@localhost soft]#

說明:

find [-path ..] [expression] 

在路徑列表的後面的是表達式 

-path "test" -prune -o -print 是 -path "test" -a -prune -o -print 的簡寫表達式按順序求值, -a 和 -o 都是短路求值,與 shell 的 && 和 || 類似如果 

-path "test" 爲真,則求值 -prune , -prune 返回真,與邏輯表達式爲真;否則不求值 -prune,與邏輯表達式爲假。如果 -path "test" -a -prune 爲假,則求值 -print ,-print返回真,或邏輯表達式爲真;否則不求值 -print,或邏輯表達式爲真。 

這個表達式組合特例可以用僞碼寫爲:

if -path "test" then  

-prune  

else  

-print  

實例2:避開多個文件夾:

命令:

find test \( -path test/test4 -o -path test/test3 \) -prune -o -print 

輸出:

[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -print

test

test/log2014.log

test/log2015.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

[root@localhost soft]#

 

說明:

圓括號表示表達式的結合。  \ 表示引用,即指示 shell 不對後面的字符作特殊解釋,而留給 find 命令去解釋其意義。  

實例3:查找某一確定文件,-name等選項加在-o 之後

命令:

find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print

輸出:

[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print

test/log2014.log

test/log2015.log

test/log2013.log

test/log2012.log

[root@localhost soft]#

5.使用user和nouser選項:

按文件屬主查找文件:

實例1:在$HOME目錄中查找文件屬主爲peida的文件 

命令:

find ~ -user peida -print  

實例2:在/etc目錄下查找文件屬主爲peida的文件: 

命令:

find /etc -user peida -print  

說明:

實例3:爲了查找屬主帳戶已經被刪除的文件,可以使用-nouser選項。在/home目錄下查找所有的這類文件

命令:

find /home -nouser -print

說明:

這樣就能夠找到那些屬主在/etc/passwd文件中沒有有效帳戶的文件。在使用-nouser選項時,不必給出用戶名; find命令能夠爲你完成相應的工作。

6.使用group和nogroup選項:

就像user和nouser選項一樣,針對文件所屬於的用戶組, find命令也具有同樣的選項,爲了在/apps目錄下查找屬於gem用戶組的文件,可以用:  

find /apps -group gem -print  

要查找沒有有效所屬用戶組的所有文件,可以使用nogroup選項。下面的find命令從文件系統的根目錄處查找這樣的文件:

find / -nogroup-print

7.按照更改時間或訪問時間等查找文件:

如果希望按照更改時間來查找文件,可以使用mtime,atime或ctime選項。如果系統突然沒有可用空間了,很有可能某一個文件的長度在此期間增長迅速,這時就可以用mtime選項來查找這樣的文件。  

用減號-來限定更改時間在距今n日以內的文件,而用加號+來限定更改時間在距今n日以前的文件。  

希望在系統根目錄下查找更改時間在5日以內的文件,可以用:

find / -mtime -5 -print

爲了在/var/adm目錄下查找更改時間在3日以前的文件,可以用:

find /var/adm -mtime +3 -print

8.查找比某個文件新或舊的文件:

如果希望查找更改時間比某個文件新但比另一個文件舊的所有文件,可以使用-newer選項。

它的一般形式爲:  

newest_file_name ! oldest_file_name  

其中,!是邏輯非符號。  

實例1:查找更改時間比文件log2012.log新但比文件log2017.log舊的文件

命令:

find -newer log2012.log ! -newer log2017.log

輸出:

[root@localhost test]# ll

總計 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

-rw-r--r-- 1 root root      0 11-16 14:41 log2016.log

-rw-r--r-- 1 root root      0 11-16 14:43 log2017.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-13 06:08 test3

drwxrwxr-x 2 root root   4096 11-13 05:50 test4

[root@localhost test]# find -newer log2012.log ! -newer log2017.log

.

./log2015.log

./log2017.log

./log2016.log

./test3

[root@localhost test]#

實例2:查找更改時間在比log2012.log文件新的文件  

命令:

find . -newer log2012.log -print

輸出:

[root@localhost test]# find -newer log2012.log

.

./log2015.log

./log2017.log

./log2016.log

./test3

[root@localhost test]#

9.使用type選項:

實例1:在/etc目錄下查找所有的目錄  

命令:

find /etc -type d -print  

實例2:在當前目錄下查找除目錄以外的所有類型的文件  

命令:

find . ! -type d -print  

實例3:在/etc目錄下查找所有的符號鏈接文件

命令:

find /etc -type l -print

10.使用size選項:

可以按照文件長度來查找文件,這裏所指的文件長度既可以用塊(block)來計量,也可以用字節來計量。以字節計量文件長度的表達形式爲N c;以塊計量文件長度只用數字表示即可。  

在按照文件長度查找文件時,一般使用這種以字節表示的文件長度,在查看文件系統的大小,因爲這時使用塊來計量更容易轉換。  

實例1:在當前目錄下查找文件長度大於1 M字節的文件  

命令:

find . -size +1000000c -print

實例2:在/home/apache目錄下查找文件長度恰好爲100字節的文件:  

命令:

find /home/apache -size 100c -print  

實例3:在當前目錄下查找長度超過10塊的文件(一塊等於512字節) 

命令:

find . -size +10 -print

11.使用depth選項:

在使用find命令時,可能希望先匹配所有的文件,再在子目錄中查找。使用depth選項就可以使find命令這樣做。這樣做的一個原因就是,當在使用find命令向磁帶上備份文件系統時,希望首先備份所有的文件,其次再備份子目錄中的文件。  

實例1:find命令從文件系統的根目錄開始,查找一個名爲CON.FILE的文件。   

命令:

find / -name "CON.FILE" -depth -print

說明:

它將首先匹配所有的文件然後再進入子目錄中查找

12.使用mount選項: 

  在當前的文件系統中查找文件(不進入其他文件系統),可以使用find命令的mount選項。

實例1:從當前目錄開始查找位於本文件系統中文件名以XC結尾的文件  

命令:find . -name "*.XC" -mount -print 



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