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:
1 2 | grep -i "chuỗi" filename |
Search all files in current directory and in all subdirectories
Use the -r character before the search string:
1 2 | grep -r "chuỗi" filename |
Finds and displays the total number of times the string appears in the file
Use the -c character in front of the desired string:
1 2 | grep -c "chuỗi" filename |
Displays the line number before the string to be searched
Use the -B character and the line number to display:
1 2 | grep -B2 "chuỗi" filename |
Displays the line number after the searched string
Use the -A character and the line number to display:
1 2 | grep -A2 "chuỗi" filename |
Combine many options
It is possible to combine many options in one search to get the desired results:
1 2 3 | grep -B2 -A2 "chuỗi" filename grep -B2 -A2 "chuỗi" filename | grep "chuỗi2" |
Combining grep and ps ux
Check if a program is running or not
1 2 | ps ux | grep ten chương trinh |
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