Grep command in Linux

Tram Ho

The grep command is used to search for text. It searches for certain files for lines containing matches that match given strings or words. This is one of the most useful commands on Linux and Unix-like systems.

Find the string in the file

Search for any line containing the word in the filename, this is the most basic command to use: grep "chuỗi" filename

Search is not case sensitive

Use the -i character before the string to look for:

Search all files in current directory and in all subdirectories

Use the -r character before the search string:

Finds and displays the total number of times the string appears in the file

Use the -c character in front of the desired string:

Displays the line number before the string to be searched

Use the -B character and the line number to display:

Displays the line number after the searched string

Use the -A character and the line number to display:

Combine many options

It is possible to combine many options in one search to get the desired results:

Combining grep and ps ux

Check if a program is running or not

Above are the basic grep commands in linux, it will be very useful in application development (Investigate the cause of server errors, user behavior, …) Happy coding

Share the news now

Source : Viblo