Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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?

share|improve this question
-h and -k are mutually exclusive. Pick one. – wfaulk May 9 '12 at 21:12
@wfaulk Thanks, I didn't know that. I will change that in my code. – DarthCaniac May 9 '12 at 21:15
up vote 2 down vote accepted

You want to use awk.

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

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`
share|improve this answer

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.

share|improve this answer
Bash inserts a TAB with $'\t', so dirsize=${dirsize_temp%%$'\t'*} works. – Thor May 11 '12 at 8:22

+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:]])/ } ...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.