12

I find myself opening a terminal and then CD'ing a lot into the same directory the path of which is very long.

This means I am cd'ing a lot and using the tab to speed things up a lot.

IT would be great if I could have some sort of link to this directory and then just cd to that so that I would be in the deep nested directory quickly.

Is this possible?

1
  • 1
    I have several (bash) scripts that do small (related) tasks like cd'ing to a github project and running git status. I also have some aliases in my .bash_profile. Not exactly what you're asking for, but related.
    – keyser
    Jul 28, 2013 at 22:55

10 Answers 10

25

Here is the way to deal with complex paths to cd frequently.

mkdir ~/Links
ln -s /complexpath/there ~/Links

this will create the symbolic link

~/Links/there

To use this new Links directory efficiently, if your login shell is bash just include this command in your ~/.profile (which is used by all Bourne style shells bash, ksh, zsh…):

CDPATH=~/Links:.
export CDPATH

This means that for every cd, your shell will look within 2 directories to find a localpathname:

~/Links
.

You won't need to restart you session to test it, just run:

. ~/.profile

which means just read my ~/.profile. (You won't have to put it in your ~/.bashrc since there is no need to repeat the same variable initialization when you don't change it.)

Once you have tested it, you will be able to restart your session ;).

From now on, everytime you will want to go to there you will have to type:

cd there
2
  • This is the most elegant general solution I've seen. I typically make aliases for common destinations (like aliasing cdora to cd $ORACLE_HOME and such…)
    – bmike
    Jul 26, 2013 at 21:42
  • Would anyone be interested by a simple equivalent through the Finder interface?
    – dan
    Aug 10, 2013 at 10:22
11

Defining an alias is a very easy way to have a short abbreviation for a longer command. Once you have your aliases right, you could create a section in your .bashrc file (or other startup file) so that you don't have to re-type these aliases for each new shell you start:

in .bashrc:

alias ws4='ssh qsv-rhws4'
alias httpdlog='cd /var/log/httpd/'
alias EC2='ssh -i BMTestEC2.pem [email protected]'

etc.

Log out and back in (or just run . ~/.bashrc) and now you can just type httpdlog and it performs the cd. Or ws4 and it actually performs the ssh command etc.

This removes the need to create (a large amount of) empty links that will cause your home directory too look like a mess.

11

Try using either autojump or z.

After installing either of the two, you just cd around like normal and they learn your most visited directories and allows you to go to them by only typing a part of the path:

$ cd /very/long/path/foobar
$ cd
$ z foobar
$ echo $PWD
/very/long/path/foobar

I prefer z of the two.

1
  • don't know about autojump but 'z' script rocks!
    – Kris
    Aug 1, 2013 at 5:57
7

I highly recommend fasd which is similar to autojump and z but better IMHO. From the website:

Fasd (pronounced similar to "fast") is a command-line productivity booster. Fasd offers quick access to files and directories for POSIX shells. It is inspired by tools like autojump, z and v. Fasd keeps track of files and directories you have accessed, so that you can quickly reference them in the command line.

The name fasd comes from the default suggested aliases f(files), a(files/directories), s(show/search/select), d(directories).

Fasd ranks files and directories by "frecency," that is, by both "frequency" and "recency." The term "frecency" was first coined by Mozilla and used in Firefox (link).

4

Alternatively you could create an alias. Throw the following into something like .bashrc or .aliases

alias shortcut='cd ~/path/to/deep/deep/deep/directory/'

Then make sure that whatever file it is in is sourced. My aliases are in .bashrc, which I source with this line in .bash_profile: source ~/.bashrc

2

Adding convenience symlinks is fine for a special directory or two, but very quickly it gets to be a pain to manage. A far better solution is to use the environment variable CDPATH, available on bash and other shells for just this purpose.

CDPATH is a colon-separated list (like PATH) of places to look for a directory name. It can contain both absolute and relative pathnames. I have mine set to something like export CDPATH=".:..:$HOME/work/repos:$HOME/projects:<etc>".

With this set up, I can type cd pdftools from anywhere and it will take me to ~/projects/pdftools. (With a symlink, I'd have to type cd ~/pdftools if I wasn't in my HOME directory.) I can also shortcut into subdirectories: cd pdftools/bin.

The .. in my CDPATH lets me type cd sisterdir instead of cd ../sisterdir, etc. As you can expect, when I create new directories anywhere in CDPATH they'll automatically be found by cd-- no need to create new symlinks or aliases.

This feature gets even nicer if you use the bash_completion file distributed, e.g., with Debian systems. It didn't come with my OS X box, so I fetched a copy and it works fine. (Get it with brew, or just download it manually from here.) This file defines enhancements to commandline completion, so that I can type cd pdf<TAB> and the pdftools directory is found and expanded from anywhere on CDPATH.

1

Sure: Create a symbolic link

ln -s /some/really/long/path/to/somewhere/ ~/shortcut
cd ~/shortcut
1

I like using bashmarks to easily create "bookmarks to directories" on the fly.

See the readme at: https://github.com/huyng/bashmarks

Note: you'll need git installed in order to follow bashmark's three-step installation process: http://git-scm.com/download/mac

Good luck!

1

Alternatively you can also try installing fish shell.

http://fishshell.com/

This has excellent prediction for commands. It also intelligently lets you type case insensitive without any screw up.

Downside of fish is that it has its own scripting language and they are not bash compatible.

1

All I do is type "cd " and then drag the folder from Finder into the Terminal which pastes in the full path. Perhaps not quite as elegant, but works for all folders vs a few presaved shortcuts?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .