Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

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
add comment

4 Answers 4

up vote 2 down vote accepted

You want to use awk.

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

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
add comment

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
1  
Bash inserts a TAB with $'\t', so dirsize=${dirsize_temp%%$'\t'*} works. –  Thor May 11 '12 at 8:22
add comment

+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
add comment

Your Answer

 
discard

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.