Reputation: 1534
I am trying to find out all directories and the overall size starting with pattern int-*
For this I am using the below command
$sudo ls -ld int-* | grep ^d | wc -l
3339
$ sudo ls -ld int-* | grep ^d | du -sh
204G .
Are my commands correct ? Any other command combination to gather the above information ?
Upvotes: 0
Views: 2824
Reputation: 59416
No, your commands are not okay (though the first is not outright wrong).
Both parse the output of ls
which a dangerous thing to do as ls
is supposed to produce human-readable output and the format might change in the future (and indeed it has several times over the years, and it differs across various Unix flavors). So generally, parsing the output ls
is considered bad. See http://mywiki.wooledge.org/ParsingLs for details.
The second command also pipes this output into du
while du
isn't reading anything from stdin
. It just ignores this kind of input and will do the same as it would have done if being called without the pipe: du -sh
. This of course is not what you intended.
What you wanted can best be achieved in a proper fashion like this:
find -maxdepth 1 -type d -name 'int-*' -printf 'x\n' | wc -l
find -maxdepth 1 -type d -name 'int-*' -print0 | du --files0-from=- -c
Using the option --files0-from=-
the command du
does read NUL-separated file names from stdin
. -c
makes it print a total of all arguments.
Of course you can still add options -h
for human-readable sizes (4G etc.) and -s
if you do not want sizes for the subdirectories of your arguments.
If you want only the grand total, the best way is to chomp the output by piping it into tail -1
.
Upvotes: 0
Reputation: 11355
Simply du -shc ./int-*/
should give the grand total of all directories under the pattern int-*
. Add a trailing slash would do the trick for directories
AS
-s, report only the sum of the usage in the current directory, not for each directory therein contained
-h, is to obtain the results in human readable format
Upvotes: 4