DEV Community

loading...

Why to `find` files when you can `fd` them ;)

chhajedji profile image chinmay chhajed ・2 min read

If you happen to be a frequent terminal user, you must be using the GNU find command to know the location of files. But as the number of files increase, you can see a significant increase in runtime of find. This is where the younger sibling fd comes in picture ;)

According to the README of the project, some highlights of fd are:

  • Very fast due to parallelized directory traversal.
  • Uses colors to highlight different file types (same as ls).
  • The command name is 50% shorter than find :-)

For installation, most package managers like brew, apt, dnf, pacman, emerge, zypper and others have fd package so you can just run the standard installation command. For more details, see the installation guide for your system.

Note that for Debian based distros like Ubuntu and others, you will have to install fdfind because fd is already used by a different package. You can see the guide how to use fd on Ubuntu instead of fdfind. Spoiler alert: It's not about creating alias. xD

Personally, I am very impressed with the speed of fd. When I had to wait for few seconds for find to finish, fd does the work within a second. Also, if you know regex , fd is a big blessing as it uses regex for searching.

Coming to some numbers from comparison:

Number of all .png files in my home directory searched with find:

# Do a regex search with `find` with ignoring case
# and piping output to `wc -l` which will count number
# of lines in result 
find ~/ -iregex '.*\.png$' 2>/dev/null | wc -l
# 13530 results found
Enter fullscreen mode Exit fullscreen mode

Number of all .png files in my home directory searched with fd:

# Same regex, but now with `fd`
# -H flag to search in hidden directories
# -I to not ignore any files. `fd` ignores files like
# .gitignore, .ignore, .fdignore

fd -HI '.*\.png$' ~/ 2>/dev/null | wc -l
# 13530 results found
Enter fullscreen mode Exit fullscreen mode

So the number of results are same in both cases.

Now let's see the time taken for each command.

  • find:
time (find ~/ -iregex '.*\.png$' >/dev/null 2>&1)

# real    0m3.892s
# user    0m2.944s
# sys     0m0.937s
Enter fullscreen mode Exit fullscreen mode
  • fd:
time (fd -HI '.*\.png$' >/dev/null 2>&1)

# real    0m0.607s
# user    0m2.110s
# sys     0m1.779s
Enter fullscreen mode Exit fullscreen mode

As you can see, fd is more than 6 times faster than the GNU find. When you find files frequently, the time saved is quite significant! Experiencing the power of fd can be easily done with other programs like fzf and dmenu which I'll keep for another post ;).

Screenshots of above commands and their outputs for my system:
alt text

Discussion (0)

pic
Editor guide