7 Uses of grep Commands in Linux
A super tool for string searching
To say the grep
command is a useful tool for Linux administrators is still an understatement. The grep
command is a must-know command for all backend developers.
To feel how beautiful it is, let’s start from an interesting tech interview question:
For a software project, which is the easiest way to find the
main()
function in its source code?
There are many possible answers for this question, such as searching the “main” string directly on VSCode. But how to do it if you are handling it on a remote server without GUI?
Of course you need to enter the corresponding directory firstly. After that, a cool and neat answer is just one line of command:
grep -r main .
1. Searching a String in a File
As the starter shows, the grep
command, whose full name is Global Regular Expression Print, is used as a tool to search specific strings inside files.
The basic syntax of it includes 3 parts:
grep string file_name
For example, if I have a file named “test1.txt” and it contains the following 3 lines:
Yang is a man.
Who is him?
Yang is handsome.