为什么du或echo管道不起作用? [英] Why du or echo pipelining is not working?

查看:251
本文介绍了为什么du或echo管道不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对当前目录中的每个目录使用du命令.所以我正在尝试使用这样的代码:

I'm trying to use du command for every directory in the current one. So I'm trying to use code like this:

ls | du -sb

但是它没有按预期工作.它仅输出电流."的大小.目录,仅此而已. 回声也是一样

But its not working as expected. It outputs only size of current '.' directory and thats all. The same thing is with echo

ls | echo

输出空行.为什么会这样?

Outputs empty line. Why is this happening?

推荐答案

使用管道将第一个命令的输出( stdout )发送到 stdin (输入)子进程(第二个命令).您提到的命令在stdin上没有任何输入.例如,这可以与cat一起使用(并且通过工作,我的意思是像cat这样的工作没有参数运行,只是传递您给它的输入):

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

对于您的应用程序来说,这是xargs的来源.它接受管道输入并将其作为指定命令的参数.因此,您可以使其工作如下:

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

请注意,默认情况下,xargs会中断空格输入,因此,如果文件名包含空格,则该空格将无法正常工作.因此,在这种情况下,这样做会更好:

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 *

这篇关于为什么du或echo管道不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆