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 /home/sk/Downloads/ostechnix [[email protected]]: ~/Downloads/ostechnix>$
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:
- How To Use Pushd, Popd And Dirs Commands For Faster CLI Navigation
- How To Navigate Directories Faster In Linux
And, that's all for now, folks. Hope this helps.
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Reddit | Facebook | Twitter | LinkedIn | RSS feeds
Have a Good day!!
1 comment
Thanks. Great job.