Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to calculate the size of a directory tree I am going to compress for archiving. I can't for the life of me figure out/feel how the word splitting or command substitution (which I've tried using to remedy the problem) in bash is functioning.

I want to exclude several directories. I place the arguments, which will also be passed to tar in the same manner, in a variable:

exclude_opts="--exclude='VirtualBox VMs/Windows 7/*'  
--exclude='Software/*'  
--exclude='LSTC/*'"

I then reference this in my call to du

$ du -sb $HOME $exclude_opts

38711578819 /home/patrick  
du: cannot access `VMs/Windows': No such file or directory  
du: cannot access `7/*"': No such file or directory  
38711578819 total

ok; $exclude_opts is being word split for some reason. How do I fix this? All my attempts have failed.

I would like to do this from within the same file; no --exclude-from

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You really need to use arrays when collecting arguments like this. Arrays will treat whitespace properly:

Update: removing the single quotes and "/*" from the exclude options

exclude_opts=(
  "--exclude=VirtualBox VMs/Windows 7"
  "--exclude=Software"
  "--exclude=LSTC"
)

du -sb "$HOME" "${exclude_opts[@]}"
share|improve this answer
    
While your solution does fix the white space problem (thank you for the good answer to the question), it introduces a new problem where du does not recognize the options you feed it. $ du -s "$HOME" 37G /home/patrick 37G total $ du -sb "$HOME" "${exclude_opts[@]}" 37G /home/patrick 37G total –  Patrick Feb 28 '12 at 21:26
    
+1, but I would also note that the double-quotes in "${exclude_opts[@]}" are significant; things will go terribly wrong without them. –  jjlin Feb 28 '12 at 21:33
    
@Patrick, after a quick test, need to remove the single quotes and "/*" from the exclude options. Please try again. –  glenn jackman Feb 29 '12 at 11:19
    
Single or double quotes does it for me; no variable substitution here. It was the wildcards pointed out earlier by @Hai-Vu. The command now functions as intended. Thank you both for your time and effort. –  Patrick Feb 29 '12 at 15:06

I tried to simulate your situation and found out that the following works:

--exclude="Windows 7"

These do not work:

--exclude='Windows 7/*'
--exclude='VirtualBox VMs/Windows 7/*'

Basically, it works if I only include the immediate directory, no wildcard. My system is a Linux server with kernel version 2.6.9.

share|improve this answer

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.