How can I grep hidden files?

前端 未结 10 1213
北荒
北荒 2020-12-07 14:27

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

grep does not include this folder if I run



        
相关标签:
10条回答
  • 2020-12-07 14:48

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

    0 讨论(0)
  • 2020-12-07 14:57

    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' {} \;
    
    0 讨论(0)
  • 2020-12-07 14:58

    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.

    0 讨论(0)
  • 2020-12-07 14:59

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

    0 讨论(0)
  • 2020-12-07 14:59

    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
    
    0 讨论(0)
  • 2020-12-07 15:02

    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 * .[^.]*
    
    0 讨论(0)
提交回复
热议问题