LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to search text in all text files of all the sub-directories ? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-search-text-in-all-text-files-of-all-the-sub-directories-803245/)

moicpit 04-21-2010 05:19 AM

How to search text in all text files of all the sub-directories ?
 
Hi !

Currently, when I'm searching text in files of my PHP project, I use this line :
Code:

grep -r 'myTextToFind' *
But now, I would like to search only in ".lang" files.
How can I do that ?

Thanks !

-Pit

kilgoretrout 04-21-2010 05:44 AM

find / -name '*.lang' | grep -r 'myTextToFind' *

assuming root(/) is your topmost directory you want to search from.

moicpit 04-21-2010 06:30 AM

Thank you but it's not working : it search in all the files, not only in "*.lang" files.

barrie60s 04-21-2010 06:50 AM

grep "string to find" `find . -name "*.lang"`

The back quotes ` generate file names for file argument to grep

bs

LouRobytes 04-21-2010 07:09 AM

Code:

find / -name '*.lang' -print | xargs grep 'string to find'
You can be more specific than "/" with the start point.

Cheers, Lou

EDIT: If you don't want to run into permission problems, execute as root.

b0uncer 04-21-2010 07:19 AM

One could also use the exec option of find,
Code:

find / -type f -name '*.lang' -exec grep 'string to find' '{}' \;
which should work equally to any other command where find generates the list on which grep is run. The empty curly parenthesis means the result of find's work (i.e. filelist), and it usually has to be enclosed in quotation marks to prevent misinterpretations. The semicolon ends the command, and that too usually has to be escaped. You can also use a plus sign instead of a semicolon at the end, and it affects the behaviour, as told by the man page of find:

Quote:

-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca‐
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines. Only one instance of `{}'
is allowed within the command. The command is executed in the
starting directory.
You don't need to necessarily give the 'type -f', but I'm accustomed to telling find whether it should work on files only or both files and directories...

If you need to get the filenames where the matches were found as well, use the -H option for grep. Also, '*.lang' matches any file whose suffix is 'lang', but you only need to specify the dot if you (might) have files that end with 'lang' (i.e. 'thislang', 'thatlang' and so on, but not 'thislang.xxx' because it doesn't end with 'lang').

LouRobytes 04-21-2010 07:23 AM

Thanks bOuncer.

Lou

moicpit 04-21-2010 08:45 AM

OK, thanks for all this !

That helped me to have this command that does exactly what I wanted :
Code:

grep -H "nb" `find . -name "*.lang"`


All times are GMT -5. The time now is 11:53 AM.