Last active
August 21, 2018 10:29
-
-
Save sathishvj/8e4ee27f848adc1adbbe382904008ed0 to your computer and use it in GitHub Desktop.
bash script to go into a sub directory that matches a pattern
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
# go into dir with a specific file in the subdirectory | |
function cdf() { | |
f=$(dirname $(find . -iname "*$1*" -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" | |
fi | |
} | |
# go into a specific dir anywhere below this | |
function cdd() { | |
d=$(find . -iname "*$1*" -type d -print -quit 2>/dev/null | head -1) | |
if [ -z "$d" ]; then | |
echo #'not found' | |
else | |
cd "$d" | |
fi | |
} | |
# go into dir with a specific file in the subdirectory. Uses regex and substitues . with .* | |
function cdfr() { | |
pat=$1 | |
pat=${pat//./.*} | |
f=$(dirname $(find . -regex ".*$pat.*" -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" | |
fi | |
} | |
# go into a specific dir anywhere below this. Uses regex and substitues . with .* | |
function cddr() { | |
#replace in between "."s with .* | |
pat=$1 | |
pat=${pat//./.*} | |
#echo $pat | |
d=$(find . -regex ".*$pat.*" -type d -print -quit 2>/dev/null | head -1) | |
if [ -z "$d" ]; then | |
echo #'not found' | |
else | |
cd "$d" | |
fi | |
} | |
# 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 | |
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 | |
} | |
# i further customized it to my requirements working with golang and angular. | |
# also added capability to put ^ and $ at beginning and end. | |
# 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 | |
fi | |
if [[ $pat != *$ ]]; then #if it ends with a '$', leave it as is | |
pat=$pat".*" | |
fi | |
#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