Search code examples
basharithmetic-expressions

How to compare percent file size in an if


Im trying to do this in a bash shell. Basically want to compare the size of two files by there percentage. If file1 is 90% different then file2 do something:

This is what I have so far:

newsize=$(wc -c <"$newfile")
oldsize=$(wc -c <"$oldfile")

if [[ $(($oldsize * 0.9)) -ge $newsize ]]; then
  echo 'This file is 90% or greater'
else
  echo 'This file is not large enough'
fi

Im getting an invalid arithmetic operator error on token "0.9" Any help or pointer would be appropriated


Solution

  • Try using integer math (such as 9/10) instead of floating point.

    Updated Script

    newsize=525
    oldsize=584
    
    if [[ $(($oldsize * 9/10)) -ge $newsize ]]; then
      echo 'This file is 90% or greater'
    else
      echo 'This file is not large enough'
    fi
    

    Example Output

    This file is 90% or greater