How to find text inside a file

The file is long. grep shows only the line where the word appears.

Comandu penguin with a magnifying glass over the word texto in a file

The line, not the whole file

The file is long and the word is in the middle. ls only shows the name. Opening everything buries the line you want among the others.

grep reads the file and returns only the lines where that piece appears. The rest stays out.

The word and the file

The command takes two pieces of information: the word and the file. grep erro app.log looks for erro inside app.log.

bash - comandu
aluno@desafio:~$ grep erro app.log
2026-10-25 19:02 erro ao abrir o banco
2026-10-25 19:04 erro de permissão

Every line that comes back contains the piece. erro also matches inside erros, because grep looks for the piece inside the line, not the word standing alone.

Solve

You ran grep erro app.log. What comes back in the terminal?

The line number

grep -n erro app.log puts the line number in front. -n does not change the search. It only marks where the line sits in the file.

bash - comandu
aluno@desafio:~$ grep -n erro app.log
12:2026-10-25 19:02 erro ao abrir o banco
18:2026-10-25 19:04 erro de permissão

The first answer is on line 12. The second is on line 18. You can open the file and go straight to that point.

Solve

You need to know which line the word is on. Which command asks for that?

Uppercase and lowercase

erro and ERRO are different pieces for grep. If the log writes the word in uppercase, a lowercase search comes back empty.

grep -i erro app.log ignores that difference. -i accepts erro, Erro, and ERRO in the same search.

bash - comandu
aluno@desafio:~$ grep -i erro app.log
2026-10-25 19:02 erro ao abrir o banco
2026-10-25 19:10 ERRO fatal no disco

Figure it out

The log has ERRO in uppercase and grep erro showed nothing. What changes the search?

The whole folder

The word may be in another file in the folder. grep -r erro logs repeats the search in logs and in what is inside. The file name goes in front of the line.

bash - comandu
aluno@desafio:~$ grep -r erro logs
logs/app.log:2026-10-25 19:02 erro ao abrir o banco
logs/nginx.log:erro de conexão

The first line came from logs/app.log. The second came from logs/nginx.log.

Silence and a missing file

If the word is not in the file, the terminal writes nothing. The search happened. No line matched.

If the file name is wrong, the answer changes. grep writes No such file or directory. It never looked for the word, because it did not find the file.

bash - comandu
aluno@desafio:~$ grep erro aplicacao.log
grep: aplicacao.log: No such file or directory

The real name was app.log. To check the folder before the search, see how to navigate folders.

Figure it out

The terminal wrote No such file or directory. What happened?

Categories

Files