Linux find用法意思

find 命令是 Linux 系統中用於搜尋檔案和目錄的命令。它的基本用法如下:

find [搜尋起點] [搜尋選項] [動作]

以下是一些常用的 find 命令示例:

  1. 查找當前目錄及其子目錄下所有名為 file.txt 的檔案:

    find . -name "file.txt"
  2. 查找當前目錄及其子目錄下所有大小超過 100KB 的檔案:

    find . -type f -size +100k
  3. 查找當前目錄及其子目錄下所有一周內被修改過的檔案:

    find . -type f -mtime -7
  4. 查找當前目錄及其子目錄下所有以 test 開頭的檔案,並列印出它們的絕對路徑:

    find . -name "test*" -print
  5. 查找當前目錄及其子目錄下所有屬於 root 用戶的檔案:

    find . -user root
  6. 查找當前目錄及其子目錄下所有執行檔:

    find . -type f -perm +x
  7. 查找當前目錄及其子目錄下所有 .txt 檔案,並遞歸地刪除它們:

    find . -name "*.txt" -exec rm -f {} \;

在使用 find 命令時,需要注意許可權問題,特別是執行刪除操作時要格外小心,以免誤刪重要檔案。此外,find 命令的搜尋速度可能會受到系統資源的影響,尤其是在搜尋大量檔案時。