How to Use the ‘fd’ Command in Linux
The fd command, short for “find directory,” is designed to search and locate files and directories within the filesystem. It’s known for its simplicity and speed, making it a favorite among system administrators and developers alike.
Compared to the traditional find command, fd offers a more user-friendly syntax and faster search capabilities. It’s commonly used for locating specific files, cleaning up directories, or even in automation scripts. Additionally, the fd command can be paired with other commands like grep for more advanced search functionality.
How to Install the fd Command
fd is not typically included by default in most Linux distributions, so you will need to install it. The installation process can vary depending on the distribution you are using. Here’s how you can install and uninstall fd on some common distributions:
Ubuntu/Debian
To install fd on Ubuntu or Debian, you can use the following command:
sudo apt-get update sudo apt-get install fd-find
To uninstall it later, you can use:
sudo apt-get remove fd-find
Fedora
On Fedora, you can install fd with:
sudo dnf install fd-find
To uninstall it:
sudo dnf remove fd-find
macOS (using Homebrew)
If you’re on macOS and using Homebrew, you can install fd with:
brew install fd
To uninstall it:
brew uninstall fd
Note: the package name might be fd-find in some package managers, and you may need to use the command fdfind instead of fd. You can alias it to fd by adding alias fd=fdfind to your shell’s configuration file (e.g., .bashrc or .zshrc).
How to Use fd
1. Search for Files by Name
Syntax: fd PATTERN
Explanation: Searches for files and directories with a name matching the given pattern.
Example: fd 'report.txt'
Output:
/home/user/documents/report.txt /home/user/archive/report.txt
The command searches for all occurrences of report.txt in the current directory and its subdirectories, listing the full paths to the files.
2. Search for Files with a Specific Extension
Syntax: fd '.*EXTENSION'
Explanation: Searches for files with a specific extension.
Example: fd '.*\.pdf'
Output:
/home/user/documents/file1.pdf /home/user/documents/file2.pdf
The command searches for all PDF files in the current directory and its subdirectories.
3. Search for Files Modified in the Last N Days
Syntax: fd --changed-within 'Nd'
Explanation: Searches for files and directories modified within the last N days.
Example: fd --changed-within '7d'
Output:
/home/user/documents/week_report.docx /home/user/photos/recent_image.jpg
The command lists all files and directories modified within the last 7 days.
4. Search for Directories Only
Syntax: fd --type d PATTERN
Explanation: Searches for directories with a name matching the given pattern.
Example: fd --type d 'projects'
Output:
/home/user/development/projects /home/user/design/projects
The command searches for all directories named projects in the current directory and its subdirectories.
5. Search for Files Excluding Certain Directories
Syntax: fd --exclude DIR PATTERN
Explanation: Searches for files and directories matching the pattern, excluding specified directories.
Example: fd --exclude 'archive' 'report.txt'
Output:
/home/user/documents/report.txt
The command searches for report.txt but excludes any results from the “archive” directory.
6. Search for Files Larger than a Specific Size
Syntax: fd --size '+SIZE'
Explanation: Searches for files larger than a specified size.
Example: fd --size '+1M'
Output:
/home/user/videos/large_video.mp4 /home/user/music/big_album.flac
The command lists all files larger than 1 megabyte in the current directory and its subdirectories.
7. Search for Files and Execute a Command on Them
Syntax: fd PATTERN -x COMMAND
Explanation: Searches for files matching the pattern and executes a specified command on them.
Example: fd '.*\.txt' -x cat
Output:
Contents of file1.txt Contents of file2.txt
The command searches for all text files and then runs the cat command on them, displaying their contents.
8. Search for Files in a Case-Insensitive Manner
Syntax: fd --ignore-case PATTERN
Explanation: Searches for files and directories matching the pattern, ignoring case.
Example: fd --ignore-case 'readme'
Output:
/home/user/README /home/user/projects/readme.md
The command searches for all occurrences of readme in the current directory and its subdirectories, ignoring the case.
9. Search for Files Using a Regular Expression
Syntax: fd --regex 'REGEX'
Explanation: Searches for files and directories matching a given regular expression.
Example: fd --regex '^a.*\.txt$'
Output:
/home/user/documents/a_file.txt /home/user/documents/another_file.txt
The command searches for all text files in the current directory and its subdirectories that start with the letter a.
10. Search for Files with Specific Permissions
Syntax: fd --perm PERMISSIONS
Explanation: Searches for files with specific permissions.
Example: fd --perm 755
Output:
/home/user/scripts/executable_script.sh
The command searches for files with permissions set to 755 (read, write, and execute for the owner; read and execute for the group and others).
More Linux commands:
| Directory Operations | rmdir · cd · pwd · exa · ls |
| File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
| File System Operations | chown · mkfs · locate |
| Networking | ping · curl · wget · iptables · mtr |
| Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
| System Information and Management | env · history · top · who · htop · glances · lsof |
| User and Session Management | screen · su · sudo · open |