5

I want to get the total size of a directory and all directories inside it.

I am using the command

du -h -c "$input_variable" | grep total

It prints the results and the word total. How do I get the results without the word total?

3 Answers3

8

You're passing exactly one file: "$input_variable", so you can get rid of grep by using -s instead of -c:

-s
Instead of the default output, report only the total sum for each of the specified files.

(source)

If I'm reading the documentation right, du should use a space character in its output to separate size from pathname. Common implementations use tab though. awk with its default field separator should be able to separate the first field in both cases:

du -hs "$input_variable" | awk '{print $1}'

Notes:

  • the solution does not rely on the word total, so localized du may also be supported;
  • -s is a portable option, while -c is not (and -h you used is not portable either).
3

With GNU du:

du -sh "$input_variable" | awk '{print $1}'
Cyrus
  • 5,751
2

Pipe it into sed: | sed 's/\ttotal//g'

S.S. Anne
  • 126
  • 7