Last active
February 25, 2016 20:21
-
-
Save ctataryn/ed27be41e7679c26e2e3 to your computer and use it in GitHub Desktop.
pcd - Parent Change Directory -- One of my favourite little .bashrc includes. Just source this file from .bashrc and then at a command prompt you can type "pcd <some parent dir>" and you'll be automatically back to the folder specified
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find_target(){ | |
## The target directory | |
_target=$1 | |
## Iterate through the parent directories | |
printf "%s\n" "$PWD" | sed -r 's#/#/\n#g' | | |
while read parent_dir; | |
do | |
## Check if the target directory exists under this | |
## aprent directory and if it does, print the | |
## target's path and break the loop. | |
_path="${_path}${parent_dir}" | |
[ -d "${_path}${_target}" ] && echo "${_path}${_target}" && break | |
done | |
} | |
## If the value passed as an argument to | |
## this function exists as a subdirectory of | |
## any of the current directory's parents, | |
## cd into it. Else, fail silently. | |
pcd(){ | |
cd "$(find_target $1)" 2>/dev/null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment