grep - bradsorour/notes GitHub Wiki

Find anything with grep.

The grep command looks inside one or several files for the string, or text, you specify.

Its syntax is: grep options search_string file...

At its most basic, you tell grep what to look for and where, for example:

$ grep AppleTalk /etc/services

Here, you tell grep to look for AppleTalk in the services file located in the /etc directory. (This useful file contains a list of network port numbers for commonly used services.)

To find... Use this option Example Note
Text in subfolders -r $ grep -r Walden ~/Documents/* Finds "Walden" in any file in any subfolder of ~/Documents.
Whole words only -w $ grep -w live Finds only text "live"; does not find liver, lives, lived, etc.
-r -w $ grep -r -w error ./* Recursively finds text "error" (only) in all subfolders
Case-insensitive text -i $ grep -i pond Finds pond , POND , or Pond.
File names only -l $ grep -l Walden Finds files containing Walden, returns list of file names only.
Number of occurrences only -c $ grep -c Walden Returns names of files containing Walden and number of hits in each file.

Search for Multiple Strings

Using the pipe (|), a Unix redirection operator, you can tell grep to search for more than one string.

Say you want to find files containing both Walden and Pondon the same line. You'd use this command:

$ grep Walden * | grep Pond

The first part of the command looks for the word Walden in any files in the current directory, and the second runs another grep command on the results of the first command. Terminal displays only the final results of the two commands combined.

You could string together many grep commands, like this:

$ grep a /usr/share/dict/words | grep e | grep i | grep o | grep u

This command looks in a special dictionary file for words containing the lowercase letter a. It then looks for words containing e in the results, and so on, finally returning only those words that contain all five vowels.

Here's a common way to find the process ID of a program that's stuck so you can force-quit it from the command line: type

$ ps -ax | grep Finder

This command first gets a list of all processes running on your Mac, and then sifts through this list looking for lines containing the word Finder. Now you would type the command (where the process ID is the final argument) kill -9 390.

Add Regular Expressions

You can go much further using regular expressions, special combinations of characters that act as wild cards. Here are a few examples:

If you're not sure how to spell the word separate, for example (is that an a or an e?), run this command to check the special dictionary file hidden in your Mac's entrails:

$ grep ^sep.r.te /usr/share/dict/words

You'll get back a list of words that includes separate, separately, separately, separateness, and separates. Note the two special characters in the command: the caret (^) and the dot (.). The caret tells grep to search for the string at the beginning of a line, so the results don't include words like inseparate. The dot matches any character except a new line.

What if you want to find all the phone numbers in a specific file? Try this command, which will find phone numbers in the 555-1234 format:

$ grep [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9] phonebook.txt

Each of the[0-9] wild cards matches any character in the range specified in brackets. You can use ranges such as[1-3] to limit your search to specific strings. This works for letters, too:[a-n] matches any lowercase character from a to n.You can build your own range with sets of characters—for example, [aeiou] will match only vowels.

Recording console output

You can use the script command to record terminal output (http://man7.org/linux/man-pages/man1/script.1.html)

For a list of commands type:

$ man script

$ script output.txt
Script started, file is output.txt

$ exit
exit
Script done, file is output.txt

Find (and kill) process locking on port 8080

Find:

$ sudo lsof -i :8080 or lsof -nP -iTCP:8080 | grep LISTEN

Kill:

$ sudo kill -9 <PID>

Find errors in CCD logs

$ ./ccd compose logs | grep error

To view all processes

Type ps -ax at Terminal’s command prompt to list every process running

To find a specific process

Type ps -ax | grep <application name> for example ps -ax | grep Skype

To terminate (kill) a process

Type kill <PID> for example kill 40224

If there are issues with permissions, try the following:

sudo kill 40224 or sudo kill -9 40224

⚠️ **GitHub.com Fallback** ⚠️