user2380782 user2380782 - 9 months ago 50
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
    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_");
a=$(ls -1 "$folder"/*.solution | wc -l);
echo $folder has "${a}" files;

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


This simple script could work for you:-



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