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

In this article I will show you how to cd into a directory by only 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 2012-06-29 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 2012-07-01 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

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!