天天看點

linux下搜尋指令之type,whatis,whereis,which,locate,find

linux下搜尋指令之type,whatis,whereis,which,locate,find

                                                       ---搜尋

第一個:type--查詢一個指令的類型

   -查詢一個指令為内部或者外部指令的指令;

   -linux的衆多指令中,有内部指令和外部指令,這時可以用type指令來查詢一個指令到底是屬于内部指令還是屬于外部指令;

   -内部指令和外部指令的區分方法:在系統中有存儲位置的為外部指令,沒有存儲位置的為内部指令,因為内部指令和shell是一塊的,是以查不到位置;

   -其實type指令主要是輔助來檢視一個指令的幫助文檔的,如果用help擷取幫助,那麼内部指令的擷取方式為“help 指令”,如果為外部指令則為“指令 --help”。

   [root@localhost ~]# type mkdir       //mkdir的存儲位置在/bin/目錄下,那麼mkdir就是外部指令

   mkdir is /bin/mkdir

   [root@localhost ~]# type passwd       //passwd也是外部指令

   passwd is /usr/bin/passwd        

   [root@localhost ~]# type cd      //cd和help兩個指令則為内部指令,因為他們沒有存儲位置,和shell一塊

   cd is a shell builtin

   [root@localhost ~]# type help

   help is a shell builtin

   [root@localhost ~]# type mkdir passwd        //後面可以跟多個選項同時查詢指令的位置

   passwd is /usr/bin/passwd

第二個:whatis--查詢指令的man入口

    個人認為whatis指令和type指令一樣,也是一個輔助的指令,輔助man這個幫助指令的;

    後面的選項為一個指令,查詢該指令用man指令查詢時間的入口。

   [root@localhost ~]# whatis passwd  //可以看到passwd這個指令man入口有1,和5,那麼可以用“man 5//passwd”或者“man 1 passwd”來擷取相應的passwd指令的幫助文檔;

   passwd               (1)  - update user's authentication tokens

   passwd               (5)  - password file

   passwd              (rpm) - The passwd utility for setting/changing passwords using PAM

   passwd [sslpasswd]   (1ssl)  - compute password hashes

   [root@localhost ~]# whatis mkdir

   mkdir                (1)  - make directories

   mkdir                (1p)  - make directories

   mkdir                (2)  - create a directory

   mkdir                (3p)  - make a directory

第三個:whereis--查找程式/手冊/源檔案

whereis也是輔助指令,查詢linux系統中指令的位置以及被查詢指令幫助文檔的位置;

   [root@localhost zhangsp]# whereis pwd mkdir passwd ls    //檢視pwd,mkdir,passwd,ls這四個外                                               //部指令在linux系統中存在的位置,以及該指令幫助文檔的位置;

pwd: /bin/pwd /usr/share/man/man1p/pwd.1p.gz /usr/share/man/man1/pwd.1.gz

mkdir: /bin/mkdir /usr/share/man/man1p/mkdir.1p.gz /usr/share/man/man1/mkdir.1.gz        /usr/share/man/man3p/mkdir.3p.gz /usr/share/man/man2/mkdir.2.gz

passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz      /usr/share/man/man1/passwd.1.gz

ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz

第四個:which--查找可執行檔案(外部指令、腳本)在linux系統中的存儲位置

   which指令搜尋的位置為$PATH;

   root使用者和普通使用者的$PATH可以用“echo $PATH”指令檢視:

<a href="http://s3.51cto.com/wyfs02/M00/12/2A/wKioL1L7bY7hRxT1AADfPoaLa8o933.jpg" target="_blank"></a>

  例:下面可以看到,因為cd為内部指令,是以不能在$PATH所存在的路徑中找到,其他的都有位置

[root@localhost zhangsp]# which cd ls mkdir pwd

alias ls='ls --color=tty'

/bin/ls

/usr/bin/which: no cd in        (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

/bin/mkdir

/bin/pwd

第五個:locate--在資料庫中進行模糊查詢(包括linux中的檔案和文檔)

由于實在索引中搜尋的,是以速度很快,但也有缺點,當不更新索引庫的時候,可能會出現錯誤!

   1)使用updatedb建立/更新locate索引庫(使用指令uadatedb指令來更新索引庫)

   [root@localhost ~]# updatedb

       2)使用locate查找包含指定關鍵字的檔案/目錄

    [root@localhost ~]# locate inittab    //可以查到linux系統中所有包含inittab字元的檔案或者目錄

   /etc/inittab

   /usr/share/man/man5/inittab.5.gz

   /usr/share/terminfo/a/ansi+inittabs

   /usr/share/vim/vim70/syntax/inittab.vim

   [root@localhost ~]# locate mkdir

   /bin/mkdir

   /usr/bin/gnomevfs-mkdir

   /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/auto/POSIX/mkdir.al

   /usr/share/man/man1/mkdir.1.gz

   /usr/share/man/man1p/mkdir.1p.gz

   /usr/share/man/man2/mkdir.2.gz

   /usr/share/man/man2/mkdirat.2.gz

   /usr/share/man/man3p/mkdir.3p.gz

   3)删除/新增加的檔案對象,需要更新索引庫以後才能展現效果

   如:在目前目錄下建立aaa目錄和bb.txt檔案,将bb.txt複制到aaa目錄中,然後使用locate查詢時沒有結果

的,這時更新一下資料庫索引,在此查詢可以查詢到兩個bb.txt檔案存在的位置。

   [root@localhost ~]# ls

   anaconda-ks.cfg  Desktop  install.log  install.log.syslog

   [root@localhost ~]# touch bb.txt

   [root@localhost ~]# cp bb.txt aaa/

   [root@localhost ~]# ls aaa/

   bb.txt

   [root@localhost ~]# locate bb.txt //雖然bb.txt存在,在沒有更新資料庫的時間,查詢bb.txt是沒有結果的

   [root@localhost ~]# updatedb    

   [root@localhost ~]# locate bb.txt    //更新之後再次查詢可以查詢到結果

   /root/bb.txt

   /root/aaa/bb.txt

   [root@localhost ~]#

第六個:find--精确多條件查找檔案或者目錄(一般用于特殊檔案的搜尋)

   1)使用 -type 按類型查找

   這裡的類型指的是普通檔案(f)、目錄(d)、塊裝置檔案(b)、字元裝置檔案(c)等。塊裝置指的是成塊讀        取資料的裝置(如硬碟、記憶體等),而字元裝置指的是按單個字元讀取資料的裝置(如鍵盤、滑鼠等)。

   如:

   --1

   [root@localhost ~]# find /boot/ -type l  //查詢/boot目錄下的符号連結檔案,如果不指定目錄,則預設為目前目錄         /boot/grub/menu.lst

   [root@localhost ~]# ll -h /boot/grub/menu.lst

   lrwxrwxrwx 1 root root 11 02-10 21:03 /boot/grub/menu.lst -&gt; ./grub.conf

   --2

   [root@localhost ~]# find /boot/ -type d    //查詢/boot目錄下有哪些目錄,還有其本身

   /boot/

   /boot/lost+found

   /boot/grub

   [root@localhost ~]# ll -F /boot/

   總計 6333

   -rw-r--r-- 1 root root   67857 2012-11-29 config-2.6.18-348.el5

   drwxr-xr-x 2 root root    1024 02-10 21:03 grub/

   -rw------- 1 root root 2837626 02-10 21:54 initrd-2.6.18-348.el5.img

   drwx------ 2 root root   12288 02-11 04:45 lost+found/

   -rw-r--r-- 1 root root  118626 2012-11-29 symvers-2.6.18-348.el5.gz

   -rw-r--r-- 1 root root 1282424 2012-11-29 System.map-2.6.18-348.el5

   -rw-r--r-- 1 root root 2125660 2012-11-29 vmlinuz-2.6.18-348.el5

2)使用 -name 按名稱查找

[root@localhost ~]# find /boot -name '*grub*'    //查找/boot目錄下名稱包含“grub”的對象

/boot/grub

/boot/grub/grub.conf

3)組合多個條件進行查找

--可使用-a同時比對多個條件(都必須滿足), -a可省略,例如可隻列出/boot目錄下名稱包含“grub”的普通文                        件

[root@localhost ~]# find /boot/ -name '*grub*' -a -type f

[root@localhost ~]# find /boot/ -name '*grub*' -type f

--可使用-o同時比對多個條件(隻需滿足其中任何一個),-o 不可省略,例如查找/boot目錄下名稱以vmlinuz開                        頭和名稱為menu.lst的對象

[root@localhost ~]# find /boot -name "vmlinuz*" -o -name "menu.lst"

/boot/vmlinuz-2.6.18-348.el5

/boot/grub/menu.lst

注:在寫通配符的時候,用單引号和雙引号效果相同

4)使用 -size 按大小查找,可指定容量機關k(小寫)、M、G

--通過“-size +大小”指定是否超過指定的容量。例如在/boot目錄下查找容量超過2MB的檔案

[root@localhost ~]# find /boot -size +2M

/boot/initrd-2.6.18-348.el5.img

[root@localhost ~]# ll -h /boot/initrd-2.6.18-348.el5.img /boot/vmlinuz-2.6.18-348.el5

-rw------- 1 root root 2.8M 02-10 21:54 /boot/initrd-2.6.18-348.el5.img

-rw-r--r-- 1 root root 2.1M 2012-11-29 /boot/vmlinuz-2.6.18-348.el5

--通過“-size -大小”指定是否小于指定的容量,例如在/boot/grub目錄下查找容量小于2KB的普通檔案

[root@localhost ~]# find /boot/grub/ -size -2k -type f

/boot/grub/device.map

/boot/grub/stage1

[root@localhost ~]# ll -h /boot/grub/device.map /boot/grub/grub.conf /boot/grub/stage1

-rw-r--r-- 1 root root  63 02-10 21:03 /boot/grub/device.map

-rw------- 1 root root 598 02-10 21:03 /boot/grub/grub.conf

-rw-r--r-- 1 root root 512 02-10 21:03 /boot/grub/stage1

5)使用 -exec 指定可執行語句對查找結果進行處理

在處理語句中,以 {} 代替查找結果、最後以 \; 表示處理結束(注意與前面的執行語句之間隔一個空格)。

--上述find查找/boot目錄下大于2MB檔案操作,可直接用 -exec接上ls指令來列出詳細屬性

[root@localhost ~]#  find /boot -size +2M -exec ls -lh {} \;

-rw------- 1 root root 2.8M 02-10 21:54 /boot/initrd-2.6.18-348.el5.img

-rw-r--r-- 1 root root 2.1M 2012-11-29 /boot/vmlinuz-2.6.18-348.el5

   --在/boot/grub目錄下查找容量小于2KB的普通檔案也可以直接用-exec街上ls指令列出詳細屬性

[root@localhost ~]# find /boot/grub/ -size -2k -type f -exec ls -lh {} \;

-rw-r--r-- 1 root root 63 02-10 21:03 /boot/grub/device.map

6)使用 -mtime 按是否在指定日期内修改過進行查找

find -name "file?.*" -mtime +30    //在目前目錄下查找30天之前(-mtime +30)修改過的檔案

find -name "file?.*" -mtime -30    //在目前目錄下查找30天之内(-mtime +30)修改過的檔案

find -name "file?.*" -mtime 30    //在目前目錄下查詢第30天(-mtime 30)修改過的檔案

關于+30:為&gt;31;-30為&lt;29;30為=30(30-31)

擴充

7)使用 -user 、-group查找屬于指定使用者、組的文檔

--查詢根目錄下屬于zhangsp使用者的目錄

[root@localhost ~]# find / -user zhangsp -type d    //查詢根目錄下屬于zhangsp使用者的目錄

   /home/zhangsp

   /home/zhangsp/.mozilla

   /home/zhangsp/.mozilla/plugins

   ...

--在/dev/目錄下查找屬于列印組lp的裝置,并用ls列出其詳細屬性

[root@localhost ~]# find /dev/ -group lp -exec ls -lh {} \;

   crw-rw---- 1 root lp 6, 0 02-12 12:36 /dev/lp0

   crw-rw---- 1 root lp 99, 3 02-12 12:35 /dev/parport3

   crw-rw---- 1 root lp 99, 2 02-12 12:35 /dev/parport2

   crw-rw---- 1 root lp 99, 1 02-12 12:35 /dev/parport1

   crw-rw---- 1 root lp 99, 0 02-12 12:35 /dev/parport0

8)使用 -perm 查找指定權限的文檔

   在/etc/init.d/目錄下查找以auto開頭,權限正好是755的文檔

   [root@localhost ~]# find /etc/init.d/ -name 'auto*' -perm 0755

   /etc/init.d/autofs

   [root@localhost ~]# find /etc/init.d/ -name 'auto*' -perm 0755 -exec ls -lh {} \;

   -rwxr-xr-x 1 root root 3.9K 2012-11-12 /etc/init.d/autofs

備注:touch指令--可以修改已經存在的檔案的修改日期,也可以建立一個空檔案并制定其日期  (參數-t)

   在find的第六個沒有進行測試,可以借助于touch來進行測試;

   過程:

   [root@localhost test]# date    //目前日期為2014年2月12日

   2014年 02月 12日 星期三 22:22:57 CST

   [root@localhost test]# touch -t 1308121030 file1.doc      //建立日期為13年8月12日10點30分的檔案file.doc

   [root@localhost test]# touch -t 02101020 file2.doc       //不寫年份标示目前年份

   [root@localhost test]# touch -t 1109291122 file3.doc     //建立file3.doc檔案的日期為11年9月29日11點22分

   [root@localhost test]# ll -h file*    //用ll可以檢視檔案的日期

   -rw-r--r-- 1 root root 0 2013-08-12 file1.doc

   -rw-r--r-- 1 root root 0 02-10 10:20 file2.doc

   -rw-r--r-- 1 root root 0 2011-09-29 file3.doc

   [root@localhost test]# ls

   file1.doc  file2.doc  file3.doc

   [root@localhost test]# find /home/test/ -name 'file*' -mtime +40

   /home/test/file1.doc

   /home/test/file3.doc

   [root@localhost test]# find /home/test/ -name 'file*' -mtime +40 -exec ls -lh {} \;

                               //顯示40之前修改的檔案,可以看到有兩個形式上開起來日期比較老的檔案

   -rw-r--r-- 1 root root 0 2013-08-12 /home/test/file1.doc

   -rw-r--r-- 1 root root 0 2011-09-29 /home/test/file3.doc

   [root@localhost test]# find /home/test/ -name 'file*' -mtime -40 -exec ls -lh {} \;

                                //顯示/home/test/目錄下最近40天修改的檔案

   -rw-r--r-- 1 root root 0 02-10 10:20 /home/test/file2.doc

   總結:

       type,whatis,which,whereis都是針對指令的:

       type,whatis一般用來檢視該指令是否為内外部指令的;

       which搜尋的路徑為該使用者的環境變量,而whereis則在指令相關目錄和指令幫助的相關目錄                搜尋;

       locate和find是針對檔案:

       locate在索引中搜尋,速度快,但是需要更新資料庫;

       find在linux檔案系統中搜尋,慢,但是全;

   。。。學了那麼久,有的東西學了忘,忘了學,之後還會忘,好記性不如爛筆頭,做一次筆記一個小時,寫一篇部落格需要兩個小時,全當加深記憶了!

   以前總是以為一個指令後面隻能跟一個選項,後來自己也用過好多選項,如:type pwd whatis passwd這條指令,明明知道type後面可以跟很多選項,還是習慣type pwd,type whatis。。。分開來用,以後得改了,有利于提高效率!

本文轉自 murongqingqqq  51CTO部落格,原文連結:http://blog.51cto.com/murongqingqqq/1358592