## More Grep

We already played with grep a bit in the last section, let's do a little bit more. First, let's learn what grep stands for: Global Regular Expression Print.

$cat animals | grep "dog"$ grep "dog" animals


Both of those do the same thing. We can also search an entire directory by using the "*" symbol, often known as a wildcard. This wildcard will open every file in a directory.

$grep "dog" *  As a note, you can also cat * in case you ever want to print every file in a directory. Many other commands use the wildcard operator. If you only want to know which files in a directory have the target word, use "-l". $ grep -l "dog" *


If you want to know the file name and line number, use "-n".

$grep -n "dog" *  If you only want to see the lines in the file which don't have the term you are greping for, use "-v". $ grep -v "dog" animals


This defaults to showing line numbers and file names, so if you only want to see the filenames, use "-l".

$grep -l "dog" animals  Play around with a few of the grep settings to see what else is possible, but don't worry about memorizing them, stick to cheatsheets. ## regex So far I've only shown you the exact match operator which uses parentheses, but grep has the ability to do many other forms of pattern matching described in the manpage. One of the most useful is regex. Regex stands for "Regular Expression" (hey remember that from the grep acronym?) Regular expressions are a technical way to define a particular search pattern, and grep is built around them. While we have only done "literal" matches so far, regex is crazy complicated and basically a programming language in itself. I'd love to try and teach you, but learning regex is a course in itself. Anytime you need to use it, pull up a helpful site like this regex tester and get your regex query working on some test data. I highly recommend Google and specifically Stack Overflow results for regex help, very often someone else has already done 75% of what you are trying to accomplish. ## find Alright, last command for the search section! While grep cared about searching through files, find only cares about file information! Here are some example uses of find. There are a ton of flags for this, so use man anytime you need to do look for something you don't know the flags for $ find directoryName -name secret
> find all files named secret in a folder named directoryName
$find directoryName -type f -name "*.txt" > find all .txt files. Note the "*" wildcard$ find directoryName -type f -perm 0777 -print
> find all files with the permission 0777. Don't worry too much about permissions yet.
$find directoryName -type f -name ".*" > find all secret files! Remember the "." in front of the filename hides it from a standard ls.$ find / -user dennis
> find all files from the root directory owned by the user dennis
> remember that / by itself searches from root (which is a whole lot of searching, so it takes a few minutes), while ./ only searches from current directory. Looks similar but very different results.
$find / -size 50M > find all files from root that are 50MB$ find / -mtime 5
> find all files modified in the last 5 days. Kind of useful from a forensics standpoint, though timestamps are unreliable!


This ain't everything but should be enough to help.

## Assignment

Questions:

1. Write a command that will find all .html files on a computer that are 12 bytes in size, owned by "jake".
2. From that command, write the regex that will only select files with a string in it that matches the format:
* any character, any number, case insensitive letter D, lowercase v, !.
* Ignore all "," and " "" in that description for your actual regex
* Example strings that would match this are:
* s3Dv!
* !0Dv!
* 42Dv!
3. Combine the last two questions to write a command that does everything required in the first question, and prints the line of any file that matches the regex.
3. What does alphanumeric mean?

Resources:



Respond in this format.

 Answers:

1.
2.
3.

Resources:

Pre-Questions:

Post-Questions:

Feedback: