Grep Command Example on how to search for lines in a specific file Grep Command Example on how to find a file with text patternġ. Grep Command Example on how to remove all frozen messages in Exim mail queue Grep Command Example on how to display the total number of connections on a specific port Grep Command Example on how to see which socket belongs to process ID Grep Command Example on how to list all Apache processes Grep Command Example on how to search for lines in the current directory Grep Command Example on how to search in all files for specific text recursively Grep Command Example on how to add line numbers to search results Grep Command Example on how to search for specific text in all files If you have any questions or feedback, feel free to leave a comment. The grep command allows you to exclude patterns and directories when searching files. To exclude multiple directories, enclose the excluded directories in curly brackets and separate them with commas with no spaces.įor example, to find files that contain the string ‘gnu’ in your Linux system excluding the proc, boot, and sys directories you would run: grep -r -exclude-dir= linuxize * Conclusion # Here is an example showing how to search for the string linuxize in all files inside the /etc, excluding the /etc/pki directory: grep -R -exclude-dir=pki linuxize /etc The path to the excluded directory is relative to the search directory. To exclude a directory from the search, use the -exclude-dir option. The main difference between -r or -R options is that when grep is invoked with uppercase R it will follow all symbolic links Sometimes when performing a recursive search with the -r or -R options, you may want to exclude specific directories from the search result. In the following example, the lines where the string games occur at the very beginning of a line are excluded: grep -v "^games" file.txtĪ command’s output can be filtered with grep through piping, and only the lines matching a given pattern will be printed on the terminal.įor example, to print out all running processes on your system except those running as user “root” you can filter the output of the psĬommand: ps -ef | grep -wv root Exclude Directories and Files # You can specify different possible matches that can be literal strings or expression sets. If you use the extended regular expression option -E, then the operator | should not be escaped, as shown below: grep -Ewv 'nologin|bash' /etc/passwd By default, grep interprets the pattern as a basic regular expression where the meta-characters such as | lose their special meaning, and you must use their backslashed versions. GNU grep supports three regular expression syntaxes, Basic, Extended, and Perl-compatible. The following example prints the lines that do not contain the strings nologin or bash: grep -wv 'nologin\|bash' /etc/passwd You can use the -e option as many times as you need.Īnother option to exclude multiple search patterns is to join the patterns using the OR operator |. To specify two or more search patterns, use the -e option: grep -wv -e nologin -e bash /etc/passwd If the search string includes spaces, you need to enclose it in single or double quotation marks. To ignore the case when searching, invoke grep with the -i option. This means that the uppercase and lowercase characters are treated as distinct. The -w option tells grep to return only those lines where the specified string is a whole word (enclosed by non-word characters).īy default, grep is case-sensitive.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |