The following code loops through ALL directories under current path to perform a count on compressed xml files:

for i in *xml.gz; do 
    echo "XML" "${PWD##*/}" "$i" $(gunzip -c $i | echo "scale=0; `wc -l`" | bc -l); 

I'm looking for a way to perform the loop say on the last 5 directories in order to prevent counting directories previously counted (and cut back execution time) The for loop allows specifying certain directories, but I still need to only count xml files. Can't find any syntax to do both.

  • 2
    IMHO the script look only in current directory: for i in *xml.gz – Romeo Ninov Mar 24 '17 at 10:30
  • 2
    That gunzip | echo "scale=0; wc -l" | bc -l looks like a Rube Goldberg machine, does it do something different from just gunzip | wc -l ? – ilkkachu Mar 24 '17 at 11:51

When you want to list last N files based on the time you can use this idea:

ls -tr

to get newest files on the bottom of the listing

tail -5

to get last 5 lines. So the code will be something like

for i in `ls -tr|tail -5`.....
| improve this answer | |
  • Well the actual script is:for i in *xml.gz; do echo "XML" "${PWD##*/}" "$i" $(gunzip -c $i | echo "scale=0; wc -l" | bc -l); done (note the backslash at position 1 is not displayed somehow in the post) – blom0344 Mar 24 '17 at 13:07
  • @blom0344, I still can't see the backslash you mention. Do you want to tell me the script is something like: for i in */xml.gz.....? Please edit your question – Romeo Ninov Mar 24 '17 at 13:19

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.