Joh*_*itb 21
我想出了这个:
find -maxdepth 1 -type d | while read dir; do
count=$(find "$dir" -maxdepth 1 -iname \*.jpg | wc -l)
echo "$dir ; $count"
done
Run Code Online (Sandbox Code Playgroud)
-maxdepth 1如果考虑子目录,jpg文件的目录内的搜索应该是递归的,则删除第二个.请注意,这只考虑文件的名称.你可以重命名一个文件,隐藏它是一个jpg图片.您可以使用该file命令对内容进行猜测(现在,也可以递归搜索):
find -mindepth 1 -maxdepth 1 -type d | while read dir; do
count=$(find "$dir" -type f | xargs file -b --mime-type |
grep 'image/jpeg' | wc -l)
echo "$dir ; $count"
done
Run Code Online (Sandbox Code Playgroud)
然而,这要慢得多,因为它必须读取部分文件并最终解释它们包含的内容(如果幸运的话,它会在文件的开头找到一个神奇的id).在-mindepth 1从打印防止它.(当前目录)作为它搜索另一个目录.
Nic*_*ole 10
在我已经找到了自己类似的脚本后,我发现了这个问题.它似乎符合您的条件,并且非常灵活,所以我想我会将其添加为答案.
好处:
.,1表示第一级子目录,等等)find命令,所以它在大目录上要快一点原始代码:
find -P . -type f | rev | cut -d/ -f2- | rev | \
cut -d/ -f1-2 | cut -d/ -f2- | sort | uniq -c
Run Code Online (Sandbox Code Playgroud)
包含功能并解释:
fc() {
# Usage: fc [depth >= 0, default 1]
# 1. List all files, not following symlinks.
# (Add filters like -maxdepth 1 or -iname='*.jpg' here.)
# 2. Cut off filenames in bulk. Reverse and chop to the
# first / (remove filename). Reverse back.
# 3. Cut everything after the specified depth, so that each line
# contains only the relevant directory path
# 4. Cut off the preceeding '.' unless that's all there is.
# 5. Sort and group to unique lines with count.
find -P . -type f \
| rev | cut -d/ -f2- | rev \
| cut -d/ -f1-$((${1:-1}+1)) \
| cut -d/ -f2- \
| sort | uniq -c
}
Run Code Online (Sandbox Code Playgroud)
产生这样的输出:
$ fc 0
1668 .
$ fc # depth of 1 is default
6 .
3 .ssh
11 Desktop
44 Downloads
1054 Music
550 Pictures
Run Code Online (Sandbox Code Playgroud)
当然,首先它可以通过管道输送到sort:
$ fc | sort
3 .ssh
6 .
11 Desktop
44 Downloads
550 Pictures
1054 Music
Run Code Online (Sandbox Code Playgroud)
我可以更快地从命令行输入.:)
其他建议是否提供以下任何真正的优势?
find -name '*.jpg' | wc -l # recursive
find -maxdepth 1 -name '*.jpg' | wc -l # current directory only
Run Code Online (Sandbox Code Playgroud)