Home Command line utilities Automatically Switch To A Directory Without Using Cd Command

Automatically Switch To A Directory Without Using Cd Command

By sk
Published: Last Updated on 2798 Views

Most of the intermediate and almost all advanced users prefer CLI over GUI mode, because there are plethora of command line tricks to make things much easier and faster. Today, I'd like to share one simple tip. This simple bash trick can help you to automatically switch to a directory without using cd command. All you need to do is just enter the path of the directory in the Terminal, and you will be landed in that particular directory. This could be useful in scripting and  for those who use command line a lot at work. Read on.

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!

Let us simply type Downloads in the Terminal, and see what happens.

$ Downloads/
bash: Downloads/: Is a directory

You should type "cd Downloads" to go the Downloads directory, right? But, wait! You don't have to use cd command anymore. All you have to do is add a single line in your .bashrc file.

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.

[[email protected]]: ~>$ Downloads/
cd -- Downloads/
[[email protected]]: ~/Downloads>$ ostechnix/
cd -- ostechnix/
[[email protected]]: ~/Downloads/ostechnix>$ pwd
[[email protected]]: ~/Downloads/ostechnix>$
Automatically Switch To A Directory Without Using Cd Command In Linux

Automatically Switch To A Directory Without Using Cd Command

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

Another advantage of this method is it not only helps you switch to successive directories, but also go to any different paths.

For example, let us say you are currently in /home/sk/Downloads directory. You want to go to /var/mail/. How would you do that? Simple. Just type the full path of the respective directory i.e /var/mail/ in our case. Again you don't need to mention 'cd' command. Look at the following screenshot.

Similarly, you can go to any location without having to use 'cd' command.

It may not be helpful for you all the time. Because cd is just two letter command. It won't be long to type just two characters. However, this is a proof that we can do anything in Linux.

Suggested read:

And, that's all for now, folks. Hope this helps.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

1 comment

Frank Osam-Pinanko July 29, 2017 - 1:47 am

Thanks. Great job.


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More