Skip to content
Advertisement

Using bash, I want to print a number followed by sizes of 2 paths on one line. i.e. output of 3 commands on one line

Using bash, I want to print a number followed by sizes of 2 paths on one line. i.e. output of 3 commands on one line.

All the 3 items should be separated by “:”

echo -n "10001:"; du -sch /abc/def/* | grep 'total' | awk '{ print $1 }'; du -sch /ghi/jkl/* | grep 'total' | awk '{ print $1 }'

I am getting the output as –

10001:61M

:101M

But I want the output as –

10001:61M:101M

Advertisement

Answer

This should work for you. The two key elements added being the tr – d ‘n’ which effectively strips new line characters from the end of the output. As well as adding in the echo “:” to get the extra colon for formatting in there.

Hope this helps! Here’s a link to the docs for tr command. https://ss64.com/bash/tr.html

echo -n "10001:"; du -sch /abc/def/* | grep 'total' | awk '{ print $1 }' | tr -d 'n'; echo ":" | tr -d 'n'; du -sch /ghi/jkl/* | grep 'total' | awk '{ print $1 }'
Advertisement