In this tutorial I will show you, how to automatically switch to a directory without using Cd Command.
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.
How to 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:
And, add the following line at the end:
shopt -s autocd
Update the changes made in the .bashrc file with the following command:
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>$
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.
Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!