Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare file sizes in shell script

Tags:

linux

shell

sh

I'm trying to compare the size of two files in shell script but I'm getting a test: 32: 8: unexpected operator error.

I=`wc -c $i | cut -d' ' -f1`
J=`wc -c $j | cut -d' ' -f1`
if test $I == $J
then
      echo $i $j >> $1.pares
fi

I test the values in $I and $J using echo and the values are correct but I cant compare them...

like image 508
Tiago Costa Avatar asked Nov 12 '11 23:11

Tiago Costa


People also ask

How does shell compare file sizes?

Getting file size using find command The syntax is as follows for the find command: find "/etc/passwd" -printf "%s" find "/etc/passwd" -printf "%s\n" fileName="/etc/hosts" mysize=$(find "$fileName" -printf "%s") printf "File %s size = %d\n" $fileName $mysize echo "${fileName} size is ${mysize} bytes."

How do I check the size of a file in Unix shell script?

The stat is a UNIX command-line utility. Stat takes a file as an argument and returns the detailed information about a file/file system. Note: Here, %s is used to fetch the total size of the file, and -c is used for specifying output format i.e. we want to print the total size of the file only.

How do I determine file size in bash?

Another method we can use to grab the size of a file in a bash script is the wc command. The wc command returns the number of words, size, and the size of a file in bytes.


1 Answers

this works on bash

if((`stat -c%s "$file1"`==`stat -c%s "$file2"`));then
  echo "do something"
fi
like image 168
Aquarius Power Avatar answered Sep 28 '22 05:09

Aquarius Power