How to cd into a directory only by typing the directory name

How to cd into a directory only by typing the directory name

In this tutorial I will show you, how to cd into a directory only by typing the directory name.

To do this, you have to activate the autocd.

For bash: shopt -s autocd

$ ls -ld naboo
drwxrwxr-x 2 razvan razvan 4096 2017-08-04 20:05 naboo
$ naboo
naboo: command not found
$ shopt -s autocd
$ naboo
cd naboo
$ pwd
/home/razvan/naboo

As you may observe, when I type the directory name, the shell makes cd to that directory.

If you type shopt -s autocd in the terminal, the setting will discard at the first reboot. To set in persistently, add shopt -s autocd in your ~/.bashrc file:

$ echo "shopt -s autocd" >> ~/.bashrc

For Zsh: setopt autocd

% ls -ld my
drwxrwxr-x 3 razvan razvan 4096 2017-08-04 03:34 my
% my
zsh: command not found: my
% setopt autocd
% my
% pwd
/home/razvan/my

The same as in bash, with the autocd activated, the shell does cd to the name of the directory written in the terminal.

To make this setting persistent, add “setopt autocd” in the ~/.zshrc:

$ echo "setopt autocd" >> ~/.zshrc

Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!

 
Enjoyed this video?
How to cd into a directory only by typing the directory name
"No Thanks. Please Close This Box!"