Automatically Switch To A Directory Without Using Cd Command

Automatically Switch To A Directory Without Using Cd Command

The reason I like very much to work from command line is there are plethora of Bash tricks to make things much easier and faster. This simple bash trick can help you to automatically switch to a directory without using cd command. All you need to do is just the enter the path of the directory in the Terminal, and you will be landed to that particular directory. This could be useful in scripting and  for those who use command line a lot at work.

Automatically Switch To A Directory Without Using Cd Command

Let us say you wanted to go to the directory Downloads. You don’t need to type “cd Downloads” every time to go that directory. Instead, just type “Downloads” in the Terminal. Sounds cool, yeah? Indeed!

When you type Downloads in the Terminal, you will see the following message:

$ Downloads/
bash: Downloads/: Is a directory

You can type “cd Downloads” to go the Downloads directory, right? But, wait! You don’t have to use cd command anymore. To do so, edit your .bashrc file:

vi ~/.bashrc

And, add the following line at the end:

shopt -s autocd

Update the changes made in the .bashrc file with the following command:

source ~/.bashrc

All done! You can now go to any directory without using cd command. Just enter the path of the directory like below. Bash can automatically prepend cd when entering just the path of any directory in the Terminal.

[sk@sk]: ~>$ Downloads/
cd -- Downloads/
[sk@sk]: ~/Downloads>$ Telegram/
cd -- Telegram/
[sk@sk]: ~/Downloads/Telegram>$

autocd

Look at the above screenshot. Did I use “cd” command? Nope. I just entered the Directory name and it automatically went to the respective directory.

And, that’s all for now folks. Hope this helps. If you find this guide useful, please share it on your social, professional networks and support OSTechNix. More good stuffs to come. Stay tuned.

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

  • Frank Osam-Pinanko

    Thanks. Great job.