Created
November 9, 2015 01:27
-
-
Save tribou/f74b843de8d2f6d10c09 to your computer and use it in GitHub Desktop.
Bash script to locate all occurrences of a string recursively within a directory with file and directory exclusions
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
#!/bin/bash -l | |
# Recursively greps files for pattern match | |
search() { | |
usage='Usage: search PATTERN [directory]' | |
search_dir='.' | |
# Return usage if 0 or more than 2 args are passed | |
if [ $# -eq 0 ] || [ $# -gt 2 ] | |
then | |
echo "$usage" | |
return 1 | |
fi | |
# Optional second arg | |
if [ $# -eq 2 ] | |
then | |
# Remove trailing slash if any | |
search_dir=`echo "$2" | sed 's/\/$//'` | |
fi | |
grep \ | |
-rnw "$search_dir" \ | |
-e "$1" \ | |
--exclude-dir={.git,.svn,node_modules} \ | |
--exclude=bundle*.js \ | |
--color | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
useage ?