Bash Change Directories without Using cd
Whilst reviewing my Mastodon feed this morning, I came across a post from SK at OSTechNix which suggested it was possible to change directories in Linux without using the
cd command. This was sufficient to catch my attention. I read the full article, made the adjustment to my
.bashrc file and like the result so I decided to post as a reminder to future me, and create a
salt state file to push out the change to all machines if I still like it in a week.
What Is This Magic?
So it isn’t really magic after all, but by simply typing a directory path at the Bash command line and pressing Enter key, I can switch to that location using this one simple trick. Since my shell is Bash, adding
shopt -s autocd to my
.bashrc file, saving it and sourcing the change
source ~/.bashrc were the required actions to enable.
At this point I can simply type a directory path, press Enter key and be switched to that path. From now on I can save three characters (
cd ) of typing every time I change locations from the command line. As a bonus, it appears if I inadvertently type
cd in front of a directory name it continues to work as I would expect.
In the code block below, what follows the
$ is that I typed and pressed Enter key on. You can see the path changing in the prompt (before the
I am surprised I never came across this tip before, but I think this one is a keeper.
Salt State File
Initial pass at a salt state file to implement on other machines remotely. Change applied will not take effect until user next logs on after implementation.
Sample output from applying state change to a remote machine. Seems to be working as expected. I will need to come up with a state to revert the setting before I push it out further.
I think I will call this a post.
- Free Software Foundation. (2020, December 21). Bash Reference Manual Gnu. Retrieved from http://www.gnu.org/software/bash/manual/bash.html
- Kumar, S. (2017, July 28). Automatically switch directory without using cd command. OSTechNix. Retrieved from https://ostechnix.com/automatically-switch-directory-without-using-cd-command/