I'm trying to to grep through all the files within a certain directory. Is there a way to recursively list all directories & the files/directories within them via something like ls?
            Asked
            
        
        
            Active
            
        
            Viewed 361 times
        
    0
            
            
         
    
    
        Hays Stanford
        
- 93
- 1
- 8
- 
                    1Do you want to match filenames or file contents? – that other guy Jan 27 '20 at 19:16
- 
                    1http://mywiki.wooledge.org/ParsingLs; you want `find` rather than `ls` if `grep -r` doesn't do what you need. – tripleee Jan 27 '20 at 19:16
- 
                    Looking to match both file names & file contents @thatotherguy – Hays Stanford Jan 27 '20 at 19:18
- 
                    @tripleee `grep -r` should solve it. Thanks – Hays Stanford Jan 27 '20 at 19:18
- 
                    1FYI, `grep -r` will not match by filename, only file contents. To match by filename, you can use `find mydir -name '*foo*'` or more casually `find mydir | grep foo` – that other guy Jan 27 '20 at 19:20
1 Answers
3
            Try the -r option for grep. Here is a reference: https://linuxize.com/post/how-to-use-grep-command-to-search-files-in-linux/#recursive-search.
The search matches list out the path to the files that match your pattern as well. Should be easy to extract that out using something like cut
 
    
    
        synaptikon
        
- 699
- 1
- 8
- 16
- 
                    1Awesome. This answers the question better than the suggested duplicate post found here: https://stackoverflow.com/questions/1987926/how-do-i-grep-recursively – Hays Stanford Jan 27 '20 at 19:17