Boyuan Wang
Boyuan Wang

Reputation: 57

How is `ls` work compare `*`?

The result of ls is difference between *? For example: ls | du -sh will only show one line for total size of current dir. du -sh * will show the size of each file or subDir in current dir.

why the result is not same?

Upvotes: 0

Views: 85

Answers (3)

Alfe
Alfe

Reputation: 59516

You might consider something like du $(ls) to actually pass the output of ls to du as parameters. But this is probably not a good idea because file names can be ugly as hell (contain spaces, even newline characters, etc.) and du will be only confused by this.

Consider combining find and du instead.

Upvotes: 0

Andreas Louv
Andreas Louv

Reputation: 47119

du -sh doesn't read from stdin. So whatever | du -sh is the same as du -sh.

du -sh * however is expanded to du -sh file1.txt file2.txt file3.txt where file1.txt, ... are files / directories in current directory.

When multiply files are specified for du -sh the output will display sum for each file, while du -sh will only show sum for current directory.

Upvotes: 1

Cobra_Fast
Cobra_Fast

Reputation: 16101

ls | du -sh only reports the size of . because du does not support reading directory names from standard input.
So, executing ls | du -sh wastes the ls and gives the same result as only running du -sh.

Upvotes: 3

Related Questions