Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
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. –  ᴋᴇʏsᴇʀ Jul 28 '13 at 22:55
add comment

9 Answers

up vote 19 down vote accepted

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
share|improve this answer
    
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 '13 at 21:42
    
→ Mike: thanks ☺. –  daniel Azuelos Jul 26 '13 at 22:20
    
Would anyone be interested by a simple equivalent through the Finder interface? –  daniel Azuelos Aug 10 '13 at 10:22
add comment

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 ec2-user@xxxxxxxx.compute.amazonaws.com'

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.

share|improve this answer
add comment

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.

share|improve this answer
    
don't know about autojump but 'z' script rocks! –  Kris Aug 1 '13 at 5:57
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

Sure: Create a symbolic link

ln -s /some/really/long/path/to/somewhere/ ~/shortcut
cd ~/shortcut
share|improve this answer
add comment

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!

share|improve this answer
add comment

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.

share|improve this answer
add comment

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?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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