Changing to a specific directory in an easier manner [duplicate]

I'll write this assuming you don't know much about Ubuntu - please accept my apologies if it's too straightforward.

Absolutely you can. You need to create an alias. Pick a name for the command you want to alias to the command you want executed. Open a terminal (CtrlAltT) and type it to check it is not currently assigned to something.

So let's say you want to use the command prog to move to that directory, you can make it temporarily happen by creating an alias for your absolute path to the directory you want. Open a terminal and type

alias prog="cd /home/user/Desktop/Programming/C++"

(obviously you'll need to put in the correct path to your target folder; do note it has to be an absolute path not a relative one).

Then, within that terminal session, every time you type prog it will execute the cd /home/user/Desktop/Programming/C++ command.

Note that this will only work during that terminal session. To make it permanent, you'll need to edit the .bashrc file:

You need to be comfortable editing text configuration files.

I’m not personally comfortable using Vi / Vim – but nano is pretty straightforward.

It involves editing .bashrc which is in your home directory.

This is, I think, pretty safe, even if you're not very familiar with editing such files:

  1. Open a terminal (CtrlAltT)

  2. Go to your home directory with cd ~

  3. Confirm .bashrc is there with ls -a

  4. Assuming it is, first create a backup copy of it so you can undo it if you mess it up completely:

     cp .bashrc .bashrc-bak
    

    (if you cause a problem you can reinstate the original .bashrc file by typing cp .bashrc-bak .bashrc)

  5. Now you can edit .bashrc knowing there is a safe copy:

    Type nano .bashrc (or you can use an alternative text editor if you prefer)

    This opens .bashrc in the text editor.

Whilst you can add the lines anywhere, it makes sense for future editing to find a space where there are some aliases and add yours below the ones there:

alias prog=”cd /home/user/Desktop/Programming/C++” 

Then save the file by hitting CtrlO, and exit nano with CtrlX. If you make a mistake, if you can't undo it, exit nano (CtrlX) without saving and start again.

The change will take effect as soon as you either start a new terminal session, or type . ~/.bashrc (note the . at the beginning!).


There are some possibilities.

• In first instance, you could maintain aliases that bring you to specific folders, e.g

alias cdplus='cd Desktop/Programming/C++'

The command cdplus will then bring you in that directory.

• Alternatively, you can make use of the CDPATH variable. In such variable, you include directories in which the cd command will search for matching folders, so you do not have to type the full path. e.g. if you define

CDPATH=".:Desktop/Programming"

you will be able to cd C++ (and any other folder under Programming) irrespective of your current directory. The first entry, ., means the current folder, so you can keep cding into a folder in the current folder.

• Third party tools exist for fast directory navigation, such as goto, which work in a similar way as with aliases, but keeps these definitions apart. You could also use fuzzy finder (fzf, sudo apt install fzf). On Ubuntu, a bash shortcut, Alt+c ("Current directory") is automatically set up to quickly navigate to any directory under the current folder. You get a list of all directories that quickly narrows down as you type parts of the name. Hitting Enter bring you in that directory.

• A tool like autojump (sudo apt install autojump) registers the directories you visit, and allows you to revisit them quicky just by typing their name after the autojump command. This tool "learns" from your current useage, without a need to maintain a list. An alternative is Shonejump, a rewrite in the programming language "Go".