Home > Software engineering >  How to count all the files using ls -l statements separated by && in a single line?
How to count all the files using ls -l statements separated by && in a single line?

Time:10-02

I'm tyring to count all the files from several ls -l statements for certain file types separated by the double amperand symbol like so:

ls -l *.xml && ls -l *.json && ls -l *.md 

The technique I've seen for a single file type or all files will simply count all the end of line characters it finds using an egrep command: egrep -c '^-'

Here is a link for a single ls -l command to find the count for all files : Link to a question about ls -l count using egrep -c command in stack overflow.

If I count several ls -l statements using a command on a single line using like 'ls -l' and for each file type, how do I count each statement's totals in Linux using sh or bash shell script?

I tried this and it doesn't seem to work:

ls -l *.xml && ls -l *.json && ls -l *.md | egrep -c '^-'

I also tried:

ls -l *.xml && ls -l *.json && ls -l *.md | grep -v /$ | wc -l

Unfortunately, it doesn't like the '&&' symbols that concatenate the results, and it also doesn't work with the '|' (pipe symbol) for some reason. The pipe symbol must not work with the '&&' symbol the way I'm using it.

CodePudding user response:

I'm not quite sure if I understood the objective correctly.

If you're wanting a total number of all three types combined:

ls *.xml *.json *.md | wc -l
  • Related