I have directories that begin with 164 but they differ based on the last few digits. I would like to cd into a directory based on the last few digits if not the last digit itself, say 9 vs 8. The directories are unique in the last digits. Is it possible to do so? Autocomplete lists a number of possibilities when I begin with the first digits 164.

  • 2
    I'm using zsh with oh-my-zsh and have configured autocomplete to match the characters at any point in the file/directory name, not only at the beginning. Not an answer since the question is tagged bash.
    – jaskij
    Feb 2, 2021 at 14:39

3 Answers 3


With Bash, yes, you can use wildcards:

cd /path/to/*9/

(replace 9 with however many digits you need; you can drop /path/to/ if you’re in the directory containing all the 164... directories).

You need to ensure that the expression is specific enough to resolve to a single directory, otherwise cd will change to the first directory specified in its arguments (in Bash before version 4.4), or fail with an error (Bash 4.4 and later built with CD_COMPLAINS). (Take care with Zsh or Ksh which have a two-argument form of cd which you might invoke accidentally, albeit only if your current path contains the first argument.)

You can also tab-complete after typing the command above, before executing it; if more than one directory matches, your shell will list them all.


If they are actually distinct except for the last few digits, you can use a wildcard in the cd command, e.g.,

cd 164*8

(and if they are not actually distinct, the shell will remind you of this by producing an error message).


You could do something custom.

mycd() {
    local ng=$( shopt -p nullglob )
    shopt -s nullglob
    local status

    local matches=( *"$1"/ )  # directories ending in the parameter
    case ${#matches[@]} in
        0) echo "no directory ends with $1" >&2; status=1 ;;
        1) cd "${matches[0]}"; status=$? ;;
        *) echo "multiple directories end with $1" >&2; status=1 ;;

    $ng   # restore the previous nullglob setting. specifically unquoted
    return $status

mycd 89  # cd to the subdir ending with 89

That can be extended to use a select statement, when there are more than one matching dir, to allow you to choose which one you want.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.