

Though this feature isn't available on older implementations of grep, most up to date systems will have this feature. The grep command can be used recursively as well. There is no need for complicated find commands. If you need to search through multiple files like the above example, but the files are in separate directories. Searching through multiple files recursively Sometimes, when you need to search for “something” you need to search multiple files, this is as simple as giving grep multiple files to search. I use this frequently when writing bash scripts to check if something is true or false. One of my most common usages of grep is counting the number of times a search string is found. The above command will remove all files in the current directory except the ones with. While I've used -v with grep in many use cases one that pops up is performing multiple rm commands through a for loop or xargs. Normally grep will return the string that you are searching for, when given the -v flag grep will omit the searched string and return everything else.

The grep command is a powerful search tool, below are some examples of grep commands that I have found incredibly useful in daily tasks. The previous method requires both the cat and grep command to run which takes longer to run and uses more system resources (even if they are minor resources, it's less efficient).

Stop Doing This: $ cat file.log | grep "something"ĭo This More: $ grep "something" file.logĪside from saving yourself some typing, this method is preferred because you only have to read and search the file through one process. Because of this some Linux users just assume that grep can only be used with stdin it's ok, I was one of those too!īefore I continue with some grep tricks I want to clarify the basic grep usage. The grep command is a command that most Linux users learn early on, and many times they learn to use it via pipes (stdin).
