bashrc function that finds file child directory and changes directory

You are here

This bash function can find a file in a child directory and change directory to the file it is once found.

function cdf() {

    THEFILE=$1
    
    FILECOUNT=$(find  .  -name $THEFILE | wc -l)

   if [[ $FILECOUNT -gt 0 ]]; then
       CHANGEPATH=$(find . -name $THEFILE  -printf '%h\n');
        echo "Warning file was found $CHANGEPATH";
        cd  $CHANGEPATH;  
  
   else    
          echo "Warning: $THEFILE was not found"
   fi
    
}
alias cdf=cdf