Chris Smith
Chris Smith

Reputation: 3012

Add number to variable size in Bash

I am trying to find the size of a folder, and add 1M to it. The 1M is just spacing that I need for other reasons.

Here is what I have tried:

echo $($(du -sh myFolder) + 1) # command not found: 170M
echo $(`du -sh myFolder` + 1)  # same as above

I want to be able to save this to a variable so I can use it in a dd call.

Upvotes: 1

Views: 190

Answers (3)

John Hascall
John Hascall

Reputation: 9416

Yet another way:

BLOCKSIZE=1048576 du -s myFolder | awk '{print $1+1}'

or to put in a variable:

mbs=$(BLOCKSIZE=1048576 du -s myFolder | awk '{print $1+1}')

Works on Linux, BSD, and OS X (possibly others).

Upvotes: 0

Joao Morais
Joao Morais

Reputation: 1925

echo $(($(du -sb myFolder | cut -f1)+1048576))

du -sb gives a single sumarized result in bytes.

Upvotes: 1

Hudson Santos
Hudson Santos

Reputation: 164

Alternatives if you can't use -b as Joao Morais suggested:

expr `du -hs myFolder | awk '{print $1}' | tr -d M` + 1

echo $((`du -hs myFolder | awk '{print $1}' | tr -d M` + 1))

Upvotes: 1

Related Questions