Actual file size on mac os x
Solution 1
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}'
Solution 2
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/
Solution 3
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
Solution 4
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.
Related videos on Youtube
Jonatan Hedborg
Updated on September 17, 2022Comments
-
Jonatan Hedborg over 1 year
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'?
-
Jonatan Hedborg about 14 yearsThat'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 about 14 yearsLooks good, but I get an error; "minimum blocksize is 512" :/ Thanks for the effort though!
-
pra about 14 yearsI'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.
-
Jonatan Hedborg about 14 years-sk still reports disk usage, not actual file size :/