85 points · by vigneshwaran · 13y ago · 49 comments
How to use:
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'
In fact, You can simply type 'bd <starting few letters>' like 'bd s' or 'bd si'
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.)
Using bd within backticks (`bd <letter(s)>`) prints out the path without changing the current directory.
You can take advantage of that by combining `bd <letter(s)>` with other commands such as ls, ln, echo, zip, tar etc..
Check out the screenshot.
"cd -" will jump back to the last directory you were at, regardless of where it is.
Add this to your .bashrc / .zshrc / whatever:
alias cdg='cd $(git rev-parse --show-cdup)'
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/"
In fact, I have a lot of .c<dir> aliases that take me directly to various frequented directories.
function bd () {
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
}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?
Removed the 'export'. I wrote out of habit. Thanks a lot for pointing that out.
cd -
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. alias ..="cd .."
alias ...="cd ../.."
For moving around to other directories I use autojump (https://github.com/joelthelion/autojump).alias cda='cd ../' alias cdaa='cd ../../' alias cdaaa='cd ../../../'
I guess I was influenced by Lisp at the time, with car, cdr, cdar, cddr, etc ;-)
You saw that naming scheme and thought "wow, that's sensible"?!
$ pwd
/dir1
$ pushd /some/other/dir
/some/other/dir /dir1
$ pwd
/some/other/dir
$ popd
/dir1
$ pwd
/dir1 wget -O /usr/bin/bd https:[snip]
chmod +rx /usr/bin/bd
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? :-|In fact, I use history so much, I increased HISTFILESIZE[0] from 1000 commands to 10000.
[0] http://osxdaily.com/2012/04/12/change-length-of-bash-command...
alias ..1='cd ..'
alias ..2="cd ../.."
alias ..3="cd ../../.."
alias ..4="cd ../../../.."
alias ..5="cd ../../../../.."
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..' cd ...
to go up two levels, or analogously for as many dots as you needed. It was incredibly useful and fast. Thanks for the tip, and I'm using these aliases from now on. # 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
Usage: $ pwd
/home/user/svn/automation/puppet/trunk/modules/ossec/templates
$ up 4
$ pwd
/home/user/svn/automation/puppet # Hah, I have almost the exact same thing in my bash_profile
up () {
TIMES=${1:-1};
for ((i=1; i<=$TIMES; i++));
do
cd ..
done
}
EDIT: Although now I'm looking at it and it looks like I made a strange choice with `TIMES=${1:-1};`. Oh well, I think it was my first bash script... it's funny to look back at the hack-y crap that your bash_profile accumulates over time. function up () { if test $# = 1 ; then s=$( printf "%$1s" ); s=${s// /..\/}; cd $s ; else cd .. ; fi; }I made 'bd' out of frustration to use 'cd ../..' all the time from "any" random directory. I can't bookmark all possible places. They are like slightly different tools.
I think it'll be awesome to use both jump and bd in the same system!
Please give 'bd' a try. Once you get used to 'bd', you'll use it frequently without thinking. :)
Anyways z always changes directory. Using bd within backticks will just print the path without changing the directory. We can use that with other commands like ls, zip etc. Example: "ls `bd <starting letters>`" would only print the contents without changing the directory.
You can use both 'jump' to jump between bookmarked directories and 'bd' to quickly go to a parent directory in the same shell.
bind -x '"\C-h":"cd ../;ls"'
(putting it into .bashrc) and then using control-h as a command on the shell to move a directory upwards. very useful for me.
`whereis -sq <dirname>`
No extra software necessary.