Menü schliessen
Created: February 21st 2014
Last updated: August 7th 2024
Categories: Linux
Author: Marcus Fleuti

Linux (bash) find specific file content in all files within a given directory

Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

1. Search for content within files using regular grep

find ./ -type f -exec grep -Hn "YourContent" {} \;

2. Search for content using the xargs command

find ./ -type f |xargs grep -Hn "abc"

General notes

  • In most situations/directories you won't need the parameter "-type f"
  • For case insensitive search just apply the parameter -i. Examples:
    find ./ -type f -exec grep -Hni "YourContent" {} \;
    find ./ -type f |xargs grep -Hni "abc"

Extended search script with proper reporting and statistics

Powerful File Search Script for System Administrators

The following Bash script is a powerful tool for system administrators who need to search for specific content across multiple files on a Unix-like system. Here's a breakdown of what it does, how it works, and why it's useful:

What the Script Does

  1. Searches for files matching a specified REGEX pattern
  2. Within those files, looks for a specific string or REGEX pattern
  3. Outputs the results, highlighting matches in green and non-matches in red
  4. Provides a summary of all files where the pattern was found

How It Works

  1. Takes two command-line arguments:
    • A REGEX pattern to find specific files
    • A string or REGEX to search for within those files
  2. Uses the locate command to find files matching the first pattern. Ensure that your "locate" database is up-to-date by executing the command updatedb. If the system cannot find locate, install locate if it's not yet installed with
    apt install slocate
  3. Loops through each file, using grep to search for the second pattern
  4. Outputs results in real-time, color-coded for easy reading
  5. Stores successful matches in an array for a final summary

Benefits

  • Efficient: Uses locate for fast file discovery
  • Flexible: Allows for complex file and content search patterns
  • User-friendly: Provides color-coded output and a clear summary
  • Time-saving: Automates what would be a tedious manual process

When to use it

This script is particularly useful when:

  • Searching for specific configuration settings across multiple files
  • Looking for security vulnerabilities in code repositories
  • Tracking down specific log entries across various log files
  • Auditing large codebases for deprecated functions or patterns

Usage Example

To search for all PHP files in the themes directory containing the word "contact":

./searchContentInFiles.sh ".*/hosts/.*/themes/.*\.php$" "contact"

Script code

Copy the code for this script and put it into a file like "searchContentInFiles.ch". Make the file executable with

chmod +x searchContentInFiles.ch
#! /bin/bash

if [ -z "$1" ] ; then
        echo "1. Parameter missing: REGEX string to search for certain files. Put the following to search for all: .*"
	echo "	 More complicated example: \".*/themes/.*/template-[ck]ontact\.php$\""
	echo "   If you want to search for 'specific-file.php' anywhere: .*specific-file.php"
        exit
fi

if [ -z "$2" ] ; then
        echo "2. Parameter missing: Regular string to search for within each file. Example: template-name"
        exit
fi

# ANSI color codes
RED="\033[1;31m"
GREEN="\033[1;32m"
NOCOLOR="\033[0m"

# Store the regex pattern
HAYSTACK_REGEX="$1"
NEEDLE_REGEX="$2"

# Generate the list of files
echo "Generating file list, please stand by..."
TAIL_FILE_LIST=$(locate --regex "${HAYSTACK_REGEX}")

# Initialize an array to keep track of found files
FOUND_FILES=()

# Loop through each file in the list
for file in $TAIL_FILE_LIST; do
    # Check if file exists and is a regular file
    if [[ -f "$file" ]]; then
        # Search for the regex pattern in the file and capture the output
        MATCH_FOUND_ON_LINE=$(grep -En -m1 "${NEEDLE_REGEX}" "$file" | cut -d: -f1)

        if [[ -n "${MATCH_FOUND_ON_LINE}" ]]; then
            # If pattern found, output the details in green
            echo -e "${GREEN}[ ${file} ] ... Found on line [${MATCH_FOUND_ON_LINE}]${NOCOLOR}"
            FOUND_FILES+=("[ ${file} ] ... Found on line [${MATCH_FOUND_ON_LINE}]")
        else
            # If pattern not found, indicate it in red
            echo -e "${RED}[ ${file} ] ... Not found${NOCOLOR}"
        fi
    fi
done

# Display summary of all found files
if [ ${#FOUND_FILES[@]} -eq 0 ]; then
	echo -e "\n\n${RED}No files found matching [ ${NEEDLE_REGEX} ]${NOCOLOR}"
else
	echo -e "\n\n\nSummary of found files when searching for the pattern `${NEEDLE_REGEX}`:"
	for file in "${FOUND_FILES[@]}"; do
	    echo -e "${GREEN}${file}${NOCOLOR}"
	done
fi

Sample Output

This script is a valuable addition to any system administrator's toolkit, offering a quick and efficient way to search through large file systems for specific content.