Pro Terminal Commands: Use fd on macOS to Find Files

Table of Contents

fd is a simpler alternative to the find command. It uses a truncated syntax and an abbreviated command structure to keep your typed commands short and to the point. However, that lack of verbosity that makes fd easy to type makes it more difficult to understand. fd also runs pattern matching more rapidly than the default find commands. Learn how to use fd on macOS Terminal to find files quickly and accurately.

Install fd on macOS

fd is not included with macOS by default. To use fd on macOS, you’ll need to download and install the application first. Then you can run it from within Terminal.

To install fd, begin by opening a fresh Terminal window. You can find Terminal at /Applications/Utilities/Terminal.app or through Spotlight.

1. If you don’t have Xcode installed already, install the Xcode command line tools with the Terminal command below:

xcode-select --install

2. Install Homebrew using the command below:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

3. Then use the command below to install fd:

brew install fd

4. Once fd is finished downloading, you can confirm its existence by typing man fd and viewing the man page.

use fd on macOS fd man page

Using fd on macOS

use fd on macOS example command

fd commands have a basic structure of fd pattern compared to find . -iname 'pattern'.

To search for a file by its name, use the following command:

fd filename

This searches within the preset working directory, including subdirectories. To search within a specific directory, specifying it after your search term:

fd filename /path/to/search

Executing Commands on Results

Like find, fd has a functionality that passes found files to another command for execution. Where find uses find . -iname pattern -exec command, fd uses the -x flag:

fd -e zip -x unzip

This command would send all zip files in the directory to the unzip command. You can use the following symbols within the “phrase” of the execute command to pass information in specific ways.

  • {}: A placeholder is replaced with the path of the search result (files/images/portrait.jpg).
  • {/}: A placeholder that will be replaced by the the filename of the result only, known by UNIX aficionados as the basename (portrait.jpg).
  • {//}: Placeholder replaced with the parent directories of found items (files/images).
  • {.}: Placeholder replaced with the path to the filename, without extension (files/images/portrait).
  • {/.}: Placeholder replaced with the basename of the found item, without extension (portrait).

Consider the following example, using two of the above-cited placeholder symbols:

fd -e flac -x ffmpeg -i {} -c:a libopus {.}.opus

This command would return all the files with the extension flac. Then, it would send them through the ffmpeg command. The {} placeholder would be replaced with the path to the files that fd found. The {.} would be replaced with the basename of the files that fd found. In the end, you’d have flac files and opus files side-by-side in the same folder.

Other Useful fd Flags

  • -e: search the extension of files only, with no separating dot.
  • -E pattern: exclude results matching the following pattern.
  • --changed-newer-than date|duration: filter results based on the time since file modification. This will only show files with modification dates later than the specified date. The time can be given a duration that count backwards from the present moment (10h, 1d, 35min) or set to a specific time ("YYYY-MM-DD HH:MM:SS").
  • –changed-older-than date|duration: Like –change-newer, but shows files editing before the specified date or duration, not after.
  • -t: only show files of the specified type (-tf for files, -td for directories, -tx for executables, -tl for symlinks, -te for empty files).
  • -p: search within the entire pathname, not just the filename.
  • -s: force case-sensitivity. By default, fd ignore case unless an uppercase letter is typed in the search pattern.
  • -H: show hidden files and directories within results
  • -L: follow links into symlinked directories.

Conclusion

Thanks to its abbreviated format, fd makes a useful replacement for the unwieldy find command. The syntax of fd is truncated, but it’s also straight-forward and generally easier to understand that the find command’s confusing organizational structure. It’s also easier to search with various restriction. But, in certain cases, fd may not be as powerful as the find command. You’ll want to keep both in your tool kit, but fd is a great daily driver when you’re hunting for files.

You might also like the following posts:

Getting Started with Terminal: Install and Use wget

Pro Terminal Commands: Working with chflags in macOS

Pro Terminal Commands: Customize Terminal on macOS

Picture of Kokou Adzo

Kokou Adzo

Kokou Adzo is a stalwart in the tech journalism community, has been chronicling the ever-evolving world of Apple products and innovations for over a decade. As a Senior Author at Apple Gazette, Kokou combines a deep passion for technology with an innate ability to translate complex tech jargon into relatable insights for everyday users.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts