Linux make my life easier - find text within files
For me as a Linux user , the need to search for a specific word in one or multiple files arises quite often . But how do I find all files containing a specific text within the files ??
It's not complicated . I will use the following commands .
grep -rnw 'path' -e 'pattern'
- -r or -R is recursive,
- -n is line number, and
- -w stands to match the whole word.
- -l (lower-case L) can be added to just give the file name of matching files.
- -e is the pattern used during the search
Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:
- This will only search through multiple files extensions :
grep --include=\*.{txt,c,h} -rnw 'path' -e "pattern"
- This will exclude searching all the files ending with a single extension:
grep --exclude=\*.txt -rnw 'path' -e "pattern"
- For directories it's possible to exclude one or more directories using the --exclude-dir parameter. For example, this will exclude the directories dir1/, dir2/ and all of them matching *.dts/
grep --exclude-dir={dir1,dir2,*.dts} -rnw '/path/to/search/' -e "pattern"
As an example I want to find all the files with ‘.c’ and ‘.h’ extensions in '/home/mominux/LTE01R02A05_C_SDK_U/' folder that contain "enum_aud_player_state" string .?