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 $ ).

~ $ Projects
cd -- Projects
~/Projects $ ~
cd -- /home/chuck
~ $ cd /mnt/dasd
/mnt/dasd $ /mnt/terastation
cd -- /mnt/terastation
/mnt/terastation $ cd
~ $

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.

{# State to enable switching directories in bash without typing cd#}
{# Created 2021-10-02 #}

{% if (grains['kernel'] == 'Linux') %}

{# user's .bashrc to alter #}
{% set user_home = 'chuck' %}

autocd:
  file.append:
    - name: /home/{{user_home}}/.bashrc
    - text: |

        # autocd to change directories without typing cd
        shopt -s autocd

{% endif %}

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.

 ~ $ sudo salt mc1_0* state.apply bash_settings.autocd test=False
mc1_0:
----------
          ID: autocd
    Function: file.append
        Name: /home/chuck/.bashrc
      Result: True
     Comment: Appended 3 lines
     Started: 07:10:32.929759
    Duration: 53.837 ms
     Changes:
              ----------
              diff:
                  ---

                  +++

                  @@ -7,3 +7,6 @@


                   alias ls='ls --color=auto'
                   PS1='[\u@\h \W]\$ '
                  +
                  +# autocd to change directories without typing cd
                  +shopt -s autocd

Summary for mc1_0
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  53.837 ms

I think I will call this a post.

References