Skip to main content

Depth and Pruning

Even with parallel execution and .gitignore support, crawling the root partition (/) of a server with millions of files is computationally expensive.

To optimize performance and prevent fd from wandering into high-latency areas (like network mounts), you must control its traversal scope.

1. Depth Control (-d, --max-depth)

The simplest way to restrict a search is to limit how deep fd is allowed to crawl into the directory tree.

# Search only the current directory (do not enter any folders)
fd pattern -d 1

# Search down to a maximum of 2 subdirectories
fd pattern -d 2

Exact Depth (--exact-depth)

If you know the files you want are exactly 3 levels deep (e.g., year/month/day/file.log), you can use --exact-depth.

# Only matches files exactly 3 directories down
fd "\.log$" --exact-depth 3

2. Pruning Paths (-E, --exclude)

If you want fd to ignore a specific directory that is NOT listed in a .gitignore file, use the -E flag.

This is the equivalent of find's verbose -prune action, but much simpler.

# Search /var, but completely skip /var/cache and /var/tmp
fd -E cache -E tmp pattern /var

Exclude Patterns

The -E flag accepts glob patterns.

# Exclude any directory starting with "test_"
fd pattern -E "test_*"

3. Limiting Threads (-j, --threads)

fd automatically detects the number of CPU cores available and spawns a proportional number of search threads.

On production servers, launching a CPU-intensive parallel search might starve other critical applications (like web servers or databases) of resources.

You can throttle fd by limiting its thread count.

# Force fd to run on a single thread (minimizing CPU impact)
fd pattern -j 1

# Limit fd to 4 threads
fd pattern -j 4

4. Handling Network Mounts

By default, fd will traverse across filesystem boundaries. If your server has a massive, slow NFS share mounted at /mnt/backup, running fd from / will cause the search to crawl to a halt as it reads the remote drive.

Unlike find (which has -xdev), fd does not have a native "stay on this filesystem" flag. You must explicitly exclude the mount points.

fd pattern / -E /mnt -E /media