fd icon indicating copy to clipboard operation
fd copied to clipboard

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

Open MansoorMohsin opened this issue 2 months ago • 2 comments

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.

MansoorMohsin avatar Oct 27 '25 20:10 MansoorMohsin

First of all, I don't really understand where this would be useful.

Secondly, wouldn't the extension always be the same for the full path and the filename?

tmccombs avatar Oct 27 '25 23:10 tmccombs

I don't really understand where this would be useful.

This is useful when want to rename a file while keeping the same extension. Below is a concrete example of where this feature can be useful.

Example

I have a folder with multiple video files. Some are mov, some are mp4, and some are mkv. I want to apply compression encoder tool on all these videos. The encoded videos should have the filename of the format <original_filename>_encoded.<original_extension>. Without this feature, I have to run fd multiple times, one for each extension type:

fd -e mov -x encode {} -o {.}_encoded.mov
fd -e mp4 -x encode {} -o {.}_encoded.mp4
fd -e mkv -x encode {} -o {.}_encoded.mkv
...

With this feature available, a single command will do it:

fd -x encode {} -o {.}_encoded.{.ext}

Secondly, wouldn't the extension always be the same for the full path and the filename?

Yes, you are right. You can scratch that.

MansoorMohsin avatar Oct 28 '25 17:10 MansoorMohsin