6

I was just at a lecture, where I noticed the lecturer using a command (probably aliased) to jump to a specific folder.

Example:

~/code$ j sciproj
~/projects/sciproj2011/$

This looked quite slick, so I started wondering:

Is this a standard utility, and if so, what is the name?

I have two theories as to how it works:

  1. It can both create, delete and jump to aliases directly from the command-line in the style of the example, without having to set up aliases in a configuration file or script or whatnot manually.

  2. It searches the home directory for a folder matching the name and jumps to it.

The second option seems a bit slow, however, so the first would be preferred.

1

3 Answers 3

6

As an addition to sagarchalise's comment:

It's in natty: http://packages.ubuntu.com/natty/autojump

Should work in older releases as well.

2
2

How about this?

function j() { for dir in ~/projects/$@*; do if [ -d $dir ]; then cd "$dir"; fi; done ;}

It finds the nearest possible match. So say I have a dir listing like this:

~/projects/
~/projects/apples/
~/projects/apples-and-pears/
~/projects/cabbage/

j a will match both apples* dirs but as apples-and-pears is the later, it will cd into that last, and that's where you'll end up (the flaw in this function). j c will put you in ~/projects/cabbage/.

Bung that into a terminal to test it and when you're happy, just plonk it on the end of ~/.bashrc.

Edit: I've also been playing around making a find variant but it's not as good (slower and less accurate). Feel free to hack around with it though.

function j() { cd $(find ~/projects -maxdepth 1 -type d -name "$@*"); }
1
  • It's gonna be reeeaaaally slooooww...
    – ulidtko
    Feb 11, 2011 at 16:35
0

Usually you can use popd, pushd and dirs to jump between some folders. But in your case, it's some custom alias which you can define in your ~/.bash_profile file.

In example:

# Change dir via find
# Usage: cdf (dir)
cdf() {
  pushd $(find . -name $1)
}

And then cdf some_dir will jump into folder ignoring the sub-dirs.

Live example to search for exact folder names (ignore prompt):

~/$ cdf() { pushd $(find . -name $1); }
~/$ mkdir -p ab/cd/ef/gh/ij/kl
~/$ cdf kl
    ~/ab/cd/ef/gh/ij/kl ~/
~/$ pwd
     ~/ab/cd/ef/gh/ij/kl
kl$ popd # We're going back to our previous folder.
~/$ pwd
    ~/

To similar alias for folders containing a partial text:

cdf() { pushd $(find . -name \*$1\*); }

Example .bash_profile file.

You must log in to answer this question.

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