對于新手而言,在 linux 中使用指令行可能會非常不友善。沒有圖形界面,很難在不同檔案夾間浏覽,找到需要的檔案。本篇教程中,我會展示如何在 linux 中查找特定的檔案。
<a target="_blank"></a>
使用 linux find 指令可以用不同的搜尋标準如名字、類型、所屬人、大小等來搜尋目錄樹。基本文法如下:
<code># find path expression search-term</code>
下面是使用 find 指令根據檔案名來查找特定檔案的一個例子:
<code># find -name test.file</code>
指令會搜尋整個目錄樹來查找名為 <code>test.file</code> 的檔案,并且會提供其存放位置。你可以使用你 linux 上一個存在的檔案名來嘗試一下。
find 指令有時會花費幾分鐘來查找整個目錄樹,尤其是如果系統中有很多檔案和目錄的話。要顯著減少時間,你可以指定搜尋的目錄。比如,如果你知道 <code>/var</code> 中存在 <code>test.file</code>,那就沒有必要搜尋其它目錄。這樣,你可以使用下面的指令:
<code># find /var -name test.file</code>
find 還可以根據時間、大小、所屬人、權限等選項搜尋檔案。要了解更多關于這些選項的資訊,你可以使用檢視** linux find 指令**的手冊。
<code># man find</code>
要在linux中使用<code>locate</code>指令,首先需要安裝它。
如果你正在使用 ubuntu,運作下面的指令來安裝 locate:
<code># apt-get update</code>
<code># apt-get install mlocate</code>
如果你使用的是 centos ,運作下面的指令來安裝 locate:
<code># yum install mlocate</code>
locate 是一種比 find 更快的方式,因為它在資料庫中查找檔案。要更新搜尋資料庫,運作下面的指令:
<code># updatedb</code>
使用 locate 查找檔案的文法:
<code># locate test.file</code>
就像 find 指令一樣,locate 也有很多選項來過濾輸出。要了解更多你可以檢視linux locate 指令的手冊。
<code># man locate</code>
原文釋出時間為:2017-12-06
本文來自雲栖社群合作夥伴“linux中國”