英文:
How can I use wc for each file returned by ls
问题
ls -d */ | xargs ls | wc -l
第二个尝试是
for i in ls -d */; do echo $i; ls $i | wc -l; done
但返回
name folder
wc result
name folder
wc result
name folder
wc result
而不是
folder_a 1000
folder_b 10
folder_c 10
...
英文:
I am trying to get the following information, given a folder with a lot of folders, get the name of each one with the total number of subfolders without recursivity, in order to do it fast.
To do this i am triying:
ls -d */ | xargs ls | wc -l
This of course sum all xargs ls, the problem is that I don't know how to split
100000
The second try is
for i in ls -d */; do echo $i; ls $i | wc -l; done
but returns
name folder
wc result
name folder
wc result
name folder
wc result
returned by that command instead of
folder_a 1000
folder_b 10
folder_c 10
...
is it possible to do this in one line or do I need to create a bash script?
Thanks
答案1
得分: 1
像这样:
printf '%s\n' */ |
while IFS= read -r dir; do
res=$(printf '%s\n' "$dir"* | wc -l)
echo "$res $dir"
done
英文:
Like this:
printf '%s\n' */ |
while IFS= read -r dir; do
res=$(printf '%s\n' "$dir"* | wc -l)
echo "$res $dir"
done
答案2
得分: 0
可能的解决方案,使用find|xargs
组合:
find * -type d -maxdepth 1 -print0 |
xargs -0 -I@ sh -c 'echo @ $(find "@" -maxdepth 1| wc -l)'
英文:
A possible solution, using find|xargs
combo:
find * -type d -maxdepth 1 -print0 |
xargs -0 -I@ sh -c 'echo @ $(find "@" -maxdepth 1| wc -l)'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论