Description: Quickly go back to a specific parent directory in bash instead of typing "cd ../../.." redundantly.<p>How to use:<p>If you are in this path /home/user/project/src/org/main/site/utils/file/reader/whatever and you want to go to site directory quickly, then just type: 'bd site'<p>In fact, You can simply type 'bd <starting few letters>' like 'bd s' or 'bd si'<p>If there are more than one directories with same name up in the hierarchy, bd will take you to the closest. (Not considering the immediate parent.)<p>Using bd within backticks (`bd <letter(s)>`) prints out the path without changing the current directory.<p>You can take advantage of that by combining `bd <letter(s)>` with other commands such as ls, ln, echo, zip, tar etc..<p>Check out the screenshot.
Two other simple tips without requiring a script:<p>"cd -" will jump back to the last directory you were at, regardless of where it is.<p>Add this to your .bashrc / .zshrc / whatever:<p>alias cdg='cd $(git rev-parse --show-cdup)'<p>And then type "cdg" to keep going up directories until you reach whichever folder contains your git repo. Useful when going from "/Users/philfreo/Projects/myproject/myproject/src/assets/js/views/" back up to "/Users/philfreo/Projects/myproject/"
Why not put something like this in your .bashrc or your .profile file instead?<p>function bd () {<p><pre><code> OLDPWD=`pwd`
NEWPWD=`echo $OLDPWD | sed 's|\(.*/'$1'[^/]*/\).*|\1|'`
index=`echo $NEWPWD | awk '{ print index($1,"/'$1'"); }'`
if [ $index -eq 0 ] ; then
echo "No such occurrence."
else
echo $NEWPWD
cd "$NEWPWD"
fi</code></pre>
}<p>That way you don't have to execute a separate shell script and source its output. Also note that the "export OLDPWD" wasn't necessary in your script, either. Why pollute the environment of future processes spawned by your shell?
This article covers more advanced functionality than what's built in to shells but don't forget:<p><pre><code> cd -
</code></pre>
That goes to previous dir. A utility that follows this convention but adds some more features would be nice. For instance, maybe typing 'cd -2' could take me back two spots in history, like a web browser.
I have these two aliases in my .zshrc file:<p><pre><code> alias ..="cd .."
alias ...="cd ../.."
</code></pre>
For moving around to other directories I use autojump (<a href="https://github.com/joelthelion/autojump" rel="nofollow">https://github.com/joelthelion/autojump</a>).
pushd and popd can be used maintain an easily rewindable history of places you have been. Use pushd to add the cwd to the stack and cd to another directory. Use popd to cd to the top dir on the stack and remove it.<p><pre><code> $ pwd
/dir1
$ pushd /some/other/dir
/some/other/dir /dir1
$ pwd
/some/other/dir
$ popd
/dir1
$ pwd
/dir1</code></pre>
<p><pre><code> wget -O /usr/bin/bd https:[snip]
chmod +rx /usr/bin/bd
</code></pre>
Really? Run wget as root to get a random script, and put it into every user's PATH and a directory that should only be used by the system? :-|
I have tried stuff like this in the past, but I find it creates too much of a dependence on special configuration. My keymapping and vimrc are already enough. Instead, I usually just do a `cd ..` followed by up/enter/up/enter, which is very easy to type quickly.
An interesting aside: I can't seem to right click that link in Safari (6.0.5). It seems to be the "../../.." in the tag content that does it, because it works if I edit it out in the inspector. Does anyone else run in to this?
I implemented a fairly sophisticated system for navigating directories based on pushd and popd. I also set up a system based on CDPATH to hop quickly to directories I go to frequently. I fell out of the habit of using them once my usage patterns changed, though, and, since I gave "cd -" a new meaning, sometimes it confuses me. It's hard to come up with an interface that's intuitive enough to be future-proof.
Simple bashrc function<p><pre><code> # go up X directories
up_dir() {
num_of_dirs=$1
for i in $(seq 1 $num_of_dirs)
do
cd ..
done
}
alias up=up_dir
</code></pre>
Usage:<p><pre><code> $ pwd
/home/user/svn/automation/puppet/trunk/modules/ossec/templates
$ up 4
$ pwd
/home/user/svn/automation/puppet</code></pre>
I've also found this extremely useful. It lets you mark directories with a keyword and jump directly to them regardless of where you are.
<a href="http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html" rel="nofollow">http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-f...</a>
Here's my, yet another, alternative to the many nice shell hacks out there: a bash/zsh "up" command that does "cd ..", or "up <n>" does "cd .." <i>n</i> times.<p><pre><code> function up () { if test $# = 1 ; then s=$( printf "%$1s" ); s=${s// /..\/}; cd $s ; else cd .. ; fi; }</code></pre>
Very useful! Thank you ver much! Although I'd like to add one suggestion and that is to surpress an 'echo' on successful navigation. I'd see my path twice right underneath of eachother, which just clutters up the screen.
Smart idea. Good job! Could you extend bd to jump to bookmarked directories. I often jump between /data... and ~/dir/containing/code/ -- would be useful to have one command help with both scenarios.
Shameless plug: This works similar to a script called 'up' which I am the author of: <a href="https://github.com/helpermethod/up" rel="nofollow">https://github.com/helpermethod/up</a>
For those using ZSH: <a href="http://stackoverflow.com/questions/3986760/cd-1-2-3-etc-in-z-shell" rel="nofollow">http://stackoverflow.com/questions/3986760/cd-1-2-3-etc-in-z...</a>
For zsh users, I created a similar command with auto-completion! No root access needed to install!<p><a href="https://github.com/Tarrasch/zsh-bd" rel="nofollow">https://github.com/Tarrasch/zsh-bd</a>
How about using<p>bind -x '"\C-h":"cd ../;ls"'<p>(putting it into .bashrc) and then using control-h as a command on the shell to move a directory upwards. very useful for me.
zsh can be set to automagically track directories (like using `pushd` for you when using `cd`), so all that's needed is `popd`. No scripting required.
In FreeBSD I use the following to change to an arbitrary directory when I'm unsure of the correct path:<p>`whereis -sq <dirname>`<p>No extra software necessary.