0

I've got a directory that contains hundreds of sub-directories that all have dates as their names, and inside each of those sub-directories, there's a bunch of files I need to be searching through. The grep command I'm using right now is this:

grep -ER --exclude-dir=2017060200_it2 "PATTERN" *_it2/file00*.pjson

and the folder structure looks like this:

parcel/
    |--- 2015010100_it2/
        |--- file001.pjson
        |--- file002.pjson
        ...
        |--- file005.pjson
    |--- 2015010200_it2/
        |--- file001.pjson
        ...
    ...
    |--- 2017060200_it2/
        |--- file001.pjson
        |--- file002.pjson
        ...

I want my grep command to be searching in file00*.pjson for every single sub-directory EXCEPT the most recent one, in this case being 2017060200_it2. However, my --exclude-dir=2017060200 doesn't seem to be accomplishing this.

I'm using grep V2.20 on a CentOS 6.7 system, and when I use grep --help, I do get the option for using --exclude-dir, so I don't think this information is relevant.

Bonus: If you could show me how to get the current date and format it as yyyyMMdd00_it2 so that I could make this a cron job, that would be awesome, but I was planning on figuring out how to do that separately once I got this working.

0

Your --exclude-dir is correct, it's just that it affects the recursion - but you have explicitly specified all *_it2 directories in the file glob, and that comes before any recursion.

I suggest using find instead - something like (untested)

find . -name 2017060200_it2 -prune -name 'file00*.pjson' -execdir grep -E "PATTERN" {} \;

Bonus: date '+%Y%m%d00_it2'

  • Awesome, worked really well! Had to modify the command a little bit, but this did the job! Thank you so much. Bonus: The date part worked well too, thank you! – phdumaresq Jun 2 '17 at 14:41
  • On another note, I got this to work using grep --exclude instead of grep --exlude-dir... I'd thought --exclude was for files, but I guess it's just about the recursive behavior... – phdumaresq Jun 2 '17 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.