1

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.

3 Answers 3

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

du -sb gives a single sumarized result in bytes.

Sign up to request clarification or add additional context in comments.

2 Comments

This gives me the error, "bad math expression: operator expected at '1157680\n40...'"
@chris13524 edited the answer, a -s (sumarize) was missing.
1

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))

1 Comment

This is only going to work if the -h (human readable) flag finds a size it wants to represent in MBs. I have, for example: du -hs /u6 155G /u6
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.