sharkdp/fd

find a file upwards

gutenye opened this issue · 2 comments

I want to find the package.json or .nvmrc file upwards (the closest upward directory). Start from the current directory, then the parent directory, until it finds one in the ancestor directory.

Although, that would be useful, I think that is out of scope for fd.

If it helps, here is a bash function that would accomplish that (assumes linux, or other unix-like):

function upfind() {
  local target current
  target="$1"
  current="$(realpath --canonicalize-existing "$PWD")"

  while [[ "$current" != "/" ]]; do
    if [[ -e "${current}/${target}" ]]; then
      printf "%s\n" "$current"
      return
    else
      current="$(dirname "$current")"
    fi
  done
  return 1
}

Thanks.