作者:北南南北
摘要:本文簡單介紹了搜尋檔案的方法,一是通過檔案名來檢視檔案所處的位置,二是在一個檔案或輸出中查找指定的内容。
目錄
1 、關于搜尋;
2、通過搜尋檔案名來查找檔案;
2.1 find ;
2.2 locate
2.3 whereis 和which
2.3.1 where
2.3.2 which
3、在一個檔案或輸出中查找;
4、關于本文;
5、參考文檔;
6、相關文檔;
1 、關于搜尋;2、通過搜尋檔案名來查找檔案;
+++++++++++++++++++++++++++++++++++++++
正文
在Linux檔案系統中,搜尋概念有兩種,一種是搜尋檔案名,另一種是在一個檔案中搜尋指定的内容;這兩種搜尋,我們都簡要的介紹一下,但不一定極為專業。想到哪寫到哪;
有的弟兄經常會說檔案都放在哪裡了,我怎麼查找檔案?其實這裡說的意思就是通過檔案名來查找檔案所處的位置;搜尋檔案名,我們常用的工具有find、locate、where等。現在我們簡單的說一說這幾個工具在檔案系統中,通過查找檔案名的方法來檢視其所在的位置;
<code>find 路徑 -name 檔案名</code>
舉例:比如在/etc中搜尋vsftpd.conf檔案;
<code>[root@localhost ~]# find /etc -name vsftpd.conf</code>
find 更為詳細的說明請參考: 《Linux檔案查找指令find,xargs詳述》
find 是最強勁的搜尋工具,用法也比較靈活,而不是僅僅找一個檔案名這個功能。
locate 是用來通過檔案名搜尋檔案的工具,它的用法也比較簡單。locate 的應用,首先要通過updatedb建立索引資料庫,然後才能應用;如果您新安裝了軟體或者存放了新的檔案,也要先運作updatedb指令,以生成最新索引庫。
用法:
<code>locate 檔案名</code>
我們要先運作updatedb;
<code>[root@localhost ~]# updatedb</code>
舉例: 我們要找vsftpd.conf檔案都位于哪個位置;
<code>[root@localhost ~]# locate vsftpd.conf /etc/vsftpd/vsftpd.conf /home/beinan/vsftpd.conf /root/.vsftpd.conf.swp /root/vsftpd.conf /root/vsftpd.config /usr/share/doc/vsftpd-2.0.4/EXAMPLE/INTERNET_SITE/vsftpd.conf /usr/share/doc/vsftpd-2.0.4/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf /usr/share/doc/vsftpd-2.0.4/EXAMPLE/VIRTUAL_USERS/vsftpd.conf /usr/share/logwatch/default.conf/logfiles/vsftpd.conf /usr/share/logwatch/default.conf/services/vsftpd.conf /usr/share/man/man5/vsftpd.conf.5.gz</code>
whereis 是來尋找指令的二進制檔案,同時也會找到其幫助檔案;
比如我們不知道fdisk工具放在哪裡,我們就可以用whereis fdisk 來查找;
<code>[root@localhost ~]# whereis fdisk fdisk: /sbin/fdisk /usr/share/man/man8/fdisk.8.gz</code>
如果我們尋找grub在哪裡,應該用如下指令,說明grub位于/sbin目錄,他的幫助檔案是grub.8.gz ,也就是man grub;
<code>[root@localhost ~]# whereis grub grub: /sbin/grub /etc/grub.conf /usr/share/grub /usr/share/man/man8/grub.8.gz</code>
which 和where 相似,隻是我們所設定的環境變量中設定好的路徑中尋找;比如;
<code>[root@localhost ~]# which fdisk /sbin/fdisk</code>
有時我們為了管理伺服器,可能要檢視一些日志檔案或管理指令的輸出,并抽取出來;這時我們要用到more和grep、egrep、|(管道),如果要輸出到一個檔案中, 還要用到 > 。
比如我們檢視/var/log/message 檔案,并查找5月8号的日志;我們應該用如下的指令組合;
<code>[root@localhost ~]# cat /var/log/messages |grep 'May 8' |more</code>
注意:因為May和8之間有兩個空格,是以得用''号括起來。
如果我們想把檢視到的結果輸出到一個檔案中,應該用 > 輸出到檔案;
<code>[root@localhost ~]# cat /var/log/messages |grep 'May 8' > ~/message0508.txt [root@localhost ~]# more ~/message0508.txt</code>
上面的例子,是我們首先用 cat 來檢視/var/log/message 的内容,然後抽取帶有 May 8字樣的行,然後輸出到使用者家目錄下的 message0508.txt檔案中,然後再用more來檢視message0508.txt檔案内容;
從一個輸出結果中查找
我們也可以從一個輸出的結果中查找所需要的内容,請看下面的例子;
<code>[root@localhost ~]# ls -lh 總計 24M -rwxr-xr-x 1 root root 545 04-25 11:21 adduml02.sh -rwxr-xr-x 1 root root 545 2004-01-18 adduml.sh -rw-rw-rw- 1 root root 0 04-25 14:26 dood drwxr-xr-t 2 root root 4.0K 04-24 21:59 googledir -rwxr-xr-x 1 root root 7 04-21 12:47 lsfile.sh -rw-r--r-- 1 root root 31K 05-08 13:47 message0508.txt drwxr-xr-x 2 root root 4.0K 04-21 12:46 mkuml-2004.07.17 -rwxr-xr-x 1 root root 67K 04-22 14:13 mkuml-2004.07.17-ananas.tar.bz2 drwxr-xr-x 2 root 502 4.0K 04-25 09:08 mydir -rw-r--r-- 1 root root 7.9M 04-27 20:35 myfile.img -rw-r--r-- 1 root root 4.0M 04-27 20:37 myfileSpaa -rw-r--r-- 1 root root 3.9M 04-27 20:37 myfileSpab -rw-r--r-- 1 root root 7.9M 04-27 20:38 newmyfile.img drwxrw-rw- 2 root root 4.0K 04-25 14:22 sundir drwxr-xr-x 2 root root 4.0K 04-25 09:20 testdir -rwxr-xr-x 1 root root 613 03-26 18:41 upgrade.log -rw------- 1 root root 4.0K 04-29 20:12 vsftpd.conf -rw-r--r-- 1 root root 4.0K 04-29 20:23 vsftpd.config -rw-r--r-- 1 root root 100 04-27 16:14 xaa</code>
在ls -lh 的輸出結果中,檢視帶有04-27字樣的行,我們看到帶有04-27字樣的行都出來了。04-27在這裡表示檔案建立或最後修改(通路)的時間。符合這一特征的都在裡面。
<code>[root@localhost ~]# ls -lh |grep '04-27' |more -rw-r--r-- 1 root root 7.9M 04-27 20:35 myfile.img -rw-r--r-- 1 root root 4.0M 04-27 20:37 myfileSpaa -rw-r--r-- 1 root root 3.9M 04-27 20:37 myfileSpab -rw-r--r-- 1 root root 7.9M 04-27 20:38 newmyfile.img -rw-r--r-- 1 root root 100 04-27 16:14 xaa</code>
查找正在運作的程式:
這也是從一個輸出中查找的例子,先用ps 來列出所有正在運作中的程序,然後通過grep 來提取。下面的例子中查找是否有gaim程式在運作;
<code>[root@localhost ~]# ps -aux |grep gaim Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.6/FAQ beinan 2682 0.0 4.0 152644 30188 ? S 08:59 0:16 gaim root 5660 0.0 0.0 5160 720 pts/1 S+ 13:58 0:00 grep gaim</code>
從上面的例子,我們可以看到的确有gaim運作,程序号是2682 。如果想殺掉gaim怎麼辦?應該有kill 2682 或killall gaim
<code>[root@localhost ~]# kill 2682 或 [root@localhost ~]# killall gaim</code>
對于程序的查找,也可以用pgrep 來進行;比如我們查找gaim;
<code>[root@localhost ~]# pgrep gaim 2682</code>
等價于;
<code>[root@localhost ~]# ps -aux |grep gaim Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.6/FAQ beinan 2682 4.4 2.3 105000 17504 ? S 14:05 0:02 gaim root 5716 0.0 0.0 5156 712 pts/1 R+ 14:06 0:00 grep gaim</code>
關于查找的指令和工具說起來比較複雜,本文也僅僅是一個入門性的文檔。如果隻是通過檔案名來查找到他位置,我感覺還是用locate好一點。有時find太費時間 ;
這篇文章本來僅僅是寫檔案和目錄的搜尋的,後來我想到:可能有的弟兄要學一點指定關健字在一個檔案或輸出中查找。是以又寫了一點在一個檔案或輸出中查找所需要的内容。看上去本文有點拼湊的感覺,是不是太随意了?
man 和help