我想获取当前级别的文件夹列表(不包括其子文件夹),并简单地打印文件夹名称和文件夹中文件的数量(如果可能,最好过滤为 *.jpg)。

这在标准 bash shell 中可能吗? ls -l 打印除文件计数之外的所有内容:)

有帮助吗?

解决方案

我想出了这个:

find -maxdepth 1 -type d | while read dir; do 
    count=$(find "$dir" -maxdepth 1 -iname \*.jpg | wc -l)
    echo "$dir ; $count"
done

放下第二个 -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

然而,这要慢得多,因为它必须读取部分文件并最终解释它们包含的内容(如果幸运的话,它会在文件开头找到一个神奇的 id)。这 -mindepth 1 阻止它打印 . (当前目录)作为它搜索的另一个目录。

其他提示

在我已经弄清楚我自己的类似脚本之后,我发现了这个问题。它似乎适合您的条件并且非常灵活,所以我想我会将其添加为答案。

优点:

  • 可以分组为 任何深度 (0 表示 ., 1 表示一级子目录等)
  • 打印漂亮的输出
  • 没有循环,只有一个 find 命令,因此在大目录上速度更快一些
  • 仍然可以调整以添加自定义过滤器(最大深度以使其非递归,文件名模式)

原始代码:

  find -P . -type f | rev | cut -d/ -f2- | rev | \
      cut -d/ -f1-2 | cut -d/ -f2- | sort | uniq -c

包装成一个函数并解释:

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
}

产生如下输出:

$ fc 0
1668 .

$ fc # depth of 1 is default
   6 .
   3 .ssh
  11 Desktop
  44 Downloads
1054 Music
 550 Pictures

当然,首先可以将数字传送到 sort:

$ fc | sort
   3 .ssh
   6 .
  11 Desktop
  44 Downloads
 550 Pictures
1054 Music

我的从命令行输入速度更快。:)

其他建议比以下建议有真正的优势吗?

find -name '*.jpg' | wc -l               # recursive


find -maxdepth 1 -name '*.jpg' | wc -l   # current directory only
#!/bin/bash
for dir in `find . -type d | grep -v "\.$"`; do
echo $dir
ls $dir/*.jpg | wc -l
done;

您无需外部命令即可完成此操作:

for d in */; do 
  set -- "$d"*.jpg
  printf "%s: %d\n" "${d%/}" "$#"
done

或者你可以使用 awk (诺克 或者 /usr/xpg4/bin/awk索拉里斯):

printf "%s\n" */*jpg |
  awk -F\/ 'END { 
    for (d in _) 
      print d ":",_[d] 
      }
  { _[$1]++ }'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top