$ wc /var/log/*.log List single/totals for many filesġ4076 100989 970777 total Sorting Output with sort $ wc /var/log/dmesg List counts for a single file By default, wc prints the number of lines, words, and bytes in a file: For example, the following command lists how many hits in an Apache log file come from a specific IP address: The wc command can be used to count the lines that it receives. There are times when you need to know the number of lines that match a search string. NOTE When piping the output of ps into grep, here's a trick to prevent the grep process from appearing in the grep results: $ grep -v " 200 " /var/log/httpd/access_log* Show lines without " 200 " To display all lines that do not match the string, add the -v option: To display only the name of the file that includes the search term, add the -1 option: $ grep -i selinux /var/log/messages Search file for selinux (any case) If you want to ignore case when you search messages, use the -i option: This example searches for the string sshd in the file auth. Use the -h option to disable the display of file names. $ grep -color -Rn VirtualHost /etc/httpd/conf*īy default, in a multifile search, the file name is displayed for each search result. To colorize the searched term in the search results, add the -color option: You can apply this technique to other files as well.Īdd line numbers (-n) to your grep command to find the exact lines where the search terms occur: Note that your system may not have any files with names starting with conf in the /etc/httpd directory, depending on what you have installed on your system. d directories for the string VirtualHost: The following command recursively searches files in the /etc/httpd/conf and /etc/ httpd/conf.
The grep command can also recursively search a few or a whole lot of files at the same time. The last command checks the kernel buffer ring for any ATA device information, such as hard disks and CD-ROM drives. Displaying bracketed commands that are output from the ps command is a way to see commands for which ps cannot display options. By searching access_log for 404 you can see requests to your web server for pages that were not found (these could be someone fishing to exploit your system, or a web page you moved or forgot to create). These command lines have some particular uses, beyond being examples of the grep command. $ dmesg | grep "ata\|"ata" Show ata kernel device information $ ps auwx | grep "\n Show bracketed commands # grep 404 /var/log/httpd/access_log Show lines containing 404 $ ps auwx | grep init Show init lines from ps output $ grep francois myfile.txt Show lines containing francois In fact, the phrase to grep has actually entered the computer jargon as a verb, just as to Google has entered the popular language. To escape the special meaning of the next character, use the \ (backslash) symbol.The grep command comes in handy when you need to perform more advanced string searches in a file. For example, find the lines that contain boat or “ coat, you could use the following pattern. Use (brackets) to match any single character enclosed in the brackets. For example, to match anything that begins with string1 then has two characters and ends with the text string2, you could use the following pattern.
(period) symbol to match any single character. In the following example, the text string will match only if it occurs at the very end of a line. Use the $ (dollar) symbol to match expression at the end of a line. In the following example, the text string will match only if it occurs at the very beginning of a line. Use the ^ (caret) symbol to match expression at the start of a line.
Grep -r string -include= filename -include= *.php -include= folder/* folder-name Regular Expression Search