LinuxQuestions.org
Visit Jeremy's Blog.
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, 05: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, 05:51 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,017
Blog Entries: 54

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

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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, 09:43 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,017
Blog Entries: 54

Rep: Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764
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, 12: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, 06:24 AM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

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

Rep: Reputation: 127Reputation: 127
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, 05: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 07:45 AM
bash string comparison noir911 Programming 1 01-25-2006 06:37 PM
Bash script that returns the size of a file spank Linux - General 6 01-19-2006 03:24 PM
Browser size comparison TigerLinux Linux - Software 2 09-22-2005 09:21 AM
newbie: how to see the size of file under bash? sirpelidor Linux - Newbie 3 05-19-2004 07:23 PM


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

Main Menu
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