18

I am trying to find a line of code in a folder and using the terminal. I use this command, which I think should work:

MacBook-Pro:myWordpress myId$ grep "<?php get_template_part('loop', 'archives'); ?>" -R

where the folder I am inspecting is called "myWordpress". But I get this:

grep: warning: recursive search of stdin

I don't use much the terminal, so I am unsure as to how to do what I want. Any help appreciated. Thanks

David

2
  • If you want to search for pattern in all HTM/HTML files in current directory, use grep "pattern" *htm* Commented Sep 9, 2017 at 8:18
  • If you want to search for string in all files in current directory, use grep "string" * Commented Sep 9, 2017 at 8:31

4 Answers 4

27

You have to specify the directory too as the last argument:

grep -r -F "string to search" /path/to/dir

Or if you want to search in current directory, write . as the target directory:

grep -r -F "string to search" .

If you don't specify a directory, grep will try to search in the standard input, but recursive search does not make sense there - that's why you get that warning.

The manpage for grep says that:

Not all grep implementations support -r and among those that do, the behaviour with symlinks may differ.

In this case, you can try a different approach, with find and xargs:

 find /path/to/dir -name '*.*' -print0 | xargs -0r grep -F "string to search"

Again, you can write . as the directory parameter (after find) if you want to search in the current directory.

Edit: as @EdMorton pointed out, the -F option is needed for grep if you want to search for a simple text instead of a regular expression. I added it to my examples above as it seems you are trying to search for PHP snippets that may contain special characters, which would lead to a different output in regexp mode.

3
  • sorry, i forgot to mention, I am already in the directory I want to look in. I have tried grep -r "string to search" and it's coming back with the same grep: warning: recursive search of stdin
    – Paul
    Commented Sep 9, 2017 at 8:23
  • Did you try as I said, with . as the last argument?
    – juzraai
    Commented Sep 9, 2017 at 8:44
  • 1
    @EdMorton Thanks for pointing that out! I added it to my answer.
    – juzraai
    Commented Sep 9, 2017 at 11:55
5

For macOS this will be super useful and easy, and also it highlights the search result matches of text!

brew install ack 
ack "text"

enter image description here

1
  • 1
    This helped me. Thanks
    – anand
    Commented Jul 22, 2020 at 16:44
3

I would suggest giving a try to ripgrep

$ brew install ripgrep

Besides been faster it gives you multiple options, check the examples

Once you have it installed just need to do:

$ rg your-string 
1

Never use -r or -R with grep. It is a terrible idea, completely unnecessary (the UNIX tool to find files is named find!), and makes your code GNU-specific.

Best I can tell without any sample input/output, all you need is:

grep "<?php get_template_part('loop', 'archives'); ?>" *

or if you have files AND directories in your current directory but only want to search in the files:

find . -type f -maxdepth 1 -exec grep -H "<?php get_template_part('loop', 'archives'); ?>" {} +

or to search in all sub-directories:

find . -type f -exec grep -H "<?php get_template_part('loop', 'archives'); ?>" {} +

If that doesn't do what you want then provide the sample input/output in your question.

2
  • Thanks for this, it's got me a bit further. it comes back with this:grep: wp-admin: Is a directory grep: wp-content: Is a directory grep: wp-includes: Is a directory -i am basically trying to find some code inside a wordpress folder, so it's just coming back with the folders inside the folder of wordpress. I know the line is there somewhere but I have never used the terminal before. Thanks
    – Paul
    Commented Sep 9, 2017 at 14:14
  • What we're struggling with is whether you want to search in the current directory (they're directories in UNIX, not folders - that's a Windows term), or if you need to search in sub-directories in addition to or instead of the current directory. Again, edit your question to clarify and provide sample files you're trying to search in and some info about your directory hierarchy and needs. I've updated my answer with a couple more examples of scripts to address what you might be trying to do.
    – Ed Morton
    Commented Sep 9, 2017 at 15:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.