Marc Ermshaus About Contact

Published on 7 Nov 2026. Tagged with bash, micro.

Here’s a cdsub shell function that lets you navigate into a subdirectory by typing any part of its name (like cd, but with partial matching). This is particularly useful when directory names are prefixed with a year and month, so you can simply type the more meaningful part of the name.

cdsub() {
    if [[ $# -ne 1 || -z "$1" ]]; then
        echo "(ERR) Please provide a query."
        return 1
    fi

    local d
    d=$(find . -mindepth 1 -maxdepth 1 -xtype d -iname "*$1*" -printf '%f\n' | sort | head -n 1)

    if [[ -z "$d" ]]; then
        echo "(ERR) No directory matching '$1'."
        return 1
    fi

    cd -- "$d" || return 1
}

Example:

2026_11_01-foo  2026_11_02-bar  2026_11_03-qix

$ cdsub bar