Difference between find and grepįor those just starting out on the Linux command line, it’s important to remember that find and grep are two commands with two very different functions, even though we use both to “find” something that the user specifies. We mention this because files compressed with gzip are very commonly tar archives. Zgrep also works on tar files, but only seems to go as far as telling you whether or not it was able to find a match.
#Grep options in linux with examples archive#
It allows you to search the files inside of a compressed archive without having to first decompress that archive, basically saving you an extra step or two. You can use zgrep to search compressed files for a pattern. You could use this information to kill a running process or troubleshoot issues with the services running on your system. This is similar in function to just piping the output of the ‘ps’ command to grep. $ ls | grep DocumentsĪs you can see in the screenshot above, using the grep command saved us time by quickly isolating the word we searched for from the rest of the unnecessary output that the ls command produced. Let’s look in our home directory for a folder called Documents.Īnd now, let’s try checking the directory again, but this time using grep to check specifically for the Documents folder. That’s something you would use the “ls” command for.īut, to make this whole process of checking the directory’s contents even faster, you can pipe the output of the ls command to the grep command. Say that you need to check the contents of a directory to see if a certain file exists there. Let’s look at some really common examples. You can use it to search a file for a certain word or combination of words, or you can pipe the output of other Linux commands to grep, so grep can show you only the output that you need to see. To output a list of file names that do not contain a matching word or pattern, use the -L or -files-without-match flags.Grep is a command-line tool that Linux users use to search for strings of text. grep -l -r "student1" /var/logs Show file names without matches To output only file names that contain a matching word or pattern, use the -l or -files-with-matches flags in your command. Grep can output file names only, if you are more interested in finding files with a matching word or pattern inside of them. grep -I -r "student1" /opt/myapp Show only file names of matches If you find yourself wanting to match files from a directory or nested directory, you may want to exclude binary files in your search. grep -v "error" /var/log/apache2 Ignore Binary Files To perform inverse matches with grep use the -v flag.įor example, if you wanted to return all log entries except errors, you would use the following command. grep -c "error" /var/logs/apache2 Inverse Matches with GrepĪn inverse match returns results that do not match a word or pattern used with grep. Grep supports counting matches with the -c flag. You may be more interested in knowing the number of matches rather than outputting the matches to file or screen. grep 'warning|error' /var/log/apache2 Counting Matches with Grep Each word separated by that character will be matched separately.įor example, to find the words “warning” or “error” in the output of all of your Apache web server logs, you would run the following command. To search multiple words, use the | character as a delimination. Multiple words can be matched against using grep, which is very useful when you’re not sure what you are looking for or want to find multiple items. grep -r "error" /var/logs Grep Searching Multiple Words To perform a recursive search, where grep finds a string in files of all nested, child directories, you can use the -r or -R flags. The grep command will not recursively search directories by default. grep -i Student1 ~/class/students Recursive Directory Searches If do not know the case or want to find all cases, you add the -i flag. It search for using the exact case specified at the command-line. Grep will search each file and output the matching line from each. The grep command allows us to chain multiple files into our search by adding them at the end of the command.įor example, to find the world hello in the files file1, file2, file3 and file4, we would run the command as follows. You may find yourself wanting to search multiple files for a matching string. grep "hello" file.txt Multiple File Grep Search For example, to find the word hello in a file named file.txt we would run the following command. To find a word inside of a single file we specify the word we want matched and file to search. It is used to search a single file or an entire directory, including child directories, for a matching string. The grep command in Linux is a utility used to search any given input files for one or more matching words or patterns.
#Grep options in linux with examples how to#
In this tutorial, you will learn how to use the grep command in Linux, Unix and OSX, with examples of common use cases.