Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add placeholder for file extension ({.ext} or {/.ext}) #1818

@MansoorMohsin

Description

@MansoorMohsin

Summary

Currently, fd provides placeholders for:

  • {} → full path
  • {.} → path without extension
  • {/} → basename
  • {/.} → basename without extension

…but there’s no placeholder for just the file extension.

This makes it awkward to use --exec or --exec-batch commands that depend solely on a file’s extension (e.g. converting, filtering, or renaming files by type), since users must rely on shell-specific parameter expansion.

Problem

Example:

fd . -x echo '{.}'

prints the path without the extension.

There’s no way to easily get just:

txt
jpg
rs

Without resorting to shell tricks like:

fd . -x bash -c 'echo "${1##*.}"' _ {}

This limits usability, readability, and portability—especially on non-POSIX shells.

Proposed solution

Introduce a new placeholder to capture just the file extension:

Placeholder Meaning
{.ext} Extension of the full path (e.g. "txt" for "dir/file.txt")
{/.ext} Extension of the basename only (e.g. "txt" for "file.txt")

Example usage

fd . -x echo '{.ext}'
# Output:
# txt
# jpg
# md

Implementation concept

In src/exec/mod.rs, extend the placeholder handling logic:

"{.ext}" => match full_path.extension() {
    Some(ext) => ext.to_string_lossy().into_owned(),
    None => String::new(),
},
"{/.ext}" => match file_name.extension() {
    Some(ext) => ext.to_string_lossy().into_owned(),
    None => String::new(),
},

Benefits

  • Improves parity with existing {.} / {/.} placeholders.
  • Enables concise, readable, and portable fd --exec one-liners.
  • Keeps extension logic inside fd, avoiding shell-specific syntax.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions