TL,DR: add . /etc/bash_completion
to your .bashrc
.
To answer the exact question you asked, there is no such built-in commands. Bash has commands to complete various kinds of objects (file names, user names, variable names, etc.) but not one that's restricted to directories.
However, unless you're running an antique version of bash, you can use context-sensitive completion, a feature added in version 2.04 which was released in 2000. Some distributions enable it by default; for others you need to install the bash-completion
package and activate it in your ~/.bashrc
. Most distributions provide a package called bash-completion
and put the activation script in /etc/bash_completion
, i.e. your .bashrc
should contain the line
. /etc/bash_completion
After this, if you enter cd
and press Tab, only directory names will be completed.
If you have a non-ancient version of bash (≥4.0), you can put shopt -s autocd
in your .bashrc
, and after this you can omit the cd
command, so you can write just bar
instead of cd bar
. Tab completion will complete both command names from $PATH
and directory names from the current directory (and $CDPATH
) on the first word of the line.
Bash 4.0 also lets you write your own command line edition functions (earlier versions kinda did but you had to jump through hoops). So if you really want you could do that. How to customize Bash command completion? has a relatively complex example; search for READLINE_LINE
and complete
to find other examples.
zsh
completion behaves closer to what you want.