Can I add a shortcut to replace a path in Linux?

Two shortcuts I use all the time for things like this:

Aliases

alias somedir='cd /home/john/www/something/'

Then you can type somedir to go to that directory. Add these to your .bashrc.

Symbolic Links

ln -s /long/path/to/some/other/folder /shortcut

This will make a file at /shortcut which links to /long/path/to/some/other/folder. Then you can type cd /shortcut instead. The caveat of this is it fills up your root directory (or whichever directory you put the links in) pretty quick. I prefer aliases.


You can use the environment variable CDPATH for this. From the Bash man 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".

In your case, you can set

export CDPATH=.:/user/something/somefolders

in ~/.bashrc, and then typing cd somewhere would take you to /user/something/somefolders/somewhere (assuming there's no directory named somewhere within the current directory).

Alternatively, if you don't want to refer to the folder somewhere by its real name, you could create a hidden directory that contains a symbolic link to /user/something/somefolders/somewhere with the name you want to use. It could also contain links to any other directories you frequently visit. Then set CDPATH to include the path to that hidden directory. Although note that with this method, if you cd somewhere and then cd .., you'll wind up in the hidden directory. That may or may not be an issue for you.


I tend to use the bash interactive search all the time. Try it. Invoke it with ctrl+r and start typing some part of your path, like somewhere. Probably your cd command will pop up. :)