1103

· 👁 20 views

My new #bash alias: one function to go to the next folder (like from 2025 to 2026, from aaa to bbb) and the second one to #cd to prev:

cdn() {
  local cur next
  cur="$(basename "$PWD")"

  next="$(
    find .. -mindepth 1 -maxdepth 1 -type d -printf '%f\n' \
    | sort \
    | awk -v cur="$cur" '$1>cur{print; exit}'
  )"

  if [[ -z "$next" ]]; then
    echo "no next folder"
    return 1
  fi

  cd "../$next"
}

cdp() {
  cur="$(basename "$PWD")"

  prev="$(
    find .. -mindepth 1 -maxdepth 1 -type d -printf '%f\n' \
    | sort \
    | awk -v cur="$cur" '$1<cur{p=$1} END{print p}'
  )"

  if [[ -z "$prev" ]]; then
    echo "no previous folder"
    return 1
  fi

  cd "../$prev"
}