Linux學習筆記四:Linux的檔案搜尋命令

來源:互聯網
上載者:User

標籤:des   style   blog   color   ar   os   sp   java   strong   

1、檔案搜尋命令  which

文法:which [命令名稱]

範例:$which ls  列出ls命令所在目錄

[[email protected] ~]$ which lsalias ls=‘ls --color=auto‘          /bin/ls

另外一個命令:whereis [名稱名稱],也可以列出命令所在目錄。

[[email protected] ~]$ whereis lsls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

他們的不同之處在於:Which提供命令的別名資訊,Whereis提供命令的協助資訊。

如上面中的“alias ls =‘ls --color=auto‘”就是ls命令的別名資訊,它表示我們按下ls命令就相當於執行“ls --color=auto”命令。

whereis中輸出部分的檔案ls.1.gz和ls.1p.gz是指ls這個命令的協助檔案。

2、檔案搜尋命令  find

文法:find [搜尋路徑] [選項] [搜尋索引鍵]

[選項]

-name 按檔案名稱尋找(* 匹配任一字元,包括0個字元。? 匹配單個字元)

範例:

find /etc -name init  在/etc目錄下尋找名字為init的檔案find /etc -name init*  在/etc目錄下尋找所有以init開頭的檔案find /etc -name init??? 在/etc目錄下尋找所有以init開頭的,並且init後還有3個字元的文字[[email protected] Desktop]$ lshello.txt  linuxqq-v1.0.2-beta1.i386.rpm  Screenshot.png  test13  test.txt[[email protected] Desktop]$ find . -name test\*    <!-- 這裡的星號要加右斜杠轉移,否則會出錯 -->./test13./test13/testfile./test.txt

-size  按檔案大小尋找(+ 表示大於指定大小,- 表示小於指定大小, = 表示剛好等於指定大小)

單位是block,1個block512Byte,即1KB=2Block,1KB等於兩個資料區塊。

範例:

find / -size +2048在根目錄下尋找大小大於100MB的檔案find / -size -2048在根目錄下尋找大小小於100MB的檔案find / -size +2048在根目錄下尋找大小剛好等於100MB的檔案(一般不用,因為不可能剛好等於100MB)

-user 根據檔案的所有者尋找

範例:

find /home -user chan在/home下尋找所有者為chan的檔案

-根據時間尋找

一共有兩組共6個選項,分別是:

ctime/atime/mtime  以天為單位

cmin/amin/mmin  以分鐘為單位 

c 表示change,指檔案的屬性(所有者、所屬組、許可權等)被修改過

a 表示accesss,表示被訪問過

m表示modify,表示檔案內容被修改過

其中還有   -表示之內,+表示超過

範例:

find . -mmin -120在目前的目錄下尋找120分鐘內內容被修改過的檔案find . -mmin +60在目前的目錄下超過60分鐘之前內容被修改過的檔案(即60分鐘內被修改過的檔案除外)find . -mtime -2 尋找兩天之內內容被修改過的檔案find . -atime -2 尋找兩天之內內容被訪問過的檔案

-type 按檔案類型尋找(f 表示二進位檔案,L 表示軟連結檔案,D 表示目錄)

範例:

find . -type f尋找目錄下的所有二進位檔案

-inum 根據i節點找(i節點是linux中檔案的唯一標識)

範例:

//尋找到hello.txt檔案的i節點是781951[[email protected] Desktop]$ ls -i781951 hello.txt                      782880 Screenshot.png  782291 test.txt782823 linuxqq-v1.0.2-beta1.i386.rpm  782587 test13//根據i節點尋找[[email protected] Desktop]$ find . -inum 781951./hello.txt

一般情況下用來尋找一些檔案名稱為特殊字元的檔案,因為這些特殊檔案名稱對於linux來說無法識別(比如“a b” 空間有空格)

3、find串連符

當我們既想以檔案名稱又想用檔案大小作為查詢條件時,就需要用到find串連符了。

-a  邏輯與串連符,-o 邏輯或串連符,-exec 串連執行符, -ok 串連執行符(-ok與-exec的區別就是,ok在尋找到目標之後會詢問你是否進行之前命令中設定的操作,而-exec則不會進行詢問)

範例:

--【-exec串連符】找到hello.txt並直接顯示它[[email protected] Desktop]$ find . -name hello.txt -exec cat {} \;第一行第二行第三行倒數第2行Hello, I‘m in centos system to write a introduce file to you.--找到hello.txt後,詢問使用者是否進行對應操作,輸入Y/N[[email protected] Desktop]$ find . -name hello.txt -ok cat {} \;< cat ... ./hello.txt > ? y第一行第二行第三行倒數第2行Hello, I‘m in centos system to write a introduce file to you.

註:命令列裡的“{}”表示尋找到的對象,是起到一個指代的作用,後面的“\;”是固定的。

3、檔案搜尋指令  locate

文法:locate [搜尋索引鍵]

範例:

[[email protected] Desktop]$ locate hello.txt/home/chanshuyi/Desktop/hello.txt

Locate命令與find命令不同,locate尋找是根據系統的檔案目錄資料庫進行尋找的,而find是直接去硬碟尋找的。

因此locate對於剛剛建立的檔案或目錄會找不到。(因為系統檔案目錄資料庫還未更新)。

你也可以用updatedb去手動更新系統的檔案目錄資料庫。但locate命令一般只有在linux系統中存在,在其他類型的Unix系統中是不存在的。

注意:updatedb指令只有root使用者才有許可權操作。你可以用su命令切換到root使用者下。

4、在檔案中搜尋字串匹配的行並輸出  grep

文法:grep [指定字串] [源檔案]

範例:

grep ftp /etc/services在services檔案中搜尋ftp欄位,並輸出所在的行

 

下一篇:Linux學習筆記四:Linux的協助命令

Linux學習筆記四:Linux的檔案搜尋命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.