[Linux](EN) Find contents or string in current or subfolder files using grep and its option
Find contents or string in current or subfolder files using grep and its option
Environment and Prerequisite
- Linux base system
- Bash shell(/bin/bash)
grep command
What is grep?
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
grep
: Print lines matching a pattern. It can be used with other commands and various options.
Find contents in all subfolder files using r and n option
Usage
- Use below command in folder or directory where you want to find
grep -rn [PATTERN]
Basic example
-r
: Find all subdirectory files recursively-n
: Print matching string with line number
$ grep -rn "abc def hij"
test/test_ascii.txt:2:abc def hij
Find many of patterns
-e
: You can find more than one pattern => OR
$ grep -rn -e "send" -e "test"
.gitignore:41:# Unit test / coverage reports
.gitignore:47:nosetests.xml
.gitignore:51:.pytest_cache/
smc_on_in_text:13:0 10 17 * * python /home/twpower/MailReminder/reminder.py >> /home/twpower/test.txt
smc_on_in_text:14:50 10 * * 3 python /home/twpower/MailReminder/reminder.py >> /home/twpower/test.txt
...
Exclude specific pattern
-v
: Exclude specific pattern
$ grep -rn -e "send" -e "test" | grep -v ".gitignore"
smc_on_in_text:13:0 10 17 * * python /home/twpower/MailReminder/reminder.py >> /home/twpower/test.txt
smc_on_in_text:14:50 10 * * 3 python /home/twpower/MailReminder/reminder.py >> /home/twpower/test.txt
reminder.py:6:def sendMail(sender_email, receiver_email, app_password ,msg):
reminder.py:9: smtp.login(sender_email, app_password)
...
Give specific directory
- You can give specific directories as arguments.
$ grep -rn -e "def sednMail" -e "with open" MailReminder ./test/neutron
MailReminder/reminder.py:19:with open('/home/twpower/MailReminder/account.json') as account_json_file:
./test/neutron/doc/source/contributor/internals/quality_of_service.rst:349:combination with openflow rules.
...