Why du or echo pipelining is not working?

删除回忆录丶 提交于 2019-12-04 06:48:20
FatalError

Using a pipe sends the output (stdout) of the first command, to stdin (input) of the child process (2nd command). The commands you mentioned don't take any input on stdin. This would work, for example, with cat (and by work, I mean work like cat run with no arguments, and just pass along the input you give it):

ls | cat

For your applications, this is where xargs comes in. It takes piped input and gives it as arguments to the command specified. So, you can make it work like:

ls | xargs du -sb

Beware that by default xargs will break its input on spaces, so if your filenames contain spaces this won't work as you want. So, in this particular case, this would be better:

du -sb *

Use command substitution, like this:

du -sb $(ls -d */)
$ find . -type d -maxdepth 1 -exec du -sb {} \;

or

$ ls -d */ | xargs du -sb
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!