du usage to get proper byte count

Posted on 2011-10-18
Last Modified: 2016-06-08
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.

Question by:bhilgenkamp
    LVL 39

    Expert Comment

    did you try du -b ?

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

    Expert Comment


    du -s ?

    Author Comment

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

    LVL 39

    Accepted Solution

    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.

    Author Comment

    I'll try to play around with rsync, I've never used it before so I'll see what I can come up with.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
    Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now