user2380782 user2380782 - 4 months ago 9
Linux Question

echo the output of a ls command with less files than n

I have 400 folders with several files inside, I am interested in:


  1. counting how many files with the extension
    .solution
    are in each folder, and

  2. then output only those folder have less than 440 elements



The point 1) is easy to get with the command:

for folder in $(ls -d */ | grep "sol_cv_");
do
a=$(ls -1 "$folder"/*.solution | wc -l);
echo $folder has "${a}" files;
done


But is there any easy way to filter only the files with less than 440 elements?

Answer

This simple script could work for you:-

 #!/bin/bash

 MAX=440

 for folder in sol_cv_*; do
     COUNT=$(find "$folder" -type f -name "*.solution" | wc -l)
     ((COUNT < MAX)) && echo "$folder"
 done
Comments