[英]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个文件夹,里面有几个文件,我感兴趣:
.solution
are in each folder, and .solution
的文件,以及 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.