Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I grep hidden files?

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?

like image 480
Zombo Avatar asked Apr 29 '12 20:04

Zombo


People also ask

How do I list hidden files?

Show Hidden Files From the Command LineThe ls command lists the contents of the current directory. The –a switch lists all files – including hidden files.

How do I see hidden files in Linux?

To toggle show/hide hidden files or folders use the keyboard shortcut Ctrl + H . in Linux and Unix systems, the files starting with . (a dot) are hidden files. To see them with the ls command, add -a or -A at your ls.

How do I use grep to search inside files?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

Which command is used to check hidden files?

Using the command line command dir /ah displays the files with the Hidden attribute.


2 Answers

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.

like image 140
bitmask Avatar answered Sep 20 '22 05:09

bitmask


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 * .[^.]* 
like image 33
insaner Avatar answered Sep 20 '22 05:09

insaner