search
HomeComputer TutorialsComputer KnowledgeHow to use grep in Linux system?

How to use grep in Linux system?

In Linux, grep is a very common and important tool. It is a command that every professional operation and maintenance engineer must master, because it can quickly find and filter files in files. Content, so how to use grep in Linux system? The following is an introduction to common usage, let’s take a look.

 1. Basic usage

The grep command is mainly used to search for lines of a specified pattern in files. For example, to find the line containing "example" in the file file.txt, you can use the grep command.

 grep ‘example’file.txt

Grep will output all lines containing 'example'.

 2. Ignore case

grep is case-sensitive by default, but you can use the -i option to search regardless of case. For example, execute the following command to find lines containing 'example' in the file, regardless of case.

 grep -i “example” file.txt

 3. Regular expression search

 grep supports using regular expressions for *level searches. For example, to find lines starting with "example", you can use the regular expression anchor symbol "^":

 grep “^example” file.txt

This will output all lines starting with "example".

4. Reverse search:

Sometimes it is necessary to find lines that do not contain the specified pattern. You can use the -v option to perform a reverse search. For example, to find lines that do not contain "example", you can execute the following command:

 grep -v “example”file.txt

Grep will output all lines that do not contain "example".

 5. Count the number of matching lines

If you only care about the number of matching lines, you can use the -c option to count the number of matching lines. For example, to count the number of lines containing "example" in the file, you can execute the following command:

 grep -c “example”file.txt

Grep will output the number of matching lines.

 6. Recursive search

If you want to recursively search for files in a directory and its subdirectories, you can use the -r option. For example, to find lines containing "example" in the current directory and its subdirectories, you can execute the following command:

 grep -r “example”.

Grep will search all files recursively and output the lines containing "example".

The above is the detailed content of How to use grep in Linux system?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:每日运维. If there is any infringement, please contact admin@php.cn delete
How to Fix LSA Database Contains an Internal Inconsistency?How to Fix LSA Database Contains an Internal Inconsistency?May 07, 2025 am 12:50 AM

Is your network drive not accessible? This error occurs might because the LSA database contains an internal inconsistency. Do you have any solutions to get rid of it from your device? Get solutions from this post on php.cn Website now!

192.168.0.2 Admin Login & Change Password – Must-Know Things192.168.0.2 Admin Login & Change Password – Must-Know ThingsMay 07, 2025 am 12:49 AM

What does 192.168.0.2 stand for? If you want to configure the router, how to log in to the admin panel? Besides, how to change the default Wi-Fi password? To find details about this default router IP address, refer to this post from php.cn now.

192.168.11.1 IP Address – How to Log in and Change Password? - MiniTool192.168.11.1 IP Address – How to Log in and Change Password? - MiniToolMay 07, 2025 am 12:48 AM

Every device on the Internet needs a unique identifier called an IP address. This post from php.cn is about the 192.168.11.1 IP address. You can know what it is, how to log into it, and how to change the password. Keep on your reading.

Top-Secret Tips for Outlook Error There Is No Default Mail ClientTop-Secret Tips for Outlook Error There Is No Default Mail ClientMay 07, 2025 am 12:47 AM

What is the “There is no default mail client” error? What methods can be used when this error occurs? This article from php.cn provides you with several feasible and efficient ways worth trying.

What If You Can't Boot Windows 11 After Enabling Hyper-V?What If You Can't Boot Windows 11 After Enabling Hyper-V?May 07, 2025 am 12:46 AM

Why won’t Windows 11 boot after enabling Hyper-V? How to fix this issue? If you can’t boot Windows 11 after enabling Hyper-V, you come to the right place. From this post on php.cn, you can find some possible reasons and workarounds to solve it.

Fixed: A USB Stick Not Transferring FilesFixed: A USB Stick Not Transferring FilesMay 07, 2025 am 12:45 AM

I believe that all of you have transferred files from a USB drive to your computer. Have you ever been bothered by a USB stick not transferring files error? Do you know how to fix this error? If not, you can find answers in this php.cn post.

OneDrive Folder Not Showing Thumbnails? See How to Fix in 3 Ways! - MiniToolOneDrive Folder Not Showing Thumbnails? See How to Fix in 3 Ways! - MiniToolMay 07, 2025 am 12:44 AM

If you are using OneDrive in your Windows 11/10, you may run into thumbnails not showing in OneDrive folder. How to fix this annoying issue? Take it easy and multiple solutions will be introduced in this post by php.cn. Let’s go to see what you shoul

How to Check Disk Partitions: Exact Steps in This Pro GuideHow to Check Disk Partitions: Exact Steps in This Pro GuideMay 07, 2025 am 12:43 AM

Don’t know what partitions your disk has? Now you can learn how to check disk partitions in a few easy ways from this post on php.cn. Also, you can know how to recover deleted or lost partitions effectively.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor