sharkdp/fd · error · anyhow::Error

'{}' is not a valid size constraint. See 'fd --help'.

Error message

'{}' is not a valid size constraint. See 'fd --help'.

What it means

Thrown by SizeFilter::from_string in src/filter/size.rs:30 when the input fails to match the regex '(?i)^([+-]?)(\d+)(b|[kmgt]i?b?)$'. A valid --size argument needs an optional '+'/'-' sign, a decimal quantity, and a unit of b, k/m/g/t (optionally with 'i' for binary and trailing 'b'); anything else is rejected.

Source

Thrown at src/filter/size.rs:30

    Equals(u64),
}

// SI prefixes (powers of 10)
const KILO: u64 = 1000;
const MEGA: u64 = KILO * 1000;
const GIGA: u64 = MEGA * 1000;
const TERA: u64 = GIGA * 1000;

// Binary prefixes (powers of 2)
const KIBI: u64 = 1024;
const MEBI: u64 = KIBI * 1024;
const GIBI: u64 = MEBI * 1024;
const TEBI: u64 = GIBI * 1024;

impl SizeFilter {
    pub fn from_string(s: &str) -> anyhow::Result<Self> {
        SizeFilter::parse_opt(s)
            .ok_or_else(|| anyhow!("'{}' is not a valid size constraint. See 'fd --help'.", s))
    }

    fn parse_opt(s: &str) -> Option<Self> {
        let pattern =
            SIZE_CAPTURES.get_or_init(|| Regex::new(r"(?i)^([+-]?)(\d+)(b|[kmgt]i?b?)$").unwrap());
        if !pattern.is_match(s) {
            return None;
        }

        let captures = pattern.captures(s)?;
        let limit_kind = captures.get(1).map_or("+", |m| m.as_str());
        let quantity = captures
            .get(2)
            .and_then(|v| v.as_str().parse::<u64>().ok())?;

        let multiplier = match &captures.get(3).map_or("b", |m| m.as_str()).to_lowercase()[..] {
            v if v.starts_with("ki") => KIBI,
            v if v.starts_with('k') => KILO,

View on GitHub (pinned to 41532d114e)

Solutions

  1. Append a unit: 'fd --size +1k' or 'fd --size -100mib'.
  2. Use '+' for minimum size and '-' for maximum size: 'fd --size +1g' (>= 1 GiB), 'fd --size -10k' (<= 10 KB).
  3. Check the regex examples in 'fd --help' and confirm the whole token matches '<sign?><digits><unit>'.

Example fix

// before
fd --size 100

// after
fd --size -100k
Defensive patterns

Strategy: validation

Validate before calling

# accept <[+-]?>,<digits>,<unit>  where unit in b|k|m|g|t with optional i/b
if ! printf '%s' "$SIZE" | grep -Eq '^[+-]?[0-9]+(b|[kmgt]i?b?)$'; then
  echo "invalid --size: $SIZE" >&2; exit 1
fi
fd --size "$SIZE"

Type guard

use regex::Regex;
use std::sync::OnceLock;
fn is_valid_size(s: &str) -> bool {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"(?i)^([+-]?)(\d+)(b|[kmgt]i?b?)$").unwrap())
        .is_match(s)
}

Prevention

When it happens

Trigger: Passing 'fd --size 9999' (no unit), 'fd --size +18' (no unit), 'fd --size +50a' (bad unit), 'fd --size 1bib' (invalid unit), or 'fd --size $10M' (currency char).

Common situations: Forgetting the unit suffix (the most common form); mixing up binary vs decimal prefixes; quoting mistakes that leave a shell '$' or stray space in the argument.

Related errors


AI-assisted analysis of sharkdp/fd@41532d114e (2026-08-06). Data as JSON: /data/errors/0306286966815afd.json. Report an issue: GitHub.