find
命令用於根據指定條件(如檔名、大小、修改日期和其他屬性)搜尋和定位檔案和目錄。你可以用它搜尋整個檔案系統或某個特定目錄。
系統管理員和普通使用者常用它來快速查詢檔案,尤其是在有大量檔案的系統中。無論你是在尋找丟失的檔案,還是需要對一組符合特定條件的檔案執行批量操作,find
命令都是管理和組織檔案系統的重要工具。
find
命令的一般語法:
$ find [OPTIONS] [PATH...] [EXPRESSION]
$ find [OPTIONS] [PATH...] [EXPRESSION]
$ find [OPTIONS] [PATH...] [EXPRESSION]
1. 搜尋目錄中的特定檔案
find ./ExampleDir -name example.txt
find ./ExampleDir -name example.txt
find ./ExampleDir -name example.txt
使用 -name
引數時,該命令將嘗試在 ExampleDir 目錄中搜尋 example.txt
;如果找到,將返回檔案的路徑。
./ExampleDir/subdir1/example.txt
./ExampleDir/subdir2/subsubdir/example.txt
./ExampleDir/subdir1/example.txt
./ExampleDir/subdir2/subsubdir/example.txt
./ExampleDir/subdir1/example.txt ./ExampleDir/subdir2/subsubdir/example.txt
2. 查詢並列出副檔名相同的檔案
find ./dirname -name *.txt
find ./dirname -name *.txt
find ./dirname -name *.txt
該命令將搜尋 ExampleDir
目錄中所有副檔名為 .txt
的檔案。如果找到,每個結果都將以新行返回。
./dirname/file1.txt
./dirname/file2.txt
./dirname/subdir/file3.txt
./dirname/subdir/another_subdir/file4.txt
./dirname/file1.txt
./dirname/file2.txt
./dirname/subdir/file3.txt
./dirname/subdir/another_subdir/file4.txt
./dirname/file1.txt ./dirname/file2.txt ./dirname/subdir/file3.txt ./dirname/subdir/another_subdir/file4.txt
3. 查詢並列出空檔案和空子目錄
find ./ExampleDir -empty
find ./ExampleDir -empty
find ./ExampleDir -empty
該命令帶有 -empty
引數,將查詢並列出 ExampleDir
資料夾內的所有空檔案和空子資料夾。
空檔案的定義是檔案大小為 0 位元組,空資料夾的定義是沒有檔案或檔案大小為 0 位元組。
4. 查詢並列出包含特定文字的檔案
find ./ExampleDir -type f -name "*.txt" -exec grep 'Example' {} \;
find ./ExampleDir -type f -name "*.txt" -exec grep 'Example' {} \;
find ./ExampleDir -type f -name "*.txt" -exec grep 'Example' {} \;
該命令在 ExampleDir
目錄下副檔名為 .txt
的檔案中搜尋單詞/字串 “Example“。
./ExampleDir/file1.txt:This is an Example line in file1.
./ExampleDir/subdir/file2.txt:Another Example in a different file.
./ExampleDir/file3.txt:Example usage of the find command.
./ExampleDir/file1.txt:This is an Example line in file1.
./ExampleDir/subdir/file2.txt:Another Example in a different file.
./ExampleDir/file3.txt:Example usage of the find command.
./ExampleDir/file1.txt:This is an Example line in file1. ./ExampleDir/subdir/file2.txt:Another Example in a different file. ./ExampleDir/file3.txt:Example usage of the find command.
5. 查詢並列出特定使用者擁有的檔案和子目錄
find ./ExampleDir -user ubuntu
find ./ExampleDir -user ubuntu
find ./ExampleDir -user ubuntu
該命令帶有 -user
引數,將查詢 ExampleDir
目錄中 Ubuntu
使用者擁有的檔案和子目錄。如果找到,將返回檔名。
下面是 ls -l
的結果示例:
-rw-rw-r-- 1 newone ubuntu 20 Jan 27 06:24 example.txt
-rw-rw-r-- 1 newone ubuntu 20 Jan 27 06:24 example.txt
-rw-rw-r-- 1 newone ubuntu 20 Jan 27 06:24 example.txt
newone
表示組名, ubuntu
表示使用者。
6. 查詢並列出特定組擁有的檔案和子目錄
find ./ExampleDir -group ubuntu
find ./ExampleDir -group ubuntu
find ./ExampleDir -group ubuntu
該命令帶有 -group
引數,將查詢 ExampleDir
目錄中 Ubuntu
組擁有的所有檔案和子目錄。如果找到,將返回檔名。
更多 Linux 命令
下面羅列了最常見的一些 Linux 命令,您可以根據自己的需要查閱對應命令的詳細解析:
目錄操作 | rmdir · cd · pwd · exa · ls |
檔案操作 | cat · cp · dd · less · touch · ln · rename · more · head |
檔案系統操作 | chown · mkfs · locate |
網路 | ping · curl · wget · iptables · mtr |
搜尋和文字處理 | find · grep · sed · whatis · ripgrep · fd · tldr |
系統資訊和管理 | env · history · top · who · htop · glances · lsof |
使用者和會話管理 | screen · su · sudo · open |
此外,我們還整理 Linux 命令列大全,以幫助大家全面深入地學習 Linux。
評論留言