Sequential vs Parallel Execution
Like find, fd can execute commands on the files it discovers. However, fd offers a much cleaner syntax and the ability to execute commands in parallel, massively speeding up batch processing tasks like converting images, linting code, or compressing logs.
fd provides two primary execution flags: -x (sequential/individual) and -X (batch).
1. Individual Execution (-x, --exec)
The -x flag executes a command once for each file found.
The syntax is -x [command]. You do not need to add the confusing \; at the end like you do with find. fd automatically injects the matched filename at the end of the command if you don't specify where it should go.
# Finds all .jpg files and runs `chmod 644` on each one individually
fd -e jpg -x chmod 644
The {} Placeholder
If the command requires the filename to be in a specific position, use the {} placeholder.
# Move all .bak files to the /tmp/trash directory
fd -e bak -x mv {} /tmp/trash/
Advanced Placeholders
fd goes beyond find by offering specialized placeholders for parsing the path:
{}: Full path (./src/main.rs){/}: Basename (main.rs){//}: Parent directory (./src){.}: Path without extension (./src/main){/.}: Basename without extension (main)
Example: Batch Conversion
Convert all .flac audio files to .mp3, keeping them in the same directory but changing the extension.
fd -e flac -x ffmpeg -i {} -b:a 320k {.}.mp3
Parallelism in -x
By default, fd -x executes commands in parallel. It spawns multiple threads to run the commands concurrently, making it significantly faster than find -exec.
If your command is not thread-safe (e.g., writing to a single shared file), you must force sequential execution using -j 1 (threads = 1).
2. Batch Execution (-X, --exec-batch)
The -X (capital X) flag executes a command exactly once, passing all the matched filenames to the command as a long list of arguments.
This is the equivalent of find -exec ... + or piping to xargs.
# Finds all .js files and runs a single linter process:
# eslint a.js b.js c.js ...
fd -e js -X eslint
When to use -X instead of -x
- High Overhead Commands: If starting the command takes 1 second (e.g., booting up a Node.js linter), running it 1,000 times individually (
-x) will take 16 minutes. Running it once (-X) takes 2 seconds. - Aggregation Commands: Commands like
tar,zip, orwcexpect a list of files to process together.
Example: Create a zip archive of all config files
fd -e conf -X zip configs.zip
If fd finds 500,000 files, passing them all to a single -X command might exceed the operating system's ARG_MAX limit (resulting in an "Argument list too long" error). fd attempts to handle this gracefully by chunking the execution, similar to how xargs behaves.