The grep
utility is used to locates files by scanning their contents. You can
search a single file or a whole directory structure of files. By
default, grep prints the matching line of text. The basic syntax is:
grep [OPTIONS] PATTERN FILE
For example, if we want to search the file bobs_file.txt for the occurence of the word bob, we would use the following command:
Here is another example. If we want to search the directory /home/bob for each occurence of the word bob, we would use the following command:
In the picture above we can see that there are two occurrences of the word bob in the files inside the /home/bob directory. The -r option means that the subdirectories will also be searched. Note that the grep command has listed the filename and the line of the text with the keyword bob.
Grep is most commonly used in conjunction with commands that produce a lot of output, in order to sift through that output for the lines that are important to you. For example, suppose that we would like to see the members of the group cdrom. We can do this by typing the cat /etc/group command, which will display all the groups and their members on the system:
We can then scroll through the output and find the cdrom group and its members. Or, we could pipe the output to the grep command and display only the lines of text that contain the word cdrom:
Here is an another example of the usefulness of the grep command. We can use the ps -A command to display all processes on the system:
The ps -A command produces a lot of output. If we know the exact name of the process, we can pipe the output to the grep command:
In the picture above we can see that there are two instances of the top program running.
grep [OPTIONS] PATTERN FILE
For example, if we want to search the file bobs_file.txt for the occurence of the word bob, we would use the following command:
Here is another example. If we want to search the directory /home/bob for each occurence of the word bob, we would use the following command:
In the picture above we can see that there are two occurrences of the word bob in the files inside the /home/bob directory. The -r option means that the subdirectories will also be searched. Note that the grep command has listed the filename and the line of the text with the keyword bob.
Grep is most commonly used in conjunction with commands that produce a lot of output, in order to sift through that output for the lines that are important to you. For example, suppose that we would like to see the members of the group cdrom. We can do this by typing the cat /etc/group command, which will display all the groups and their members on the system:
We can then scroll through the output and find the cdrom group and its members. Or, we could pipe the output to the grep command and display only the lines of text that contain the word cdrom:
Here is an another example of the usefulness of the grep command. We can use the ps -A command to display all processes on the system:
The ps -A command produces a lot of output. If we know the exact name of the process, we can pipe the output to the grep command:
In the picture above we can see that there are two instances of the top program running.
NOTE – By default, grep searches the text in a case-sensitive way. You can do case-insensitive searches by providing the -i option.
Post a Comment