LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-21-2006, 06:21 AM   #1
dwarf007
Member
 
Registered: Sep 2004
Location: Malaysia
Posts: 181

Rep: Reputation: 30
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.....
 
Old 08-21-2006, 06:51 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,744
Blog Entries: 54

Rep: Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973
Check here http://www.linuxquestions.org/questi...28#post2386428 for instance.
 
Old 08-21-2006, 06:52 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 08-21-2006, 10:43 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,744
Blog Entries: 54

Rep: Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973
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.
 
Old 08-22-2006, 01:38 AM   #5
dwarf007
Member
 
Registered: Sep 2004
Location: Malaysia
Posts: 181

Original Poster
Rep: Reputation: 30
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?
 
Old 08-22-2006, 07:24 AM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,243

Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
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
 
Old 08-23-2006, 08:06 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 659

Rep: Reputation: 160Reputation: 160
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
 
Old 08-24-2006, 06:46 AM   #8
dwarf007
Member
 
Registered: Sep 2004
Location: Malaysia
Posts: 181

Original Poster
Rep: Reputation: 30
Thanks man, you all are the best
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File size using BASH script cb951303 Programming 16 11-07-2012 08:45 AM
bash string comparison noir911 Programming 1 01-25-2006 07:37 PM
Bash script that returns the size of a file spank Linux - General 6 01-19-2006 04:24 PM
Browser size comparison TigerLinux Linux - Software 2 09-22-2005 10:21 AM
newbie: how to see the size of file under bash? sirpelidor Linux - Newbie 3 05-19-2004 08:23 PM


All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration