我有400个文件夹里面有几个文件,我感兴趣的是:
.solution
的文件数量,以及点1)很容易得到命令:
for folder in $(ls -d */ | grep "sol_cv_");
do
a=$(ls -1 "$folder"/*.solution | wc -l);
echo $folder has "${a}" files;
done
但有没有简单的方法来过滤少于440个元素的文件?
答案 0 :(得分:1)
这个简单的脚本可以为您服务: -
#!/bin/bash
MAX=440
for folder in sol_cv_*; do
COUNT=$(find "$folder" -type f -name "*.solution" | wc -l)
((COUNT < MAX)) && echo "$folder"
done
答案 1 :(得分:1)
以下脚本
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
应该这样做
答案 2 :(得分:1)
避免解析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