Home  >  Article  >  Operation and Maintenance  >  How to check the contents of a file in Linux

How to check the contents of a file in Linux

WBOY
WBOYOriginal
2022-07-20 15:12:369666browse

In Linux, you can use the grep command to view the content of the file. This command is used to find strings that meet the conditions in the file. It can find files that contain the specified template style. When the parameter is set to "-r " means traversing the directory layer by layer to search, the syntax is "grep -r -l "query content" file directory".

How to check the contents of a file in Linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to check the content of a file in Linux

Find the content of a file

grep -r -l "查询内容" 文件目录

After getting the specific file name

Check the related content of a file

grep -rn "关键内容" 文件名

The Linux grep command is used to find strings that meet the conditions in the file.

The grep command is used to find files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. . If no file name is specified, or if - is given, the grep command reads data from the standard input device.

Syntax

grep [-abcEFGhHilLnqrsvVwxy][-A<显示行数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][--help][范本样式][文件或目录...]

grep common parameter description

  • -e: Use regular search

  • -i : Case-insensitive

  • -v: Find lines that do not contain the specified content

  • -w: Search by word

  • -c: Count the number of matches

  • -n: Display the line number

  • -r: Layer by layer Traverse the directory to find

  • -A: Display the matching line and the following lines, such as: -A3, which means display the matching line and the following 3 lines

  • -B: Display the matching line and the number of lines before it, such as: -B3, which means display the matching line and the first 3 lines

  • -C: Display the number of lines before and after the matching line, such as : -C3, means displaying 3 lines before and after the batch line

  • --color: The matched content is highlighted

  • --include : Specify matching file types

  • --exclude: Filter file types that do not need to be matched

Examples are as follows:

The following are two examples of text url and url1

How to check the contents of a file in Linux

#Multiple file query

How to check the contents of a file in Linux

#Find files containing jd and http Lines

How to check the contents of a file in Linux

# Lines that do not contain jd

How to check the contents of a file in Linux

## Recommended learning:

Linux video tutorial

The above is the detailed content of How to check the contents of a file in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn