How can I grep hidden files?
Asked Answered
K

10

126

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?

Kasten answered 29/4, 2012 at 20:25 Comment(0)
N
171

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.

Norrie answered 29/4, 2012 at 20:28 Comment(8)
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.Quadric
@Alan: That's weird. I use this regularly. Did you try it on the same directory?Norrie
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.Quadric
using .* will include the parent directory (because .. matches)Schumann
What would be the correct command to grep the string "func foobar(" in all *.go files, including files in a hidden subdirectory?Ornery
Is it safe to generalize about this solution as bahavior may vary between shells?Melan
for the first version you should add --exclude-dir=.. to avoid searching in the parent foldersPhthalocyanine
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.Slippy
R
18

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 * .[^.]*
Rafi answered 31/10, 2016 at 21:54 Comment(3)
i do not get any results using this: sudo grep -r ANDROID_HOME * .[^.]* what am i doing wrong here?Avoidance
@Avoidance where are you running the search?Rafi
@Norrie was correct... this was a better answer, but they're now the same.Rutty
K
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
Kneepad answered 10/2, 2014 at 13:45 Comment(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.Slippy
Q
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".

Quadric answered 13/2, 2014 at 16:49 Comment(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).Lytta
E
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.

Earthiness answered 12/10, 2015 at 7:5 Comment(1)
This is the best solution for my caseCombo
S
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 {} \;
Simmonds answered 8/2, 2018 at 17:41 Comment(0)
T
2

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' {} \;
Tennies answered 7/8, 2019 at 0:57 Comment(1)
this answer would be improved by using -name "*" -type f instead, if you only want to search inside files. The original suggestion will miss any files that do not have a type extension. This change will still recurse into hidden directories, but additionally skip running grep against directory objects.Underclothes
E
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.

Embowed answered 16/9, 2018 at 5:6 Comment(0)
B
0

To search hidden files from the current folder non-recursively

find . -maxdepth 1 -name '.*' -type f -exec grep -ls 'search text' {} \;

To search hidden files from the current folder recursively

find . -name '.*' -type f -exec grep -ls 'search text' {} \;
Ballot answered 5/4 at 22:5 Comment(0)
B
-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.

Belinda answered 16/7, 2020 at 10:22 Comment(5)
Just like described above. You need to cd into the directory first and read the part "specific types of hidden files".Belinda
I do not understand what --include=*.directory is supposed to do - it's not like all my directories end in .directory?Nereidanereids
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.Belinda
why the star then?Nereidanereids
also, the question is specifically about .git, or including hidden directories in generalNereidanereids

© 2022 - 2024 — McMap. All rights reserved.