- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
fd An Alternative to the Linux find Command
The fd command is a popular alternative to the find command in Linux. It is a faster and more user-friendly version of find, and is written in Rust for performance. Some of the key features of fd include the ability to search using regular expressions, a more natural syntax for specifying search parameters, and the ability to search using a specific file extension or name.
Installation
The fd command can be installed on Linux and macOS using the package manager of your distribution.
On Debian based distributions −
sudo apt-get install fd-find
On Fedora and Centos −
sudo yum install fd-find
On Arch Linux and Manjaro −
sudo pacman -S fd
On MacOS −
brew install fd
Alternatively, you can also install fd from source by downloading the latest release from the GitHub repository, and then building and installing it manually.
cargo install --force fd-find
Once installed, you can use the fd command in the terminal just like you would use the find command.
Basic Usage
The basic usage of the fd command is similar to the find command. Here are a few examples −
Finding all files in the current directory −
fd .
Finding all files with a specific name −
fd testfile
Finding all files with a specific extension −
fd -e txt
Finding all files that contain a specific string −
fd -s "test file"
Finding all files modified within the last 24 hours −
fd -t f -mtime -1
Finding all files that are directories −
fd -t d
Finding all files that are symbolic links −
fd -t l
The fd command also has several other options and parameters that you can use to customize your search. You can see a full list of options by running fd --help.
Specifying Search Criteria
The fd command allows you to specify various search criteria to narrow down your search results. Here are a few examples −
Searching for files that match a specific pattern −
fd -e txt -p "*.txt"
This will look for all text files that match the pattern "*.txt"
Searching for files that are larger than a certain size −
fd -L 10M
This will look for all files that are larger than 10MB
Searching for files that are smaller than a certain size −
fd -S 1K
This will look for all files that are smaller than 1KB
Searching for files that are modified within a certain time range −
fd -t f -mtime +30 -mtime -60
This will look for all files that have been modified between 30 and 60 days ago
Searching for files that have a specific owner or group −
fd -u root
This will look for all files that are owned by the user "root"
Searching for files that have a specific permission −
fd -x +rwx
This will look for all files that have read, write and execute permissions for all the users.
Searching using regular expressions −
fd -e txt -r "^[a-z]"
This will look for all text files that have names starting with a lowercase letter.
Searching using a specific path −
fd -p "/path/to/directory"
This will look for all files in the directory '/path/to/directory' and its subdirectories.
Traverse Symbolic Links
By default, the fd command does not follow symbolic links when searching for files. This means that if a symbolic link points to a directory, fd will not search the contents of that directory. However, you can use the -L option to tell fd to follow symbolic links when searching for files.
fd -L -t f
This will look for all files, including those in the directories linked by symbolic links.
You can also use the -H option to tell fd to traverse symbolic links only when they are command line arguments.
fd -H -t f
This will look for all files, including those in the directories linked by symbolic links, when the symbolic link is passed as a command line argument.
You can also use the --no-ignore option to look for files in the directories that are ignored by default, such as .git or node_modules folders.
fd --no-ignore -t f
You can also use --hidden option to look for hidden files in the directories.
fd --hidden -t f
You can combine options as well to get specific results.
Control Directory Traversal Depth
The fd command allows you to control the depth of directory traversal when searching for files. By default, fd will search the entire directory tree recursively, but you can use the -d option to specify a maximum depth for the search.
fd -d 2 -t f
This will look for all files only in the current directory and the immediate subdirectories.
You can also use the -D option to specify the minimum depth to search.
fd -D 2 -t f
This will look for all files only in the directories that are two levels deep or more.
You can also combine -d and -D together to get more specific results.
fd -d 2 -D 3 -t f
This will look for all files only in the directories that are between two and three levels deep.
You can also use the -maxdepth option instead of -d and -mindepth instead of -D for the same results.
fd -maxdepth 2 -mindepth 3 -t f
Invert Search Result
The fd command allows you to invert the search result, meaning you can find the files that do not match the specified criteria. You can use the -I option to invert the search result.
For example −
fd -I -e txt
This command will return all files that are not text files.
Another example −
fd -I -L 10M
This command will return all files that are smaller than 10MB.
You can also use the -not option to invert the search result.
fd -not -e txt
This command will return all files that are not text files.
Execute Commands on Search Results
The fd command allows you to execute commands on the search results. You can use the -e option to specify the command that should be executed on the search results.
For example −
fd -e cat
This command will display the contents of all the files that match the search criteria.
Another example −
fd -e rm
This command will delete all the files that match the search criteria.
You can also use variables in the command to represent the search results. The variable {} will be replaced by the search results.
fd -e "cat {}"
This command will display the contents of all the files that match the search criteria.
Another example −
fd -e "mv {} /path/to/destination"
This command will move all the files that match the search criteria to the directory /path/to/destination
Conclusion
The fd command is a powerful alternative to the find command in Linux, offering a faster and more user-friendly way of searching for files. fd allows you to specify various search criteria, such as file name, extension, size, and modification time, as well as allowing you to search using regular expressions, traverse symbolic links, control directory traversal depth, invert search results and execute commands on search results.