Like I said, if you want to understand it, go read more about it, but the thing you should take away is that the grep command never sees the *.cpp. One of the things is does is expand glob patterns (things like *.txt or +.pdf). So, when you type the command grep -ir "xyz" *.cpp, before grep is run, the shell handles reading your command and does a few things. (In Windows, when you double click on an icon to launch a program, or open a folder to access a file, the program that you are running is explorer.exe and it is the Windows graphical shell). Suffice it say that a shell is just a program that is designed to let you navigate the filesystem on your computer and run other programs. The first program is your shell (and unless you've done something to customize things, you are probably usually the bash shell - if you've never heard of a shell or bash, that's where you should start looking, there are tons of good articles). When you type in the command grep -ir "xyz" *.cpp and hit enter, there are two programs that are involved in executing your command. I'll also try to provide a quick explanation. If you are interested in learning more about why grep -ir "xyz" *.cpp does not work the way you think it should, you should search for "shell globbing" ( here is a good first article on the subject). ![]() xargs should read from stdin (default behavior) and split its input on nulls (-0) and then call the grep command with the specified options (grep -i "xyz") on that list of files. That list of files found should be written to the stdin of the next command: xargs. The command above says the following: find all files (-type f) under the current directory (./) that match the name glob/wildcard "*.cpp" (-name "*.cpp") and then print them out delimited by a null (-print0). If you are sure that none of your files have spaces in their names, you can omit the -print0 argument to find and the -0 to xargs type f -name "*.cpp" -print0 | xargs -0 grep -i "xyz" OR if you are on some other Unix platform, you can use this: find. The command above says to search recursively starting in current directory ignoring case on the pattern and to only search in files that match the glob pattern "*.cpp". If you are using GNU grep, then you can use the following: grep -ir -include "*.cpp" "xyz". You can also use the -include option (see the example below) ![]() (In your case, you probably do not have any directories that match the pattern "*.cpp") You could explicitly specify them: grep -ir "xyz" *.cpp */*.cpp */*/*.cpp */*/*/*.cpp, etc. Grep will recurse through any directories you match with your glob pattern.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |