Finding files, tools and text patterns

Finding files

What if you are a scatterbrain like me and you can’t for the life of you remember where you stored your favorite recipe? Well, my friend, don’t despair, find comes to the rescue.

If you run find without any arguments or options, it will list all the files in the current directory and its subdirectories. In most cases you use find to search for a specific file by its name. The -name option lets you do this:
find path -name file.txt
Note that you have to provide a path to tell him where to search.

You can use a * (wildcard character) in the file name. The * represents any number of characters, e.g.
find path -name *.txt
will search for any .txt file in the path directory and its subdirectories.

Remember that Linux is case sensitive. This is also true for file names. In case you’re not sure whether you used a capital in the name of your favorite recipe, you can force find to do a case insensitive search:
find path -iname file.txt

Finding tools

To retrieve the location of a tool use which e.g.:
which bash
will show the path to the location of the bash tool.

Finding text patterns

If you want to find a word or a text pattern in a file, you can use grep (Global Regular Expression Print). For instance you want to quickly check how much sugar you need for making chocolate mousse:
grep sugar Chocomousse.md
The first argument is the word you want to search, the second argument is the file you want to search in. The command will return every line in the file that contains the word sugar.

Here also, you can use the * (wildcard character):
grep sugar* Chocomousse.md
In this case, the command will return every line in the file that contains the word sugar, but also every line that contains the words sugary, sugarcane, sugarberry…

To do a case insensitive search:
grep -i sugar Chocomousse.md

1 – Click to do exercise in a controlled environment before taking the quiz:
LinuxSurvival (find command, part 1 – stop after Exercise 45)

2 – Click to do exercise in a controlled environment before taking the quiz:
LinuxSurvival (copying Linux directories – stop after this Exercise)

3 – Practice a bit more on your own computer:
Open Replit, go to folder Documents, and search for the .md file with the recipe for the salad.

4 – Practice a bit more on your own computer:
Open Replit, go to folder Recipies, and check if the word sugar occurs in the combined file of the soup and the mussels.