By the end of this chapter, you should be able to:
findto search for files and folders
grepto search for patterns in a string or text
One of the most useful terminal commands is the
find command. When you know how to use it well, you can easily find files on your computer without using Spotlight, Alfred or any other GUI. Let's get started by learning how the syntax works.
To find a specific file in your current directory, you can simply type
find and the name of the file. (If you try to find a folder you will find all of the contents inside as well.) For example, if try typing the following command from your home directory:
You should see a list of all your Downloads in the terminal.
To find something with a bit more complexity, use the following pattern
cd into a folder called
views and try this pattern to find anything with the name
first.txt inside of the
find . -name "first.txt"
Now this is nice if we know exactly the name of the file we are looking for, but many times we need to use wildcard characters including
. The difference between these characters is as follows:
* - any number of characters
? - one character
 - any of the characters inside the brackets
Here are some more examples:
viewsfolder (assume we are inside the views folder) anything that ends with
find . -name "*.html"
viewsfolder (assume we are inside the views folder) anything that ends with a three letter file extension like
find . -name "*.???"
viewsfolder (assume we are inside the views folder) anything that starts with the letter
find . -name "[fts]*"
viewsfolder anything that has the text
mainsomewhere in the filename (this could be the beginning as well)
find . -name "*main*"
Another extremely useful tool for finding information that we've seen before is
find is for files and folders,
grep is excellent for searching for specific values in a string or in a text file. If you type
grep on its own, it's not that valuable because you need to make sure you pass a filename and text to it. You can also use
grep with piping and
We have already seen examples using
cat to find words like
cat people.txt | grep Elie to find if the word Elie exists in the
people.txt file. Let's use the file below which we will call
names.txt as an example:
Lisa Mark Elie Beth Tim Elizabeth Tom Matt Liza Janey Jane Shana
Let's add a little more onto our knowledge of
grep and introduce some flags.
-ifor case insensitive search
grep -i "elie" names.txt =>
-wfor full word search
grep -i "beth" names.txt
grep -iw "beth" names.txt =>
-Adisplay a certain number of lines after
grep -A 3 "Beth" names.txt
Beth Tim Elizabeth Tom
-Bdisplay a certain number of lines lines before
grep -B 3 "Beth" names.txt
Lisa Mark Elie Beth
-Cdisplay a certain number of lines lines around
grep -C 3 "Beth" names.txt
Lisa Mark Elie Beth Tim Elizabeth Tom
-vinvert pattern (you can think of this as anything NOT what you are searching for)
grep -v "Jane" names.txt
Lisa Mark Elie Beth Tim Elizabeth Tom Matt Liza Shana
grep -c "Jane" names.txt =>
-nshow line number
grep -ni "Jane" names.txt
There are many more flags with
grep; you can google around for more or look at
We previously saw wildcards with
find, so how can we use them with
grep? The key is to use regular expressions. Regular expressions are used to define patterns in a string of characters, which are then used to search a text for potential matches. Regular expressions are common and quite powerful: you can use them to check whether a user has submitted a properly formatted email address or phone number, for instance.
We will not go in depth with regular expressions here. There are a number of great interactive references online. For now, but let's just take a look at a couple examples of the syntax:
. - matches any character
Example: How many names have a full name that is four characters long?
grep -wc "...." names.txt =>
* - match zero or more of the preceding character or expression.
Example: How many names start with a capital T?
grep -wc "T.*" names.txt =>
 - any specific characters
Example: How many names start with a capital L, M, or E?
grep -wc "[LME].*" names.txt =>
[^] - do not match
Example How many names do not start with a capital T?
grep -wc "[^T].*" names.txt =>
When you're ready, move on to Intermediate Terminal Exercises