bash 查找pwd中所有目录的目录中的文件数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3075377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:13:57  来源:igfitidea点击:

Finding the number of files in a directory for all directories in pwd

linuxbashshellcommand-line

提问by Abs

I am trying to list all directories and place its number of files next to it.

我正在尝试列出所有目录并将其数量的文件放在它旁边。

I can find the total number of files ls -lR | grep .*.mp3 | wc -l. But how can I get an output like this:

我可以找到文件总数ls -lR | grep .*.mp3 | wc -l。但是我怎样才能得到这样的输出:

dir1 34 
dir2 15 
dir3 2 
...

I don't mind writing to a text file or CSV to get this information if its not possible to get it on screen.

如果无法在屏幕上显示此信息,我不介意写入文本文件或 CSV 以获取此信息。

Thank you all for any help on this.

感谢大家对此的任何帮助。

采纳答案by Jon

There's probably much better ways, but this seems to work.

可能有更好的方法,但这似乎有效。

Put this in a shell script:

把它放在一个shell脚本中:

#!/bin/sh
for f in *
do
  if [ -d "$f" ]
  then
      cd "$f"
      c=`ls -l *.mp3 2>/dev/null | wc -l`
      if test $c -gt 0
      then
          echo "$f $c"
      fi
      cd ..
  fi
done

回答by Peter Lyons

This seems to work assuming you are in a directory where some subdirectories may contain mp3 files. It omits the top level directory. It will list the directories in order by largest number of contained mp3 files.

假设您位于某些子目录可能包含 mp3 文件的目录中,这似乎有效。它省略了顶级目录。它将按包含的 mp3 文件的最大数量顺序列出目录。

find . -mindepth 2 -name \*.mp3 -print0| xargs -0 -n 1 dirname | sort | uniq -c | sort -r | awk '{print  "," }'

I updated this with print0 to handle filenames with spaces and other tricky characters and to print output suitable for CSV.

我用 print0 更新它以处理带有空格和其他棘手字符的文件名,并打印适合 CSV 的输出。

回答by Wrikken

find . -type f -iname '*.mp3' -printf "%h\n" | uniq -c

Or, if order (dir-> count instead of count-> dir) is really important to you:

或者,如果顺序(dir-> count 而不是 count-> dir)对您来说真的很重要:

find . -type f -iname '*.mp3' -printf "%h\n" | uniq -c | awk '{print " "}'

回答by Dimitre Radoulov

With Perl:

使用 Perl:

perl -MFile::Find -le'
  find { 
    wanted => sub {
      return unless /\.mp3$/i;
      ++$_{$File::Find::dir};
      }
    }, ".";
  print "$_,$_{$_}" for 
    sort { 
      $_{$b} <=> $_{$a} 
      } keys %_;
  '

回答by guzzo

Here's yet another way to even handle file names containing unusual (but legal) characters, such as newlines, ...:

这是另一种处理包含异常(但合法)字符的文件名的方法,例如换行符,...:

# count .mp3 files (using GNU find)
find . -xdev -type f -iname "*.mp3" -print0 | tr -dc '##代码##' | wc -c

# list directories with number of .mp3 files
find "$(pwd -P)" -xdev -depth -type d -exec bash -c '
  for ((i=1; i<=$#; i++ )); do
    d="${@:i:1}"
    mp3s="$(find "${d}" -xdev -type f -iname "*.mp3" -print0 | tr -dc "##代码##" | wc -c )"
    [[ $mp3s -gt 0 ]] && printf "%s\n" "${d}, ${mp3s// /}"
  done
' "'\0'" '{}' +