天天看點

Linux基礎指令——其他常用指令(find locate chmod chown)

作者:雲計算練習生

1 find指令

find指令用于在檔案系統中搜尋檔案或目錄。它具有強大的搜尋功能,可以根據檔案名、大小、修改時間等條件進行搜尋。基本文法如下:

find [path] [expression]

Find指令的用法非常多,這裡先簡單舉幾個例子:

Linux基礎指令——其他常用指令(find locate chmod chown)

- path:搜尋的起始目錄。

- expression:搜尋條件。

示例:

1.1 - 在/etc及其子目錄中,查找host開頭的檔案

Linux基礎指令——其他常用指令(find locate chmod chown)

1.2 - 在目前目錄及子目錄中,查找大寫字母開頭的txt檔案

切換到/usr目錄

Linux基礎指令——其他常用指令(find locate chmod chown)

1.3 - 在/var/log目錄中搜尋最近7天内修改過的檔案

find /var/log -mtime -7

Linux基礎指令——其他常用指令(find locate chmod chown)

1.4 - 找到目前目錄下所有檔案,并列印出詳細資訊儲存到filelist檔案中

目前目錄/usr

Linux基礎指令——其他常用指令(find locate chmod chown)
Linux基礎指令——其他常用指令(find locate chmod chown)

2. locate指令

locate指令用于快速搜尋檔案系統中的檔案。它使用預先建構的資料庫進行搜尋,是以速度較快。基本文法如下:

Centos7預設沒有這個指令,是以先安裝

Linux基礎指令——其他常用指令(find locate chmod chown)

locate [options] pattern

- options:可選參數。

- pattern:搜尋模式。

示例:

2.1 - 搜尋名為file.txt的檔案:在根目錄查找檔案test.txt

Linux基礎指令——其他常用指令(find locate chmod chown)

使用指令會報錯

Linux基礎指令——其他常用指令(find locate chmod chown)

執行updatedb解決儲存

Linux基礎指令——其他常用指令(find locate chmod chown)

3. chmod指令

chmod指令用于更改檔案或目錄的權限。基本文法如下:

chmod [options] mode file

- options:可選參數。

- mode:權限模式,可以是數字(如755)或符号(如u+x)。

- file:要更改權限的檔案或目錄。

示例:

3.1 - 将檔案file.txt的權限更改為755:

Linux基礎指令——其他常用指令(find locate chmod chown)

chmod 755 file.txt, 這些代表檔案的權限,在之前的教程中還未講解Linux檔案權限,這裡先簡單了解一下,後續會詳細介紹Linux檔案權限。

Linux基礎指令——其他常用指令(find locate chmod chown)

3.2 - 為檔案file.txt的所有者添加執行權限:

chmod u+x file.txt,重新建立一個新檔案做練習,給檔案所屬使用者加執行權限。

Linux基礎指令——其他常用指令(find locate chmod chown)

4. chown指令

chown指令用于更改檔案或目錄的所有者和所屬組。基本文法如下:

chown [options] owner[:group] file

- options:可選參數。

- owner:新的所有者。

- group:新的所屬組(可選)。

- file:要更改所有者和所屬組的檔案或目錄。

示例:

4.1 - 将檔案file.txt的所有者更改為user1:

chown user1 file.txt

先建立一個新使用者test

Linux基礎指令——其他常用指令(find locate chmod chown)
Linux基礎指令——其他常用指令(find locate chmod chown)

4.2 - 将檔案file.txt的所有者更改為user1,所屬組更改為group1:

chown user1:group1 file.txt ,先建立一個新的組

Linux基礎指令——其他常用指令(find locate chmod chown)
Linux基礎指令——其他常用指令(find locate chmod chown)

希望這些資訊對您有所幫助!如果有不對的地方請不吝賜教,歡迎在評論區留言,發表你的看法。

繼續閱讀