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 have a large number of folders that each contain quite a few files of varying sizes (from a few bytes to 400kb or so), mostly smaller ones. I need to get the actual (not the disk usage) size of these folders. Is there any way to do this with a command like 'du'?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

It's fairly standard for ls to show true file sizes by default, and I've just verified that. So:

ls -l <yourfiles>

To tally up the totals:

ls -l <yourfiles> | awk '{sum+=$5} END {print sum}'

If your tally needs to include files in subdirectories, the common solution is to use find to invoke ls:

find <yourdir>/. -type f -exec ls -l '{}' \; | awk '{sum+=$5} END {print sum}'
share|improve this answer
    
Looks good, but I get an error; "minimum blocksize is 512" :/ Thanks for the effort though! –  Jonatan Hedborg Jan 27 '10 at 17:54
    
I'm home at my mac now. Confirmed that a bare "ls" returns the exact size of files (as it should, really). Modified answer to remove superfluous options, which should clear up your error. –  pra Jan 28 '10 at 5:39
    
This works. thanks! –  Jonatan Hedborg Jan 28 '10 at 12:13

You could ask for the Mac-world total (includes resource forks) like this:

# Put this in a shell function or script, 'macTotal'
osascript - "${1:-.}" <<\EOF | perl -Mbignum -lpe '$_+=0,"\n"'
on run {arg}
    alias POSIX file arg
    tell application "System Events" to get size of result
end run
EOF

$ macTotal ~/Library
4465742628

The AppleScript prints the number in scientific notation. The Perl code is a sloppy way to expand the scientific notation.

If you are OK with reading the numbers from the GUI, just open a folder's Info window in Finder. The reported size is the same as what System Events gives in the AppleScript.


If you just care about data forks, I would go with something similar to pra's answer, but using stat instead of ls and xargs instead of -exec for a bit more efficiency.

$ find . -type f -print0 | xargs -0 stat -f %z | awk '{t+=$1}END{print t}'
4461971024
share|improve this answer

You could run wc -c on each of the files. This should return the number of characters in each file. It might take a while, since it actually reads through each file and counts the characters.

share|improve this answer

To see the total of just one specific folder use:

du -s /path_to/specific_folder/

To see to total of each folder in one specific folder use an asterisk:

du -s /path_to/specific_folder/*

If you don't own all of the files in those directories use sudo to be sure everything is counted.

Edit: Ah, I understand what you are asking now. You want:

du -sk /path_to/specific_folder/

share|improve this answer
    
That's what I'm doing currently (more or less). But it returns the "size on disk", not the actual file size. The difference is quite large in this case (40-70% depending on folder content). –  Jonatan Hedborg Jan 27 '10 at 17:43
    
-sk still reports disk usage, not actual file size :/ –  Jonatan Hedborg Jan 28 '10 at 11:30

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.