Home Bash Tips How To Automatically Switch To A Directory Without Using Cd Command In Linux

How To Automatically Switch To A Directory Without Using Cd Command In Linux

By sk
Published: Last Updated on 9.3K 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.

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 the 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 will automatically prepend cd when entering just the path of any directory in the Terminal.

[sk@sk]: ~>$ Downloads/
cd -- Downloads/
[sk@sk]: ~/Downloads>$ ostechnix/
cd -- ostechnix/
[sk@sk]: ~/Downloads/ostechnix>$ pwd
/home/sk/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.

Switch to directories without cd command
Switch to directories without cd command

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 just proof that shows we can do some cool stuff in Linux.

You May Also Like

4 comments

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

Thanks. Great job.

Reply
Ramsay L October 3, 2021 - 10:30 pm

typo error in last line: “do cool stuffs in LInux” should be “do cool STUFF in Linux”
(the plural for stuff is stuff)

Reply
sk October 4, 2021 - 10:09 pm

Fixed. Thanks for pointing it out.

Reply
dnorhoj May 19, 2022 - 10:29 pm

Just use zsh at that point lmao

Reply

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. By using this site, we will assume that you're OK with it. Accept Read More