LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   File size comparison in bash? (https://www.linuxquestions.org/questions/programming-9/file-size-comparison-in-bash-475836/)

dwarf007 08-21-2006 05:21 AM

File size comparison in bash?
 
I am looking for a code to compare 2 different file size. Is there any reference? Any of you know?

Appreciate if you can help.....
Thank You.....

unSpawn 08-21-2006 05:51 AM

Check here http://www.linuxquestions.org/questi...28#post2386428 for instance.

druuna 08-21-2006 05:52 AM

Hi,

Something like this could do that:
Code:

#!/bin/bash

SIZE_01=`ls -l <file01> | awk '{ print $5 }'`
SIZE_02=`ls -l <file02> | awk '{ print $5 }'`


if [[ $SIZE_01 == $SIZE_02 ]]
then
  echo "Are equal."
else
  echo "Are not equal."
fi

You don't tell how you get the filesizes, I used awk to get field no 5 for 2 files. Should not be to difficult to re-write for your situation.

Hope this gets you going again.

unSpawn 08-21-2006 09:43 AM

SIZE_01=`ls -l <file01> | awk '{ print $5 }'`
Using ls|awk is overkill if "stat -c %s" suffices.
Just like I said in the post I mentioned.

dwarf007 08-22-2006 12:38 AM

I had tried the below but it didnt works well. Actually the file1 is smaller size than file2

#ls -l /home/abc/logs/file1 | awk '{ print $5 }'
8933
#ls -l /home/abc/logs/file2 | awk '{ print $5 }'
2851050


Code:

#!/bin/bash

SIZE_01=`ls -l /home/abc/logs/file1 | awk '{ print $5 }'`
SIZE_02=`ls -l /home/abc/logs/file2 | awk '{ print $5 }'`


if [[ $SIZE_01 > $SIZE_02 ]]
then
  echo "Success"
else
  echo "Failed"
fi

"Success" will be the output.
Whats going on?

I dont really understand the section
[HTML]Check here http://www.linuxquestions.org/questi...28#post2386428 for instance.[/HTML]

Anyone can help?

jlinkels 08-22-2006 06:24 AM

Not sure that ">" as an operator works. "-gt" is the correct syntax for sure.

Usually you would put the variables in quotes:

Code:

if [ "$SIZE_01" -gt "$SIZE_02" ]
That is not a requirement.


You also can use
Code:

SIZE=`wc -c < myfile`
but that is not the error. The awk method works as well.

jlinkels

AnanthaP 08-23-2006 07:06 AM

jlinkels is correct.

Where you expect a numeric comparison,
Code:

-gt
,
Code:

-ne
etc are required while
Code:

>
etc are for text based comparison.

End

dwarf007 08-24-2006 05:46 AM

Thanks man, you all are the best


All times are GMT -5. The time now is 09:09 AM.