06/15/2022 11:12 | Category: bash

Tags: grepfinddirnamecdscripts

cd to a directory after grepping a file

I have quite a few notes generated from my Zet-CLI. These notes are stored in a time-series format which gets annoying when I need to find a specific one, then maybe change a filename or delete a file.

To avoid going back and forth grepping, cd-ing into directories, then grepping again I've discovered a little trick.

Finding a file then cd-ing to it

To find a file and cd to the containing directory, I'm using this:

cd $(dirname $(find . | grep <my search string>))

This script does the following:

  1. find to find all files in my current directory recursively
  2. Pipe the results to grep to compare the filenames to my search string
  3. Return the results (should be 1 thing, hopefully) to dirname
  4. dirname then finds the containing directory of that file
  5. Return the containing directory to cd to take me there