Skip to content

Instantly share code, notes, and snippets.

@freejoe76
Last active October 25, 2017 21:48
Show Gist options
  • Save freejoe76/3374344 to your computer and use it in GitHub Desktop.
Save freejoe76/3374344 to your computer and use it in GitHub Desktop.
Search all files matching a pattern for a particular string
#!/bin/bash
# For the times when grep -r just doesn't cut it: Search all files matching a pattern for a particular string
# Example command (best when put in a folder that's on your path somewhere):
# searchall.bash -f *.php -s 'hoops'
# What arguments do we pass?
while [ "$1" != "" ]; do
case $1 in
-f | --file ) shift
file=$1
;;
-s | --search ) shift
search=$1
;;
-l | --less )
LESS=1
;;
esac
shift
done
find . -name "$file" -exec grep "$search" '{}' \; -print
# To search and replace across documents, do something like
# find . -name "index.html" -print0 | xargs -0 sed -i '' -e 's#http://assets.nydailynews#img src="//www.nydailynews#g'
find . -name "index.html" -exec sed -i '{}' -e 's#http://assets.nydailynews#img src="//www.nydailynews#g' \; -print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment