3

(Mint 19.1, based on Ubuntu 18.04)

I have a directory I frequently access but which has a long path. I am tired of typing it out, so I want to be able to easily jump to this directory. The simplest method I can think of is making an alias in .bashrc, say:

alias goto_project="cd /projectdir"

This works but only works if I want to use cd. I figure it would be more general to add a symlink to /projectdir in path so that I could globally use commands like cd project or mv file project (move a file to the dir) or some rsync call. I tried placing a symlink to the directory into /usr/local/bin (I used ln -s /projectdir /usr/local/bin/projects). However, this doesn't seem to enable the use of cd project as expected. For instance, calling which projects produces nothing.

Is this approach not possible? Maybe because it would potentially produce potential conflicts?

  • 1
    If you type the cd command a lot, find a unique sequence of letters within the directory name that you rarely type in any other circumstances, then hit CTRL+R in your shell session to do a reverse-i-search for that specific substring. Such searches will also eventually find the command if you search for cd and hit CTRL+R afterwards enough times, but using a unique substring should (don't just blindly hit Enter :D) find your dir on the first match. – i336_ Aug 19 at 13:55
  • Maybe more of a workaround instead of a solution, but I've had good luck using the z script (github.com/rupa/z) to shortcut changing directories. It remembers where you've been so you can just enter z directory and it'll take you there – GammaGames Aug 19 at 14:06
  • One "trick" is to set up one user per project. The you can cd ~project, cp foo ~project/whatever. With the right groups, can be rather nice. (Or not, depending on context.) – Mat Aug 19 at 15:16
  • Just to clear up a bit of confusion in your question, the stuff in your path is only referenced for the command, not any of the arguments. That is, when you run ls project, the shell looks in your path for ls but not for project. It also only looks at executable files, which is why which project didn't produce any output. – Robobunny Aug 19 at 16:34
  • If all you’re doing is cd check $CD_PATH – vol7ron Aug 19 at 18:47
6

Aliases are for commands - what you need is a simple variable that references your long directory name. Add something like this to your ~/.bashrc:

shortdir="/super/long/directory/name"

Now, commands like ls "$shortdir" or du "$shortdir" will give you what you want.

3

With bash, you have the CDPATH variable. The following is said about it:

CDPATH The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. A sample value is ".:~:/usr".

If you have /my/super/very/long/path/to/projectdir then if you put /my/super/very/long/path/to/ in CDPATH, a cd projectdir from anywhere in the hierarchy will make you go to the specific /my/super/very/long/path/to/projectdir.

It has drawbacks (possible collisions) but I use it for example to point to the top of all my git worktrees so that I can switch to them just knowing (or autocompleting in fact) their name, irrespective to where they are really stored.

It solves of course only the "simple way to cd" part of the problem, not the global one of referencing some name in some arbitrary symbolic fashion for all commands to operate on.

You may also want to have a look at pushd/popd they can help in some scenarios.

2

When you create a symbolic link, it allows you to access the directory from the place where you created it. For instance, in your case it would allow you to do cd /usr/local/bin/projects instead of cd /projectdir which, depending on the length of the path to your project could save some keystrokes.

Compare for instance cd /my/super/very/long/path/to/projectdir to cd ~/projectdir if you had previously created a symlink to your home directory with ln -s /my/super/very/long/path/to/projectdir ~/projects.

Other than this, ajgringo619 solution is the only thing that comes to mind.

  • Yeah, the home directory seems like a good place to put such symlinks. (At least, that's what I've tended to do…) It saves lots of typing; and it also saves remembering paths — especially if they differ between machines. – gidds Aug 19 at 12:12
  • Also the home directory is where user-specific configs are stored so they seem appropriate. – qwr Aug 19 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.