[Bash] grep command
The grep command searches for patterns within files and prints matching lines.
grep [options] [content] [file]
Search for a pattern in a file:
grep "pattern" filename.txt
Ignore case sensitivity:
grep -i "pattern" filename.txt
Search recursively in directories:
grep -r "partten" /path/to/directory
Show line numbers of matches:
grep -n "pattern" filename.txt
Example
vi exampletxt
Hello World
This is a sample file
Bash scripting is powerful
Learning grep, awk, and sed
Another line with the word World
Search file:
grep "World" example.txt
Hello World
Another line with the word World
grep -i "world" example.txt
Hello World
Another line with the word World
grep -n "line" example.txt
5:Another line with the word World
Search directory:
mkdir test_directory
echo "This is a sample text file" > test_directory/file1.txt
echo "Another sample text file" > test_directory/file2.txt
grep -ri "sample" test_directory # case insensitive
test_directory/file2.txt:Another sample text file
test_directory/file1.txt:This is a sample text file