Skip to content

Instantly share code, notes, and snippets.

@sathishvj
Created January 12, 2019 06:26
Show Gist options
  • Save sathishvj/109ddf28250e57f5c7d67b19dae37380 to your computer and use it in GitHub Desktop.
Save sathishvj/109ddf28250e57f5c7d67b19dae37380 to your computer and use it in GitHub Desktop.
# go into dir with a specific file or dir in the subdirectory. Uses regex and substitues . with .*
# usage
# cdr a1.b1
# ... this matches all dirs .*a1.*b1.*
# cdr ^a1.b1$
# ... this matches all dirs ^./a1.*b1$
# go into dir with a specific file in the subdirectory. Uses regex and substitues . with .*
function cfr() {
pat=$1
#replace . with .* - so we can give multiple parts
pat=${pat//./.*}
if [[ $pat != ^* ]]; then #if it begins with a '^', leave it as is
pat=".*"$pat
else
after=`echo $pat | cut -c 2-`
pat="^./"$after
fi
if [[ $pat != *$ ]]; then #if it ends with a '$', leave it as is
pat=$pat".*"
fi
f=$(dirname $(find . -regex "$pat" -not -regex ".*git/.*" -not -regex ".*node_modules/.*" -not -regex ".*pkg/.*" -not -regex ".*google-cloud-sdk/.*" -not -regex ".*vendor/.*" -type f -print -quit 2>/dev/null | head -1) 2>/dev/null)
if [ -z "$f" ]; then
echo #'not found'
else
#echo "found in:" $f
cd "$f"
pwd
fi
}
# go into a specific dir anywhere below this. Uses regex and substitues . with .*
function cdr() {
#replace in between "."s with .*
pat=$1
#replace . with .* - so we can give multiple parts
pat=${pat//./.*}
if [[ $pat != ^* ]]; then #if it begins with a '^', leave it as is
pat=".*"$pat
else
after=`echo $pat | cut -c 2-`
pat="^./"$after
fi
if [[ $pat != *$ ]]; then #if it ends with a '$', leave it as is
pat=$pat".*"
fi
echo "pattern is $pat"
#d=$(find . -regex ".*$pat.*" -type d -print -quit 2>/dev/null | head -1)
d=$(find . -regex "$pat" -not -regex ".*git/.*" -not -regex ".*node_modules/.*" -not -regex ".*pkg/.*" -not -regex ".*google-cloud-sdk/.*" -not -regex ".*vendor/.*" -type d -print -quit 2>/dev/null | head -1)
if [ -z "$d" ]; then
echo #'not found'
else
#echo "going to $d"
cd "$d"
pwd
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment