du usage to get proper byte count

I am trying to write a bash script to compare the size of a folder and a mounted disk (SD card in this case). However I get different sizes reported between the two of them using du, but Finder shows the exact byte count properly when I do a Get Info on the files. I believe this may be due to different block sizes between the SD card and the server share the files are on. The du usage I'm using is "du -c". Is there an argument I can add that will properly display the byte count between the two sources?

If du is not the right way to go for this please let me know if there is a better way. I've included some of my script for reference:

# Get volume name to erase
read -p 'Please enter the volume name you would like to erase: (default is '"$default_volume"'): ' get_volume
	if [ "get_$volume" = "$default_volume" ]; then

# Check if volume exists
	if [ ! -d /Volumes/"$volume" ]; then
		echo 'Volume does not exist. Quitting.'

# Get folder and size to check card against for identical size
echo 'Drag the folder into this window to compare to volume '"$volume"', then press return '
read "compare_folder"

source_folder_size=`du -cI ".*" "$compare_folder"/* | grep total | awk 'BEGIN { FS = "\t" } ; {print $1}'`

# Compare folder and card sizes

sd_card_size=`du -cI ".*" /Volumes/"$volume"/* | grep total | awk 'BEGIN { FS = "\t" } ; {print $1}'`
echo "Card size is $sd_card_size"
echo "Folder size is $source_folder_size"
		if [ $source_folder_size -eq $sd_card_size ]; then
			echo Sizes are the same, continuing to delete...
			read -p 'Sizes differ, continue deleting anyway? (y/n) ' continue_delete

Open in new window

So after running this I get this response:

Card size is 8014976
Folder size is 8013888

Sizes differ, continue deleting anyway? (y/n)

Like I said though Finder shows the exact same amount of bytes between the two directories, and I can't seem to figure out how to make my script check sizes the same way Finder does.

Who is Participating?
nociSoftware EngineerCommented:
I have no OS X, so sorry about that. The default current linux du does provide it.

You can try rsync --dry-run to compare directories, if no copy would occur (eveb the content) is equal.
nociSoftware EngineerCommented:
did you try du -b ?

That should give exact byte sizes.. (But it includes the size of directory files).

du -s ?
bhilgenkampAuthor Commented:
-b is not an available option on Mac OS X

-s still shows different sizes - for example:

e2:~ brent$ du -s /Volumes/ST1/\ Drop\ Folder/BOA\ Conroe\ TX\ -\ 685739\ SD\ CARDS/57/AVF_INFO/AVIN0001.BNP
225296      /Volumes/ST1/ Drop Folder/BOA Conroe TX - 685739 SD CARDS/57/AVF_INFO/AVIN0001.BNP
e2:~ brent$ du -s /Volumes/Scratch/AVIN0001.BNP
225280      /Volumes/Scratch/AVIN0001.BNP

The exact same file was compared there, the only difference is one resides on the server and the other on my local machine.

bhilgenkampAuthor Commented:
I'll try to play around with rsync, I've never used it before so I'll see what I can come up with.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.