我的目录中有数百个目录videos
。我想计算每个子目录中的文件数量。对于每个子目录,我可以用来ls -1 | wc -l
计算其中的文件数量。但我无法手动重复该命令一百次。你能告诉我其他命令来计算每个子目录中的文件数量吗?非常感谢。
答案1
简单使用find
和wc
MusicDir='/home/MyUser/Music'
find "$MusicDir" -type d | while read D; do echo -e "$(find "$D" -type f | wc -l)\t$D"; done
## Example OUTPUT
206 ./AC-DC
9 ./AC-DC/1975 - High Voltage (Australia Only)
11 ./AC-DC/1975 - T.N.T (Australia Only)
10 ./AC-DC/1976 - Dirty Deeds Done Dirt Cheap (International version)
10 ./AC-DC/1976 - High Voltage (International version)
10 ./AC-DC/1977 - Let There Be Rock
11 ./AC-DC/1978 - Powerage
11 ./AC-DC/1979 - Highway To Hell
13 ./AC-DC/1980 - Back In Black
11 ./AC-DC/1981 - For Those About To Rock (We Salute You)
11 ./AC-DC/1983 - Flick Of The Switch
6 ./AC-DC/1984 - 74 Jailbreak
11 ./AC-DC/1985 - Fly On The Wall
11 ./AC-DC/1988 - Blow Up Your Video
14 ./AC-DC/1990 - The Razor's Edge
13 ./AC-DC/1995 - Ballbreaker
13 ./AC-DC/2000 - Stiff Upper Lip
16 ./AC-DC/2008 - Black Ice
12 ./AC-DC/2014 - Rock or Bust
输出也可以排序(第二列,alpha)
MusicDir='/home/MyUser/Music'
find "$MusicDir" -type d | while read D; do echo -e "$(find "$D" -type f | wc -l)\t$D"; done | sort -k2