Atlantic.Net Blog

How to Find Files with fd Command in Linux

Finding files and directories in Linux is an essential skill for any system administrator. The fd command is a command-line tool used to find files and directories in Linux. It is a simple and fast alternative to the find command. Compared to the find command, the fd command has more intuitive syntax, colorized output, and faster search speed. It also supports the use of regular expressions and can ignore hidden files and directories by default.

In this post, you will learn how to find files with fd command in Linux.

Install the fd Command

By default, the fd command is included in the default repository of all major operating systems.

For Debian and Ubuntu operating systems, install the fd command using the following command:

apt-get install fd-find -y

For RHEL, Rocky Linux, and Fedora operating systems, install the fd command using the following command:

dnf install fd-find -y

After installing the fd command, you can check the version of the fd command with the following command:

fd --version

You will get the following output:

fd 7.3.0

Also Read

Find a File with Find and Locate Commands in Linux

Basic Syntax of fd Command

The basic syntax of the fd command is shown below:

fd [OPTION] [PATTERN] [PATH]

A brief explanation of each option is shown below:

  • -H – Include hidden files and directories in search results.
  • -I – Show the search results that would be ignored by .gitignore, .ignore, or .fdignore files.
  • -s – Perform a case-sensitive search.
  • -i – Perform a case-insensitive search.
  • -a – Show absolute instead of relative paths.
  • -L – Follow symlinks.
  • -j – Used to define the number of threads used for searching.

The fd Command Basic Usage

Using the fd command without any options will search all files and directories in your current working directory:

fd

You will get the following output:

directory1
directory1/my-File1.txt
directory1/my-file.txt
directory2
directory2/my-File2.txt
directory2/my-file2.txt
directory3
directory3/File4.txt
directory3/file2.png
directory3/man
directory3/women

By default, the fd command will not show the hidden files in the output. You can use the -H option to list all files, including the hidden files.

fd -H

Output:

.allow
.git
directory1
directory1/my-File1.txt
directory1/my-file.txt
directory2
directory2/my-File2.txt
directory2/my-file2.txt
directory3
directory3/File4.txt
directory3/file2.png
directory3/man
directory3/women

Use the -p option to search all files in the specified directory:

fd -p directory1

Output:

directory1
directory1/my-File1.txt
directory1/my-file.txt

Find File with String Match

You can use the -F option to find a file that matches a string containing regular expressions.

fd -F my-file

Output:

directory1/my-File1.txt
directory1/my-file.txt
directory2/my-File2.txt
directory2/my-file2.txt

Find Case Sensitive File

You can use the -s option to enable case sensitivity and show only case-sensitive files in the result.

fd -s file

Output:

directory1/my-file.txt
directory2/my-file2.txt
directory3/file2.png

Find File By Type

You can use the -t option to find a file by its type.

For example, to find all directories and sub-directories in your current working directory, run the following command:

fd -t d

Output:

directory1
directory2
directory3
directory3/man
directory3/women

To find only regular files use the -t option with the f indicator:

fd -t f

Output:

directory1/my-File1.txt
directory1/my-file.txt
directory2/my-File2.txt
directory2/my-file2.txt
directory3/File4.txt
directory3/file2.png

To find only executable files, use the -t option with the x indicator:

fd -t x

To find only empty files and directories, use the -t option with the e indicator:

fd -t e

Also Read

How to Use Rsync to Copy/Sync Files Between Servers

Find File By Extension

You can use the -e option to find a file by their extensions.

To find all .png files, run the following command:

fd -e png

Output:

directory3/file2.png

To find both text and png files, run the following command:

fd -e png -e txt

Output:

directory1/my-File1.txt
directory1/my-file.txt
directory2/my-File2.txt
directory2/my-file2.txt
directory3/File4.txt
directory3/file2.png

Find File By Location

You can use the -p option to find a file from a specified location.

fd file.txt -p directory1

Output:

directory1/my-file.txt

You can also find all files from multiple locations.

For example, to find all regular files from directory1 and directory2, run the following command:

fd . ./directory1 ./directory2 -t f

Output:

directory1/my-File1.txt
directory1/my-file.txt
directory2/my-File2.txt
directory2/my-file2.txt

Find File By Size

You can use the -S option to find the file by their size.

For example, to find all files with a size 1 MB or larger, run the following command:

fd -S +1m

Run the following command if you want to find all files with size 1 MB or smaller:

fd -S -1m

Find File By Data

The fd command will also allow you to find files on the basis of the creation date. You can specify the duration of the week, day, time, or date.

For example, find all files that are changed before “2022-02-02 10:00:00,” run the following command:

fd -t x --changed-before "2022-02-02 10:00:00"

To find all files that are changed within two weeks, run the following command:

fd -t x --changed-within 2week

To find all files that are changed within five days, run the following command:

fd -t x --changed-within 5day

Conclusion

In this post, we explained the fd command with different examples to show you how to find files using your custom parameters. Try it on VPS hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year