Computing.Net > Forums > Linux > COmparing file size

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

COmparing file size

Reply to Message Icon

Name: panther9_99
Date: January 30, 2005 at 20:29:43 Pacific
OS: redhat 9
CPU/Ram: amd-64bit
Comment:

Hi there,
can anybody tell me how to compare the size of the given files( 2 or more) and then display the one with bigger size. ( all this is in command line, I don't have GUI)



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: January 31, 2005 at 05:28:41 Pacific
Reply:

Create the following script. Make it executable ( chmod ugo+x scriptname ). Execute it with the names of the two files you want to compare.

#!/bin/sh
if [ $# -ne 2 ] ; then
echo "USAGE: $0 file1 file2"
exit 4
fi
file1=$1
file2=$2
file1size=`ls -l $file1 | awk '{ print $5 }'`
file2size=`ls -l $file2 | awk '{ print $5 }'`
if [ $file1size -eq $file2size ] ; then
echo "$file1 and $file2 are the same size"
elif [ $file1size -gt $file2size ] ; then
echo "$file1 is larger with $file1size bytes"
else
echo "$file2 is larger with $file2size bytes"
fi


0

Response Number 2
Name: panther9_99
Date: January 31, 2005 at 09:39:21 Pacific
Reply:

thanks for the script.
ok, this script would work fine with two files. what if in a given directory there are several files and I just want to display the size of the biggest file.
I figured out a way to display the file size and the file name, but all i want is the file size and not the file name.
here is the command:
ls -s | sort -nr | head -1
cannot figure out how can I just display the file size.


0

Response Number 3
Name: David Perry
Date: January 31, 2005 at 10:40:51 Pacific
Reply:

ls -s | sort -nr | head -1 | awk '{ print $1 }'

Bear in mind that the -s flag shows a block count not a byte count.


0

Response Number 4
Name: panther9_99
Date: January 31, 2005 at 10:59:15 Pacific
Reply:

thanx david.
so how do I show a byte count?


0

Response Number 5
Name: xan
Date: January 31, 2005 at 13:24:56 Pacific
Reply:

Don't forget to ignore folder's size:

find ./ -type f -maxdepth 1 | xargs ls -lk | sort -n -k 5 | tail -1 | awk '{print $5*1024,"bytes"}'

Modify awk's output as you wish.


0

Related Posts

See More



Response Number 6
Name: panther9_99
Date: January 31, 2005 at 22:53:25 Pacific
Reply:

thanx man, that would work.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: COmparing file size

file-size limit in 'ext3' www.computing.net/answers/linux/filesize-limit-in-ext3/23344.html

LimeWire download file size www.computing.net/answers/linux/limewire-download-file-size/14852.html

File size to big. www.computing.net/answers/linux/file-size-to-big/10610.html