Pcd – quickly “cd” interior components of your current dir
In the spirit of this submission:
https://news.ycombinator.com/item?id=10722647
here is something I use. Maybe they can work together?
pcd_guts()
{
local target=$1
local level=${2:-1}
local tail=""
local dir=$PWD
if [ $# -lt 1 ] ; then
printf "pcd: insufficient args\n"
return 1
fi
while [ $level -gt 0 ] ; do
tail=$(basename "$dir")/"$tail"
dir=$(dirname "$dir")
: $(( level-- ))
done
dir=$(dirname "$dir")
tail=${tail%/}
printf "%s" "\"$dir\"/$target/\"$tail\""
}
pcd()
{
local todir=$(pcd_guts "$@")
eval cd "$todir"
}
The concept:When we change the directory, one way to look at that we are rewriting the last path component of our current working directory: we can delete it (cd ..), add a new one (cd child) or replace it (cd ../sibling).
The pcd command lets you do a replace on the component other than the last. You can specify which by number, which defaults to 1.
a/b/c $ pcd x
a/x/c $
a/b/c $ pcd x 0
a/b/x $
a/b/c $ pcd x 2
x/b/c $
pcd is helpful if you are working in a directory tree with parallel structures. You're in "proj/parser/src" but need to be in "proj/iolib/src". Just "pcd iolib".