I have a common use case that I would like to write for a function: I often want cd in some directory to refer to some file.
My current workflow is as follows:
$ gem which rspec/core | xargs echo -n | pbcopy
$ cd *paste and delete end until direcory looks right*
<sub> Note: gem which rspec/coreprints something like "/Users/joshcheek/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.10.0/lib/rspec/core.rb" Sub>
I would like it to look like this:
$ gem which rspec/core | 2dir 3
What will cd me in "/Users/joshcheek/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.10.0" (passing the argument "3" tells it to remove "lib / rspec / core. rb "from the end)
This is the best I've got so far:
2dir() {
read dir
for i in $(seq 1 $1)
do
dir="${dir%/*}"
done
cd "$dir"
}
cd , . , , .