Score:2

Go back to default directory in terminal

fr flag

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.

hr flag
If you are using the bash shell, you may find `pushd` and `popd` useful - refer to the bash manual under [The Directory Stack](https://www.gnu.org/software/bash/manual/bash.html#The-Directory-Stack). See also [Short-cut for switching to a given directory](https://askubuntu.com/questions/226417/short-cut-for-switching-to-a-given-directory)
Score:1
us flag

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"; }
Borjovsky avatar
fr flag
Simple, direct, succinct, and just works.
Score:1
cn flag

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
    }
    
Borjovsky avatar
fr flag
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).
Score:0
cc flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.