How can I grep hidden files?

GrepHidden FilesDotfiles

Grep Problem Overview


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?

Grep Solutions


Solution 1 - Grep

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.

Solution 2 - Grep

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 * .[^.]*

Solution 3 - Grep

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

Solution 4 - Grep

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".

Solution 5 - Grep

To prevent matching . and .. which are not hidden files, you can use grep with ls -A like in this example:

ls -A | grep "^\."

^\. states that the first character must be .

The -A or --almost-all option excludes the results . and .. so that only hidden files and directories are matched.

Solution 6 - Grep

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.

Solution 7 - Grep

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 {} \;

Solution 8 - Grep

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.

Solution 9 - Grep

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' {} \;

Solution 10 - Grep

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZomboView Question on Stackoverflow
Solution 1 - GrepbitmaskView Answer on Stackoverflow
Solution 2 - GrepinsanerView Answer on Stackoverflow
Solution 3 - GrepOlvatharView Answer on Stackoverflow
Solution 4 - GrepAlanView Answer on Stackoverflow
Solution 5 - GrepmchidView Answer on Stackoverflow
Solution 6 - GrepPrashantBView Answer on Stackoverflow
Solution 7 - GrepTyler ChristianView Answer on Stackoverflow
Solution 8 - Grepuser10369828View Answer on Stackoverflow
Solution 9 - Grepnikhilgoel1985View Answer on Stackoverflow
Solution 10 - GrepmYnDstrEAmView Answer on Stackoverflow