grep for string in all files in directories with certain names

18,355

Solution 1

Yes. grep "a certain string" 200901*/*.csv.

Solution 2

You need something like:

grep NEEDLE 200901*/*.csv

(assuming your search string is NEEDLE of course - just change it to whatever you're actually looking for).

The bash shell is quite capable of expanding multi-level paths and file names.

That is, of course, only limited to the CSV files one directory down. If you want to search entire subtrees, you'll have to use the slightly mode complicated (and adaptable) find command.

Though, assuming you can set a limit on the depth, you could get away with something like (for three levels):

grep NEEDLE 200901*/*.csv 200901*/*/*.csv 200901*/*/*/*.csv 

Solution 3

Try this :

grep -lr "Pattern" 200901*/*.csv 

Solution 4

Try a combination of find to search for specific filename patterns and grep for finding the pattern:

find . -name "*.csv" -print -exec grep -n "NEEDLE" {} \; | grep -B1 "NEEDLE"
Share:
18,355
user788171
Author by

user788171

Updated on June 16, 2022

Comments

  • user788171
    user788171 almost 2 years

    I have csv files in directories with this structure:

    20090120/
    20090121/
    20090122/
    etc...
    

    I want to grep for a certain string in all of the csv files in these directories, but only for January 2009, e.g. 200901*/*.csv

    Is there a bash command line argument that can do this?

  • Phil Frost
    Phil Frost over 11 years
    Or, you could set globstar and use **