我想要一个 BASH 命令来列出目录的每个子目录中的文件数。
例如,在目录中/tmp有dir1, dir2, ... 我想看到:
`dir1` : x files
`dir2` : x files ...
Run Code Online (Sandbox Code Playgroud)
Tho*_*hor 42
假设您只需要文件的递归计数,而不是目录和其他类型,这样的事情应该可以工作:
find . -maxdepth 1 -mindepth 1 -type d | while read dir; do
printf "%-25.25s : " "$dir"
find "$dir" -type f | wc -l
done
Run Code Online (Sandbox Code Playgroud)
syn*_*ror 17
这项任务让我非常着迷,以至于我想自己找出解决方案。它甚至不需要 while 循环,并且执行速度可能更快。不用说,Thor 的努力帮助我详细了解事物。
所以这是我的:
find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'echo "{} : $(find "{}" -type f | wc -l)" file\(s\)' \;
Run Code Online (Sandbox Code Playgroud)
它看起来很谦虚是有原因的,因为它比看起来更强大。:-)
但是,如果您打算将其包含在您的.bash_aliases文件中,它必须如下所示:
alias somealias='find . -maxdepth 1 -mindepth 1 -type d -exec sh -c '\''echo "{} : $(find "{}" -type f | wc -l)" file\(s\)'\'' \;'
Run Code Online (Sandbox Code Playgroud)
请注意嵌套单引号的非常棘手的处理。不,不能对sh -c参数使用双引号。
小智 11
find . -type f | cut -d"/" -f2 | uniq -c
Run Code Online (Sandbox Code Playgroud)
列出当前文件夹中的文件夹和文件以及在下面找到的文件数。快速且有用的 IMO。(文件以计数 1 显示)。