2

I'm using Kubuntu 22.04, with "Konsole" and "Terminator".

I have set a default starting directory for each of them (let's call it /my/quite/complex/default/destination/directory).

After working a lot, traveling between many different directories, I want to go back to that default, without having to type all that... Is there a simple cd command to do it?

Note: that directory IS NOT /home/myUser, therefore cd ~ won't solve the problem, and cd - only goes back to the last one, not the very first.

Thank you all.

Borjovsky
  • 173
  • 1
  • 9

3 Answers3

1

Edit your ~/.bashrc to store the starting directory somewhere, usually in a shell variable, whenever the shell is launched. For example:

startdir=$PWD

Once you have that, you can either use it directly as cd "$startdir" (for this you could choose a shorter variable name such as $h), or make aliases / functions:

alias ret='cd "$startdir"'
ret() { cd "$startdir"; }
user1686
  • 615
1

Some options:

  • An alias, added to your ~/.bashrc, can already cut it:

      alias mcd='cd /my/quite/complex/default/destination/directory'
    

    Now, the command mcdwill bring you to that directory.

  • In ~/.bashrc, you may define your directory in a variable as

    export MD=/my/quite/complex/default/destination/directory
    

    Then, cd $MD will take you to that directory.

  • In a slightly more sophisticated approach, you can override your cd command using a function that, without arguments, brings you to your desired directory, and with arguments works as usual. This can be done with a bash function as follows. Include it in ~/.bashrc.

    cd () {
       if [ -z $1 ] ;  then
          command cd /my/quite/complex/default/destination/directory 
       else
          command cd $@
       fi
    }
    
  • Changing the $HOME variable in ~./bashrc will fundamentally achieve what you want. This would not affect the desktop environment, because the changed variable is only in effect in your interactive terminals and subshells, but there is still a chance that specific graphical (system) tools may malfunction with the wrong HOME set.

    Temporarily changing HOME in the function, however, should be safe:

    cd () {
       OLDHOME=$HOME
       HOME=/my/quite/complex/default/destination/directory
       command cd $@
       HOME=$OLDHOME ; unset OLDHOME
    }
    
vanadium
  • 88,010
  • Great explanation. But I'll stick with user1686's answer, for simplicity and using the $PWD variable (more flexible: if I change the default terminal starting directory, it will just follow naturally). – Borjovsky Oct 16 '22 at 20:36
0

Look at CDPATH from the bash man pages.
export CDPATH=".:~:/my/quite/complex/default/destination"

Then cd directory will take you to /my/quite/complex/default/destination/directory. Provided you don't have "directory" any earlier in the CDPATH.

ubfan1
  • 17,838