Location>code7788 >text

How do I find a specified log segment from a bunch of files?

Popularity:71 ℃/2024-08-09 16:44:31

contexts

This question examines the use of Linux commands, the find command and the grep command, which are widely used in linux systems and can often be used to find the contents of a given log at work.
Let's learn two commands today and answer that question.

Command Introduction

1. find command

find is often used to find files or directories on Linux systems, and the names of the files found are fed to standard output. For example, if you want
1.1 To find all log files in a directory with a .log extension, you can use the following
find /path -name ".log"
1.2 Search for a file named in the current directory
find . -name
1.3 Searching for documents of a specified size
find . -size +1M
1.4 Search all catalogs
find . -type d
Summary: find command, as the name suggests, is used to find, in linux system is usually the role of the query file, find the file and then for the contents of the file to do the search or filter.
Common parameter options are.

  • -name pattern: match by filename
  • -type type: match by file type
  • -size n[cwbkMG]: match based on file size
  • -exec command {} ;: Execute the specified command on the searched file.
  • -mtime n: match based on file modification time
  • -maxdepth levels: Limit the depth of the recursive search.
  • -mindepth levels: Set the minimum depth for recursive searching.
  • -delete: Deletes the searched file.

2、grep command

grep
grep is a common search tool that searches for a specified pattern in a file and outputs matching lines, here are some common uses
1.1 Searching for a specified string in a file
grep "pattern"
1.2 Searching for a specified string in multiple files
grep "pattern"
1.3 Recursive Search for a Specified String in a File
grep -r "pattern" /path/directory/
1.4 Finding rows beginning with 2024
grep "^2024"
1.5 Finding lines containing error and displaying line numbers
grep -n "error"
Summary: grep command, used for filtering, common use is to filter out the contents of the file containing the specified characters, or log segments, and so on.
Common parameter options are.

  • ignore capitals
    grep -i "pattern"
  • Show matching line numbers
    grep -n "pattern"
  • Show mismatched lines
    grep -v "pattern"
  • Show Matching Line Context
    grep -C 2 "pattern"
  • Display the content after the matching line
    grep -A 2 "pattern"
  • Display the content before the matching line
    grep -B 2 "pattern"
  • Using regular expressions
    grep -E "pattern"
  • Counting Matching Lines
    grep -c "pattern"

How to find the log segment with the specified characters in the log file in some files?

1, first of all, we find a file directory / path / directory under all the log files
find /path/directory -name "*.log"
2, then, we find the output in the contents of the grep operation, the output contains the contents of the pattern.
find /path/directory -type f -name "*.log" -exec grep "pattern" {} +
account for
/path/directory: is the path of the directory you want to search, you can replace it according to the actual situation.
-type f: Indicates that only ordinary files are searched.
-name "*.log": Indicates that only files with filenames ending in .log will be searched.
-exec grep "pattern" {} +: Execute grep "pattern" command to search in the found files.