简体   繁体   English

以少于n的文件回显ls命令的输出

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

I have 400 folders with several files inside, I am interested in: 我有400个文件夹,里面有几个文件,我感兴趣:

  1. counting how many files with the extension .solution are in each folder, and 计算每个文件夹中有多少个扩展名为.solution的文件,以及
  2. then output only those folder have less than 440 elements 然后仅输出那些少于440个元素的文件夹

The point 1) is easy to get with the command: 使用命令很容易获得要点1):

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? 但是,有什么简单的方法可以只过滤少于440个元素的文件?

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

The script below 下面的脚本

counterfun(){
count=$(find "$1" -maxdepth 1 -type f -iname "*.solution" | wc -l)
(( count < 440 )) && echo "$1"
}
export -f counterfun
find /YOUR/BASE/FOLDER/ -maxdepth 1 -type d -iname "sol_cv_*" -exec bash -c 'counterfun "$1"' _ {} \;
#maxdepth 1 in both find above as you've confirmed no sub-folders

should do it 应该做

Avoid parsing ls command and use printf '%q\\n for counting files: 避免解析ls命令,并使用printf '%q\\n来计数文件:

for folder in *sol_cv_*/; do
    # if there are less than 440 elements then skip
    (( $(printf '%q\n' "$folder"/* | wc -l) < 440 )) && continue
    # otherwise print the count using safer printf '%q\n'
    echo "$folder has $(printf '%q\n' "$folder"*.solution | wc -l) files"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM