Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why du or echo pipelining is not working?

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?

like image 655
Unicorn Avatar asked Feb 15 '12 17:02

Unicorn


People also ask

Why is DU command used?

The du command is a standard Linux/Unix command that allows a user to gain disk usage information quickly. It is best applied to specific directories and allows many variations for customizing the output to meet your needs.

What is the pipe command in Linux?

In Linux, the pipe command lets you sends the output of one command to another. Piping, as the term suggests, can redirect the standard output, input, or error of one process to another for further processing.


1 Answers

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 *
like image 128
FatalError Avatar answered Oct 05 '22 19:10

FatalError