122

I am searching through a Git repository and would like to include the .git folder.

grep does not include this folder if I run

grep -r search *

What would be a grep command to include this folder?

hlin117
  • 20,764
  • 31
  • 72
  • 93
Zombo
  • 1
  • 62
  • 391
  • 407

9 Answers9

168

Please refer to the solution at the end of this post as a better alternative to what you're doing.

You can explicitly include hidden files (a directory is also a file).

grep -r search * .[^.]*

The * will match all files except hidden ones and .[^.]* will match only hidden files without ... However this will fail if there are either no non-hidden files or no hidden files in a given directory. You could of course explicitly add .git instead of .*.

However, if you simply want to search in a given directory, do it like this:

grep -r search .

The . will match the current path, which will include both non-hidden and hidden files.

Community
  • 1
  • 1
bitmask
  • 32,434
  • 14
  • 99
  • 159
  • 4
    The first approach (grep -r search * .*) worked for me. The second approach (grep -r search .) did not find the string. I found similar results when omitting the "-r" and searching the top-level directory only. I'm using GNU grep 2.6.3. – Alan Feb 13 '14 at 16:25
  • @Alan: That's weird. I use this regularly. Did you try it on the same directory? – bitmask Feb 13 '14 at 17:55
  • Yes, I did. By the way, the results are the same whether I use tcsh or bash. It's a Debian 5 64-bit system, for what that's worth. – Alan Feb 13 '14 at 19:45
  • 32
    using `.*` will include the parent directory (because `..` matches) – sehe Jul 03 '14 at 08:26
  • 1
    What would be the correct command to grep the string `"func foobar("` in all `*.go` files, including files in a hidden subdirectory? – hlin117 Jul 27 '16 at 20:49
  • 1
    Is it safe to generalize about this solution as bahavior may vary between shells? – astletron Mar 20 '17 at 18:16
  • 7
    for the first version you should add --exclude-dir=.. to avoid searching in the parent folders – user762353 Nov 16 '17 at 08:08
  • Not sure what my particular problem is, but when grepping (or using ack) through .classpath files in a large directory tree, I only get matches for the top level .classpath, even with the -r flag. I have not seen this particular behavior before. Can get it using find exec grep, but odd. – ShaneK Mar 15 '22 at 20:04
17

I just ran into this problem, and based on @bitmask's answer, here is my simple modification to avoid the problem pointed out by @sehe:

grep -r search_string * .[^.]*
insaner
  • 1,641
  • 16
  • 28
5

Perhaps you will prefer to combine "grep" with the "find" command for a complete solution like:

find . -exec grep -Hn search {} \;

This command will search inside hidden files or directories for string "search" and list any files with a coincidence with this output format:

File path:Line number:line with coincidence

./foo/bar:42:search line
./foo/.bar:42:search line
./.foo/bar:42:search line
./.foo/.bar:42:search line
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Olvathar
  • 551
  • 3
  • 10
  • 1
    Pretty weird, but this is the only way I could recursively grep a large directory tree with a bunch of .classpath files. Using all of the solutions above, grep (and ack for that matter), only return matches against the top-level .classpath file. – ShaneK Mar 15 '22 at 20:02
2

You may want to use this approach, assuming you're searching the current directory (otherwise replace . with the desired directory):

find . -type f | xargs grep search

or if you just want to search at the top level (which is quicker to test if you're trying these out):

find . -type f -maxdepth 1 | xargs grep search

UPDATE: I modified the examples in response to Scott's comments. I also added "-type f".

Alan
  • 1,889
  • 2
  • 18
  • 30
  • 1
    (1) `~` is the user’s home directory.  The question was not about the user’s home directory, so the answer should not mention `~`. (2) In a `find` command, ``-name '*'`` is a no-op (i.e., it serves no purpose). – Scott - Слава Україні Jul 25 '19 at 14:40
2

All the other answers are better. This one might be easy to remember:

find . -type f | xargs grep search

It finds only files (including hidden) and greps each file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PrashantB
  • 309
  • 2
  • 4
  • 13
2

To search within ONLY all hidden files and directories from your current location:

find . -name ".*" -exec grep -rs search {} \;

ONLY all hidden files:

find . -name ".*" -type f -exec grep -s search {} \;

ONLY all hidden directories:

find . -name ".*" -type d -exec grep -rs search {} \;
Tyler Christian
  • 520
  • 7
  • 14
1

To find only within a certain folder you can use:

ls -al | grep " \."

It is a very simple command to list and pipe to grep.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

In addition to Tyler's suggestion, Here is the command to grep all files and folders recursively including hidden files

find . -name "*.*" -exec grep -li 'search' {} \;
Evgeny Minkevich
  • 2,319
  • 3
  • 28
  • 42
DEs
  • 73
  • 9
-1

You can also search for specific types of hidden files like so for hidden directory files:

grep -r --include=*.directory "search-string"

This may work better than some of the other options. The other options that worked can be too slow.

mYnDstrEAm
  • 751
  • 2
  • 8
  • 26
  • Just like described above. You need to cd into the directory first and read the part "specific types of hidden files". – mYnDstrEAm Jan 02 '21 at 17:18
  • I do not understand what `--include=*.directory` is supposed to do - it's not like all my directories end in `.directory`? – xeruf Jan 02 '21 at 17:45
  • No: those are hidden files in directories in Linux. Open a folder (which has an image set as a folder-icon for example) with your file explorer and show hidden files. There usually is a ".directory" file. – mYnDstrEAm Jan 02 '21 at 17:55
  • why the star then? – xeruf Jan 02 '21 at 18:06
  • also, the question is specifically about `.git`, or including hidden directories in general – xeruf Jan 02 '21 at 18:06