Can the command completion for `cd` be modified to only show directories and ignore files?

When I am in a directory in bash, and I press cd Space Tab, it shows everything in the directory as a possibility. (Show all 1000 possibilities?) This is really cumbersome when I am in a directory with lots of regular files and relatively few directories.

So, is it possible to make the choices for autocompletion of cd to only include directories?

I know I can get a directory listing within a directory by doing

ls -d */

but I’m not sure how to proceed from there.

I am using CentOS 6.6 Final.

Asked By: chiliNUT

||

Just add

complete -d cd

in your ~/.bashrc (or other bash configuration file).

Answered By: jimmij

This should be happening automatically on a typical install on many distros.
If it is not, you’re probably missing the bash-completion package:

  • Ubuntu: sudo apt-get install bash-completion
  • Arch: sudo pacman -S bash-completion
Answered By: user541686
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.