Search code examples
stringbashcut

Getting the total size of a directory as a number with du


Using the command du, I would like to get the total size of a directory

Output of command du myfolder:

5454 kkkkk
666  aaaaa
3456788 total

I'm able to extract the last line, but not to remmove the string total:

du -c myfolder | grep total | cut -d ' ' -f 1

Results in:

3456788 total

Desired result

3456788

I would like to have all the command in one line.


Solution

  • That's probably because it's tab delimited (which is the default delimiter of cut):

    ~$ du -c foo | grep total | cut -f1
    4
    ~$ du -c foo | grep total | cut -d'   ' -f1
    4
    

    to insert a tab, use Ctrl+v, then TAB

    Alternatively, you could use awk to print the first field of the line ending with total:

    ~$ du -c foo | awk '/total$/{print $1}'
    4