Skip to content

Instantly share code, notes, and snippets.

@tribou
Created November 9, 2015 01:27
Show Gist options
  • Save tribou/f74b843de8d2f6d10c09 to your computer and use it in GitHub Desktop.
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
#!/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
}
@tanzeelrana
Copy link

useage ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment