Skip to main content

Automated Linting and Formatting

Modern projects often use tools like Prettier, ESLint, Black, or Rustfmt to enforce code style. While these tools often have their own directory traversal logic, passing a specific subset of files via fd is faster and more precise.

1. Batch Formatting

Formatters usually prefer to receive a large list of files at once so they can boot up their internal engine a single time. We use fd's -X (batch execution) flag for this.

# Format all JavaScript and TypeScript files using Prettier
fd -e js -e ts -X npx prettier --write

2. Linting Specific Paths

Sometimes a project's .eslintignore is misconfigured, or you only want to lint files modified within a certain timeframe to avoid noisy legacy errors.

# Run ESLint ONLY on files modified in the last 2 days
fd -e js --changed-within 2d -X npx eslint

3. Auditing Permissions

If you are developing shell scripts, you need to ensure they have the executable bit set before committing them to Git.

# Find all .sh files that are NOT executable, and make them executable
fd -e sh -t f -x chmod +x

4. Deleting Build Artifacts

When make clean or npm run clean fails, you can use fd to aggressively hunt down and remove build artifacts.

# Find all directories named "target" or "build" and delete them
fd "^(target|build)$" -t d -X rm -rf
Be Careful with rm -rf

When piping fd to destructive commands, always perform a dry run first by running the fd command alone to review the output.