歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> 簡述Linux文件搜索

簡述Linux文件搜索

日期:2017/2/27 14:25:44   编辑:更多Linux
  1 、關於搜索;

  在Linux文件系統中,搜索概念有兩種,一種是搜索文件名,另一種是在一個文件中搜索指定的內容;這兩種搜索,我們都簡要的介紹一下,但不一定極為專業。想到哪寫到哪;

  2、通過搜索文件名來查找文件;

  有的弟兄經常會說文件都放在哪裡了,我怎麼查找文件?其實這裡說的意思就是通過文件名來查找文件所處的位置;搜索文件名,我們常用的工具有find、locate、where等。現在我們簡單的說一說這幾個工具在文件系統中,通過查找文件名的方法來查看其所在的位置;

  2.1 find ;

  find 路徑 -name 文件名

  舉例:比如在/etc中搜索vsFTPd.conf文件;

  [root@localhost ~]# find /etc -name vsftpd.conf  find 更為詳細的說明請參考: 《Linux文件查找命令find,xargs詳述》

  find 是最強勁的搜索工具,用法也比較靈活,而不是僅僅找一個文件名這個功能。

  2.2 locate

  locate 是用來通過文件名搜索文件的工具,它的用法也比較簡單。locate 的應用,首先要通過updatedb建立索引數據庫,然後才能應用;如果您新安裝了軟件或者存放了新的文件,也要先運行updatedb命令,以生成最新索引庫。

  用法:

  locate 文件名

  我們要先運行updatedb;

  [root@localhost ~]# updatedb

  舉例: 我們要找vsftpd.conf文件都位於哪個位置;

[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

  2.3 whereis 和which

  2.3.1 where

  whereis 是來尋找命令的二進制文件,同時也會找到其幫助文件;

  比如我們不知道fdisk工具放在哪裡,我們就可以用whereis fdisk 來查找;

[root@localhost ~]# whereis fdiskfdisk: /sbin/fdisk /usr/share/man/man8/fdisk.8.gz

  如果我們尋找grub在哪裡,應該用如下命令,說明grub位於/sbin目錄,他的幫助文件是grub.8.gz ,也就是man grub;

[root@localhost ~]# whereis grubgrub: /sbin/grub /etc/grub.conf /usr/share/grub /usr/share/man/man8/grub.8.gz

  2.3.2 which

  which 和where 相似,只是我們所設置的環境變量中設置好的路徑中尋找;比如;

[root@localhost ~]# which fdisk/sbin/fdisk

  3、在一個文件或輸出中查找;

  有時我們為了管理服務器,可能要查看一些日志文件或管理指令的輸出,並抽取出來;這時我們要用到more和grep、egrep、(管道),如果要輸出到一個文件中, 還要用到 > 。

  比如我們查看/var/log/message 文件,並查找5月8號的日志;我們應該用如下的命令組合;

[root@localhost ~]# cat /var/log/messages grep 'May 8' more




  注意:因為May和8之間有兩個空格,所以得用''號括起來。

  如果我們想把查看到的結果輸出到一個文件中,應該用 > 輸出到文件;

[root@localhost ~]# cat /var/log/messages grep 'May 8' > ~/message0508.txt[root@localhost ~]# more ~/message0508.txt

  上面的例子,是我們首先用 cat 來查看/var/log/message 的內容,然後抽取帶有 May 8字樣的行,然後輸出到用戶家目錄下的 message0508.txt文件中,然後再用more來查看message0508.txt文件內容;

  從一個輸出結果中查找

  我們也可以從一個輸出的結果中查找所需要的內容,請看下面的例子;

[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 dooddrwxr-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.txtdrwxr-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.bz2drwxr-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.imgdrwxrw-rw- 2 root root 4.0K 04-25 14:22 sundirdrwxr-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

  在ls -lh 的輸出結果中,查看帶有04-27字樣的行,我們看到帶有04-27字樣的行都出來了。04-27在這裡表示文件創建或最後修改(訪問)的時間。符合這一特征的都在裡面。

[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

  查找正在運行的程序:

  這也是從一個輸出中查找的例子,先用ps 來列出所有正在運行中的進程,然後通過grep 來提取。下面的例子中查找是否有gaim程序在運行;

[root@localhost ~]# ps -aux grep gaimWarning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.6/FAQbeinan 2682 0.0 4.0 152644 30188 ? S 08:59 0:16 gaimroot 5660 0.0 0.0 5160 720 pts/1 S+ 13:58 0:00 grep gaim

  從上面的例子,我們可以看到的確有gaim運行,進程號是2682 。如果想殺掉gaim怎麼辦?應該有kill 2682 或killall gaim

[root@localhost ~]# kill 2682或[root@localhost ~]# killall gaim

  對於進程的查找,也可以用pgrep 來進行;比如我們查找gaim;

[root@localhost ~]# pgrep gaim2682

  等價於;

[root@localhost ~]# ps -aux grep gaimWarning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.6/FAQbeinan 2682 4.4 2.3 105000 17504 ? S 14:05 0:02 gaimroot 5716 0.0 0.0 5156 712 pts/1 R+ 14:06 0:00 grep gaim



  4、關於本文;

  關於查找的命令和工具說起來比較復雜,本文也僅僅是一個入門性的文檔。如果只是通過文件名來查找到他位置,我感覺還是用locate好一點。有時find太費時間 ;

  這篇文章本來僅僅是寫文件和目錄的搜索的,後來我想到:可能有的弟兄要學一點指定關健字在一個文件或輸出中查找。所以又寫了一點在一個文件或輸出中查找所需要的內容。看上去本文有點拼湊的感覺,是不是太隨意了?

  5、參考文檔;

  man 和help

  6、相關文檔;

  《Linux 文件內容查看工具介紹》  《關於Linux 文件系統中路徑的理解》  《Linux 文件和目錄管理之列出、刪除、復制、移動及改名》  《Linux 文件類型 及文件的擴展名》  《簡述Linux 文件系統的目錄結構》  《Linux 用戶(user)和用戶組(group)管理概述》  《Linux 文件和目錄的屬性》





Copyright © Linux教程網 All Rights Reserved