bash search for pattern in all files that match search code example
Example: bash search for pattern in all files that match search
# Basic syntax:
find /directory -name 'file_pattern' -type f \
-exec grep 'string_pattern' {} \;
# Where
# - /directory is the top directory where find will recursively search
# for files in subdirectories
# -name 'file_pattern' is the pattern used to find files of interest
# -type f specifies searching for files
# -exec indicates what to execute when matching files are found
# - grep 'string_pattern' searches identified files for lines that
# contain the string_pattern
# - {} is where the files identified with find get passed in the -exec
# command
# More advanced example:
# This writes the first match to the string "MovieLength" along with the
# 2 characters before the match and 6 after for every file that ends in
# sts.xml in any subdirectory of /directory to output.txt
find /directory -name '*sts.xml' -type f \
-exec grep -m 1 -o -E '.{0,2}MovieLength.{0,6}' {} \; > output.txt