Searching for code snippets in a given directory

in Development


One thing that saved me lots of time and potential bugs is using grep for a given string in a given directory. Imagine you decided to rename a function and you now need to update every piece of code that calls it. No chance you will remember all places where you used it, and manually looking up through each file is not an option either. Instead, simply do:

grep -inFRe "literal code" /var/www/myproject

This searches for all occurrences of literal code in /var/www/myproject

  • -i: case insensitive search, so the input literal code will match actual code Literal Code and LiTeRAl CoDe;
  • -n: for every match, show not only file name but also line number where the match is;
  • -F: ensure input pattern is treated as a literal string;
  • -r: search recursively in subdirectories; changing to -R will follow symlinks too;
  • -e: explicitly specify the input pattern, for clarity.

See also: man grep

#debian #grep