0

A dir I cd into a lot has a really long path. I'd like to reduce keystrokes.

I have tried this...

sudo ln -s /mnt/c/Users/me/ho/hum/bin /mybin

It works, but something about it seems wrong.

Is there a better way?

I currently use bash, but a solution that would still work if I switched shells would be nice.

System info

Running Unbutu on Windows Subsystem for Linux.

$ uname -a
Linux Hostname 4.4.0-17763-Microsoft #253-Microsoft  \
    Mon Dec 31 17:49:00 PST 2018  \
    x86_64 x86_64 x86_64 GNU/Linux
Nafine
  • 1
  • Also see https://unix.stackexchange.com/q/26802/70524 https://unix.stackexchange.com/q/146788/70524 https://unix.stackexchange.com/q/37181/70524 – muru Jun 16 '19 at 17:47

2 Answers2

1

A simple solution is to use an alias.  Put

alias mb='cd /mnt/c/Users/me/ho/hum/bin'

into your ~/.bashrc.  Then you can just type mb, and bash will execute the cd command.

Very many shells — I dare say most, if not all — support aliases.  However, you would need to add it to the initialization file (e.g., .cshrc, .zshrc) of every shell you want to use.  Be aware that the syntax of the alias command may vary between shells.

0

By default, cd searches relative to your working directory (if you specify a relative path, rather than an absolute path). You can add to the directories which will be searched for a match by setting the CDPATH environment variable in one of your shell initialization files. This is supported in at least bash and ksh.

From the bash manual page:

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".

For your example, if you had set

export CDPATH=.:/mnt/c/Users/me/ho/hum

then you could set /mnt/c/Users/me/ho/hum/bin as your working directory with just

cd bin

Note that if your intended destination has a common name (bin), the order of entries in your CDPATH variable will be important. If your working directory were /usr/local and CDPATH set as per the previous paragraph, then cd bin would take you to /usr/local/bin rather than /mnt/c/Users/me/ho/hum/bin.

user4556274
  • 8,995
  • 2
  • 33
  • 37