ScriptGeneralLinux

Check Linux file contents

Check whether a specified Linux file contains the text you expect.

See what you’re importing.

Read-only preview

Bash Timeout 100s Runs as Local system
#!/bin/bash

# This resource is provided as a convenience for Level users. We cannot 
# guarantee it will work in all environments. Please test before deploying 
# to your production environment. We welcome contributions to our community 
# library

# Level Library
# level.io/library/script-linux-monitor-file-contains

# Specify the file path to check
file_path="{{FilePath}}"

# Specify the string to search for in the file
search_string="{{SearchString}}"
# -----------------------------------------------------------------------------

# Check if the file exists
if [ -f "$file_path" ]; then
  if grep -q "$search_string" "$file_path"; then
    echo "SUCCESS: The string '$search_string' exists in the file."
  else
    echo "ERROR: The string '$search_string' does not exist in the file."
  fi
else
  echo "ERROR: File not found: $file_path"
fi

Before you run it.

  1. Test the script in a non-production environment before widespread deployment

  2. Use a script-based monitor in Level to trigger this script and alert if the string is missing

  3. Consider building an automation in Level to run this script on a schedule if continuous checks are needed

  4. Make sure the file path and string variables are accurate and up to date

  5. Keep an eye on script output for “ERROR” vs “SUCCESS” messages to pinpoint issues quickly

More about this resource.

What it does

This script checks whether a designated string exists within a specified file. It uses Level Script Variables for both the file path and search term, providing flexibility in various environments. If the file is found and the string exists, it returns a success message; otherwise, it reports an error.

You can easily integrate this script into a script-based monitor in Level to receive alerts whenever the string is missing. By pairing it with an automated remediation workflow, you can immediately address issues detected in your environment.

When to use it
  • Monitoring log files for critical error patterns
  • Verifying configuration files for essential directives
  • Ensuring required security settings are present in system files
  • Automating checks for newly added or changed file contents
The problem it solves

Ensuring critical log messages or specific strings are present in key Linux files can be tedious without an automated check. This script helps IT Professionals and MSPs proactively detect missing entries that might indicate configuration errors, security issues, or incomplete processes, reducing manual oversight and speeding up resolution times.

Frequently asked questions.

How do I run this script?

Simply configure it in Level and assign the correct file path and search string as Script Variables.

What permissions are required?

This script runs with System or Root level permissions, ensuring it can read files that might otherwise be restricted.

What if the file doesn’t exist?

The script will return an “ERROR: File not found” message. Double-check the file path for typos or changes.

Can I automate remediation if the string is missing?

Yes, pair this script with an automation in Level to perform corrective actions whenever the script reports an error.

Does this script work with any file type?

As long as the file is text-based and grep can search it, this script will work as intended. Test in your environment to confirm.

Ready when you are.

No credit card. No sales call. Just sign up and start managing.