Home » Questions » Computers [ Ask a new question ]

Actual file size on mac os x

Actual file size on mac os x

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'?

Asked by: Guest | Views: 59
Total answers/comments: 2
Guest [Entry]

"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}'"
Guest [Entry]

"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"