I need to open automatically files and analyse their content.

I'm struggling to find a way to open them due to the fact that in each folder there is different files with different naming patterns.

I only need to open files with a specific pattern : "error_YYYYMMDD_xyz.json" "xyz being a number from 0 to 999.

I was using this algorithm before my need to only search specific files:

for folder in `ls -d $path`

 for file in `ls $folder`
 erreur=`jq ".tot_type" $folder/$file`  

 date=`echo $folder/$file | cut -d '_' -f2 | cut -c7-8 `

 if [ \( "$erreur" > 0 \) ]
      j$date=`expr ${j$date} + 1`


How can I make this work?

  • In order to only list them you can use for file in $path/$folder/error_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9]*.json; do youraction; done – Pierre-Alain TORET May 11 '17 at 13:55

I am not sure to understand exactly what you try to achieve.

From what I understood, you can refer to this answer and try to replace your second for loop with :

for file in `ls $folder | egrep 'error_[0-9]{8}_[0-9]{3}\.json'`

It will filter the output of your ls with only files corresponding to the pattern :


You can filter more precisely with a pattern which correspond only to dates starting by '20XX', month between '01' and '12' and day between '01' and '31' to restrict more the filter.

| improve this answer | |
  • Well that was exactly what I was missing! I tried doing "for file in $folder error_????????_???.json" but I was getting errors when the folder did not contain the specific file. Thanks a lot! – Matieu May 11 '17 at 13:58

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.