1

I am trying to use du -sk to get the size of two folders

dirsize_temp=`du -sk /folder/path/here`

and then an if statement to compare the sizes. The only issue is that when you run the command, it returns something like

123456         /folder/path/here

I am currently using

dirsize=${dirsize_temp%%/*}

to strip the "/folder/path/here" part, but I am having problems getting rid of the spaces. They do not seem to be actual spaces, so "${dirsize//\ /}" or "${dirsize// /}" does not work to remove them, but they still keep me from comparing two variables. Is there some escape character I am missing, or an easier way to get the size of two folders without extra stuff tacked on the end?

2
  • -h and -k are mutually exclusive. Pick one.
    – wfaulk
    May 9, 2012 at 21:12
  • @wfaulk Thanks, I didn't know that. I will change that in my code. May 9, 2012 at 21:15

4 Answers 4

2

You want to use awk.

dirsize=`du -sk /folder/path/here | awk '{print $1}'`
1

It's usually a tab. (hit most posix systems have hexdump, or the more tersely named xxd which would show you the chars printed). A more efficient solution than awk is 'cut':

 dirsize=`du -sk /folder/path/here | cut -f1`
0

du outputs a TAB character (ASCII 9) as the delimiter. You can verify that by piping the output to xxd or od -cx. Therefore, you need dirsize=${dirsize_temp/TAB*/}. You can type a literal TAB character at the bash prompt or in ViM by pressing Ctrl-V then TAB.

1
  • 1
    Bash inserts a TAB with $'\t', so dirsize=${dirsize_temp%%$'\t'*} works.
    – Thor
    May 11, 2012 at 8:22
0

+1 for demonstrating that regex substitution syntax, you learn something new every day! Mind if I researched it because of that and now one up it with ${var//+([[:space:]])/ } ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .