JAGDEEP BISHT
JAGDEEP BISHT's Blog

JAGDEEP BISHT's Blog

Creating a bash function for searching in files

Photo by Mick Haupt on Unsplash

Creating a bash function for searching in files

JAGDEEP BISHT's photo
JAGDEEP BISHT
·Apr 5, 2022·

1 min read

I use Visual Studio Code to do the search in the current project. But often times i would want to search other projects in my workspace directory.

So usually the command i would use might look like this:

grep -R --include \*.js --exclude-dir={dist,node_modules} 'OAuth2Client' .

This will search all the files with js extension excluding dist and node_modules directories under current directory.

To save me time from typing this cumbersome command multiple times, I have created the following bash function:

search() {
  DEFAULT_EXT="js"
  FILETYPE="${2:-$DEFAULT_EXT}"
  grep -R --include \*.${FILETYPE} --exclude-dir={dist,node_modules} $1 .
}

So, to search within json files you can just do:

search allowSyntheticDefaultImports json

./server/tsconfig.json: "allowSyntheticDefaultImports": true,

Also, to search within js files you can do:

search OAuth2Client

./server/src/middlewares/google-auth.js:search OAuth2Client

 
Share this