41

For example, I always go to this path:

/user/something/somefolders/somewhere

but I don't want to type

cd /user/something/somefolders/somewhere

in the terminal all the time, can I have some short hand to do so? for example, can I do something like

cd commandPlace

to replace the path?

35

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.

56

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.

3
  • nice one John !
    – Gob00st
    Oct 3 '12 at 14:54
  • it's supposed to be ln -s /long/path/to/some/other/folder /shortcut when I did it the other way around it created the shortcut in the place I wanted to create the link to. I submitted an edit to make it updated.
    – Elias
    Aug 2 '13 at 16:38
  • 1
    How about a directory in the root with a short name, e.g. /sl and create shortcuts in there? (ie ln -s /long/path/to/some/other/folder /sl/shortcut? No clutter in the root, plus a visual reminder that your prompt's path is a symlink.
    – RolfBly
    Nov 13 '16 at 19:26
14

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. :)

0
11

Another thing you can do is to store the path in question in an environment variable. Add these lines to your ~/.profile file:

somedir=/user/something/somefolders/somewhere
export somedir

You can then access the directory with

cd "$somedir"
2
  • 1
    This is by far the best solution imo because you can use the variable for any command. If you assign an alias then that alias is always tied to a command like cd...
    – BdN3504
    Jul 23 '14 at 12:27
  • This is the solution I was hoping existed, and was looking for. For the reasons BdN3504 has stated.
    – inspirednz
    Feb 17 '18 at 5:11
9

Look at the "alias" command.

In csh:

alias commandplace "cd /user/something/somefolders/somewhere"

In sh:

alias commandplace="cd /user/something/somefolders/somewhere"

But I like the symlink solution:

ln -s /user/something/somefolders/somewhere ~/commandplace 

Note: ln takes arguments in the same order as cp.

3

You can create an alias to execute that command:

  1. Create a new file ~/.bash_aliases (if not already created)
  2. Add this to that file: alias something='cd /user/something/somefolders/somewhere'. Save file.
  3. In a new terminal window, type 'something' and it'll cd to that path.

It is recommended that all user added aliases should be added to ~/.bash_aliases. This file is directly read from ~/.bashrc.

1

The alias method described by other answers is the most direct route.

Another option is to try autojump, described as "A cd command that learns". More description in this LifeHacker post.

0

The following is taken from a ZSH (not bash or sh) book: enter image description here

You can use a hash to give a particular directory an alias. This is particularly convenient for your workspace:

% echo $GOPATH
  /Users/gfestari/workspace/go

I don't want to type /Users/gfestari/workspace/go each time I want to reach the src folder in my $GOPATH directory. So how about putting hash to a good use?

% hash -d gosrc=$HOME/go/src

And now we can get there as quick as typing cd ~gosrc (pay attention to the leading ~ character). Here's another example, this time using the /var/www directory:

% hash -d www=/var/www
% cd ~www
  /var/www

You can go ahead and hash your most frequently visited directories. Just remember to add the required entries to your .zshrc, so you don't have to type the same thing over and over again.

For bonus points, set the AUTO_CD option, so you only need to input the directory's name whenever you want to change the working directories:

% setopt autocd
% ~www
  /var/www

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.