Some times we need a fast way to look for a phrase, expression or word inside few or many files.
One of the easiest way to test the grep comand is:
echo "You can't handle the truth !!" | grep 'n'
The result of the command is the same text with the n's highlighted in red.
To use the grep command to look inside a file we just need to specify the file and the word/expression to look:
grep 'text' my_file.txt
If within the file my_file.txt is wrote the word 'text, grep will show it through it output.
3 very useful flags for the grep command are:
- '-i': Ignore lowercase and uppercase (If you look for 'some' it will return 'SoME' as match)
- '-n': Shows the line number where the word was find in the file
- '-R': Recursive grep, it will open all files within the dir and it's subdirectories
If we execute:
grep -iRn 'text' /dir/
The grep command will open all the files in the folder and subfolders within the directory '/dir/', will look for the word 'text' inside all the files and will show through the output the matchs with the name of the file where it was found and the number of the line in this file.