Quickly search for files on linux – fd-find

Quickly search for files on linux

Searching for files on linux can take a long time on a big file system. The classic find command has many options for fine grained control of your search. But it’s also slow because it’s sequential. Locate an alternative creates a database of files that is more efficient for search. But it requires two steps – updating the database then searching it. It also doesn’t have many search options.

fdfind or fd is a relatively new tool that supports many of find’s options. It is much faster and works in parallel. You can use regular expressions, glob patterns, file types, file extensions and many more options in your search. As a bonus it’s also simpler to use than find.

fdfind exists in all major linux distributions repositories. To install it on ubuntu run:

sudo apt install fd-find

The simplest search is a regex search without other arguments. Which will search in your current directory.

$ fdfind test
dir1/2c_test 
dir1/2d_test 
dir1/2e_test 
dir1/3a_test 
dir1/3b_test 
dir1/3c_test 
dir1/3d_test 
dir1/3e_test 
my-test-a 
my-test-b 
my-test-c 
my-test-d 
test1 
test10 
test11

You can also specify a root directory to start the search from.

$ fdfind "_test$" somedir 
somedir/dir1/1a_test 
somedir/dir1/1b_test 
somedir/dir1/1c_test 
somedir/dir1/1d_test 
somedir/dir1/1e_test 
somedir/dir1/2a_test 
somedir/dir1/2b_test 
somedir/dir1/2c_test 
somedir/dir1/2d_test 
somedir/dir1/2e_test 
somedir/dir1/3a_test 
somedir/dir1/3b_test 
somedir/dir1/3c_test 
somedir/dir1/3d_test 
somedir/dir1/3e_test

To get an output similar to ls -l with file permissions and file modified date run:

$ fdfind -l test somedir  
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/1a_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/1b_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/1c_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/1d_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/1e_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/2a_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/2b_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/2c_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/2d_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/2e_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/3a_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/3b_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/3c_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/3d_test 
drwxrwxr-x 2 hexcontributer hexcontributer 4.0K Apr  8 10:25 somedir/dir1/3e_test 
-rw-rw-r-- 1 hexcontributer hexcontributer    0 Apr  8 10:24 somedir/my-test-a 
-rw-rw-r-- 1 hexcontributer hexcontributer    0 Apr  8 10:24 somedir/my-test-b 
-rw-rw-r-- 1 hexcontributer hexcontributer    0 Apr  8 10:24 somedir/my-test-c 
-rw-rw-r-- 1 hexcontributer hexcontributer    0 Apr  8 10:24 somedir/my-test-d

To search by file type, you can use the -t option. The values are f for file, d for directory, l for symlink, s for socket, p for pipe, x for executable and e for empty files or directories. You can add multiple -t options to search for a specific file type. For example to search for all empty directories we can type:

$ fdfind -t d -t e
dir1/1a_test 
dir1/1b_test 
dir1/1c_test 
dir1/1d_test 
dir1/1e_test 
dir1/2a_test 
dir1/2b_test 
dir1/2c_test 
dir1/2d_test 
dir1/2e_test 
dir1/3a_test 
dir1/3b_test 
dir1/3c_test 
dir1/3d_test 
dir1/3e_test

You can also execute a command on each file found using -X (similar to find)

$ fdfind -t f -X cat {}
asdf 
asdf 
asdf 
asdf 
asdf 
my file name is test1 
my file name is test10 
my file name is test11 
my file name is test12 
my file name is test13 
my file name is test14 
my file name is test15 
my file name is test16 
my file name is test17 
my file name is test18 
my file name is test19 
my file name is test2 
my file name is test20 
my file name is test3 
my file name is test4 
my file name is test5 
my file name is test6 
my file name is test7 
my file name is test8 
my file name is test9

The -e option limits search to a specific extension. Together with other options shown above, you can use it to show the number of lines of a source tree.

$ fdfind -t f -e c -X wc -l 
350 ./src/lib.c 
280 ./src/main.c 
630 total

fdfind is a good replacement for find with one major advantage — speed.

Leave a Comment