Linux運維基礎之fd命令詳解

Linux運維基礎之fd命令詳解

fd 命令是 “find directory(查詢目錄)”的簡稱,用於搜尋和定位檔案系統中的檔案和目錄。它以簡單快捷著稱,因此深受系統管理員和開發人員的喜愛。

與傳統的 find 命令相比,fd 的語法更友好,搜尋速度更快。它常用於查詢特定檔案、清理目錄,甚至在自動化指令碼中使用。此外,fd 命令還可以與 grep 等其他命令搭配使用,以實現更高階的搜尋功能。

如何安裝 fd 命令

大多數 Linux 發行版通常不預設包含 fd,因此需要安裝。安裝過程會根據你使用的發行版而有所不同。以下是在一些常見發行版上安裝和解除安裝 fd 的方法:

Ubuntu/Debian

要在 Ubuntu 或 Debian 上安裝 fd,可以使用以下命令:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get update
sudo apt-get install fd-find
sudo apt-get update sudo apt-get install fd-find
sudo apt-get update
sudo apt-get install fd-find

以後要解除安裝它,可以使用:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get remove fd-find
sudo apt-get remove fd-find
sudo apt-get remove fd-find

Fedora

在 Fedora 上,您可以使用以下命令安裝 fd

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo dnf install fd-find
sudo dnf install fd-find
sudo dnf install fd-find

解除安裝:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo dnf remove fd-find
sudo dnf remove fd-find
sudo dnf remove fd-find

macOS (使用 Homebrew)

如果你使用的是 macOS 和 Homebrew,可以用以下命令安裝 fd

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
brew install fd
brew install fd
brew install fd

解除安裝:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
brew uninstall fd
brew uninstall fd
brew uninstall fd

注:在某些軟體包管理器中,軟體包名稱可能是 fd-find,你可能需要使用 fdfind 命令來代替 fd 。你可以在 shell 配置檔案(如 .bashrc.zshrc )中新增別名 fd=fdfind,將其別名為 fd

如何使用 fd

1. 按名稱搜尋檔案

語法fd PATTERN

說明:搜尋符合給定規則名稱的檔案和目錄。

示例: fd 'report.txt'

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
/home/user/documents/report.txt
/home/user/archive/report.txt
/home/user/scripts/executable_script.sh /home/user/documents/report.txt /home/user/archive/report.txt
/home/user/scripts/executable_script.sh
/home/user/documents/report.txt
/home/user/archive/report.txt

該命令會搜尋當前目錄及其子目錄中出現的所有 report.txt,並列出檔案的完整路徑。

2. 搜尋具有特定副檔名的檔案

語法fd '.*EXTENSION'

說明:搜尋具有特定副檔名的檔案。

示例: fd '.*\.pdf'

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
/home/user/documents/file1.pdf
/home/user/documents/file2.pdf
/home/user/scripts/executable_script.sh /home/user/documents/file1.pdf /home/user/documents/file2.pdf
/home/user/scripts/executable_script.sh
/home/user/documents/file1.pdf
/home/user/documents/file2.pdf

該命令可搜尋當前目錄及其子目錄中的所有 PDF 檔案。

3. 搜尋最近 N 天內修改的檔案

語法fd --changed-within 'Nd'

說明:搜尋最近 N 天內修改過的檔案和目錄。

示例: fd --changed-within '7d'

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
/home/user/documents/week_report.docx
/home/user/photos/recent_image.jpg
/home/user/scripts/executable_script.sh /home/user/documents/week_report.docx /home/user/photos/recent_image.jpg
/home/user/scripts/executable_script.sh
/home/user/documents/week_report.docx
/home/user/photos/recent_image.jpg

該命令會列出最近 7 天內修改過的所有檔案和目錄。

4. 僅搜尋目錄

語法fd --type d PATTERN

說明:搜尋符合給定規則名稱的目錄。

示例: fd --type d 'projects'

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
/home/user/development/projects
/home/user/design/projects
/home/user/scripts/executable_script.sh /home/user/development/projects /home/user/design/projects
/home/user/scripts/executable_script.sh
/home/user/development/projects
/home/user/design/projects

該命令會搜尋當前目錄及其子目錄中所有名為 projects 的目錄。

5. 搜尋排除某些目錄的檔案

語法fd --exclude DIR PATTERN

說明:搜尋與規則匹配的檔案和目錄,不包括指定的目錄。

示例: fd --exclude 'archive' 'report.txt'

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
/home/user/documents/report.txt
/home/user/scripts/executable_script.sh /home/user/documents/report.txt
/home/user/scripts/executable_script.sh
/home/user/documents/report.txt

該命令搜尋 report.txt ,但不包括 “archive” 目錄中的任何結果。

6. 搜尋大於特定大小的檔案

語法fd --size '+SIZE'

說明:搜尋大於指定大小的檔案。

示例: fd --size '+1M'

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
/home/user/videos/large_video.mp4
/home/user/music/big_album.flac
/home/user/scripts/executable_script.sh /home/user/videos/large_video.mp4 /home/user/music/big_album.flac
/home/user/scripts/executable_script.sh
/home/user/videos/large_video.mp4
/home/user/music/big_album.flac

該命令列出當前目錄及其子目錄中所有大於 1 兆位元組的檔案。

7. 搜尋檔案並執行命令

語法fd PATTERN -x COMMAND

說明:搜尋與規則匹配的檔案,並對其執行指定命令。

示例: fd '.*\.txt' -x cat

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
Contents of file1.txt
Contents of file2.txt
/home/user/scripts/executable_script.sh Contents of file1.txt Contents of file2.txt
/home/user/scripts/executable_script.sh
Contents of file1.txt
Contents of file2.txt

該命令搜尋所有文字檔案,然後執行 cat 命令,顯示檔案內容。

8. 以區分大小寫的方式搜尋檔案

語法fd --ignore-case PATTERN

說明:搜尋與規則匹配的檔案和目錄,忽略大小寫。

示例: fd --ignore-case 'readme'

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
/home/user/README
/home/user/projects/readme.md
/home/user/scripts/executable_script.sh /home/user/README /home/user/projects/readme.md
/home/user/scripts/executable_script.sh
/home/user/README
/home/user/projects/readme.md

該命令搜尋當前目錄及其子目錄中所有出現的 readme,忽略大小寫。

9. 使用正規表示式搜尋檔案

語法fd --regex 'REGEX'

說明:搜尋與給定正規表示式匹配的檔案和目錄。

示例: fd --regex '^a.*\.txt$'

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
/home/user/documents/a_file.txt
/home/user/documents/another_file.txt
/home/user/scripts/executable_script.sh /home/user/documents/a_file.txt /home/user/documents/another_file.txt
/home/user/scripts/executable_script.sh
/home/user/documents/a_file.txt
/home/user/documents/another_file.txt

該命令搜尋當前目錄及其子目錄中所有以字母 a 開頭的文字檔案。

10. 搜尋具有特定許可權的檔案

語法fd --perm PERMISSIONS

說明:搜尋具有特定許可權的檔案。

示例: fd --perm 755

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/home/user/scripts/executable_script.sh
/home/user/scripts/executable_script.sh
/home/user/scripts/executable_script.sh

該命令搜尋許可權設定為 755 的檔案(所有者的許可權為讀取、寫入和執行;組和其他人的許可權為讀取和執行)。

更多 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。

評論留言