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
To do so, edit your
$ 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.
[[email protected]]: ~>$ Downloads/ cd -- Downloads/
[[email protected]]: ~/Downloads>$ ostechnix/ cd -- ostechnix/
[[email protected]]: ~/Downloads/ostechnix>$ pwd /home/sk/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 just proof that shows we can do some cool stuff in Linux.