Search code examples
shellunixsizetcsh

Getting the size of a directory in unix shell


I am a relative new comer to programming in the Unix shell environment and I had a question on how to get the size of directory. I have read that you can do commands such as; du, du -b, and so on. However, when I try to put this in my program it somehow returns the path name instead of the actual directory size. It's like the concatenation is not working or something. And the .garbage file is definitely in my home directory so I don't know what the problem is. It's probably some stupid error that I overlooked. If any pair of fresh eyes or someone experienced in UNIX could take a look at this for me I would greatly appreciate it! Here is my shell code:

    #!/bin/tcsh -f

set n = 1

mkdir -p ~/.garbage

while ($n <= $#argv)
 if ( -d $argv[$n] ) then
   echo "$argv[$n] is a directory and cannot be removed"
 else


     mv $argv[$n] ~/.garbage

     echo "The file moved to the garbage directory was: "$argv[$n] "\n"

 endif 
@ n++
end

  echo "The garbage bin's size is: " du ~/.garbage "bytes."

Solution

  • It seems you missed to use command substitution on your echo line. You have to enclose your call to du -sh with backticks like this

    echo "size is:" `du -sh` "...."