如何对`ls`返回的每个文件使用`wc`?

huangapple go评论71阅读模式
英文:

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)'

huangapple
  • 本文由 发表于 2023年3月7日 18:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定