Script to compare file sizes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to compare file sizes
# 1  
Old 04-19-2011
Question Script to compare file sizes

I need to write a bash script larger X Y that compares the sizes of two specified files X and Y,
and reports which file is larger. For example, if X is larger, the output should be "File X is larger",
while if Y is larger, the output should be "File Y is larger".
If the files are exactly the same size, the output should be "File X and File Y are exactly the same size".
If X and/or Y are not valid names of existing files, then report a reasonable error message, such as "File X does not exist".
If fewer than two command line arguments are specified, an appropriate error message should be given.
Show the output of your script when comparing the size of your shell script called larger to the size of the /etc/passwd file on your Linux system.

Last edited by fpmurphy; 04-19-2011 at 03:21 PM..
# 2  
Old 04-19-2011
Is this homework?
Anyway, look at the stat command to know the size of a file:
Code:
stat -c %s filename
stat --help

And use this syntax to write your conditions with bash
Code:
#!/bin/sh

# var1 contains size of file1
var1=$(stat -c%s file1)
# var2 contains size of file2
#same as above
if [[ "$var1" -lt "$var2" ]]; then
    echo '..........'
elif [[ "$var1" -gt "$var2" ]]; then
    echo '..........'
elif [[ "$var1" -eq "$var2" ]]; then
    echo '..........'
fi

man bash for more informations.

Last edited by tukuyomi; 04-19-2011 at 03:28 PM.. Reason: bash tests with [[ instead of single [, works anyways because of the shebang (bin/sh)
# 3  
Old 04-19-2011
This looks like homework, here's a start:

Code:
if [[ $(stat -c%s file1) -ge $(stat -c%s file2) ]]; then <do something>...

# 4  
Old 04-19-2011
Yes, It's a homework Smilie ..
thanks for help .. Smilie

Last edited by jim mcnamara; 04-19-2011 at 06:20 PM.. Reason: thread closed - homework
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh scripting SSH to Compare File Sizes

Hello, I currently have very little experience with Shell scripting and trying to create a script for the purpose of collecting the size of a couple sizes on 4 different Hosts. The Idea is to collected the information from the files in which the script is kicked off on, store the values into... (17 Replies)
Discussion started by: Abstract3000
17 Replies

2. Shell Programming and Scripting

Using csh / awk / sed to compare database sizes in a txt file

Hello, I have an output file showing database sizes across the 3 environments that I use (LIVE, TEST & DEVELOPMENT). I am trying to write a script that lets me know if the size of a db on one environment is different to its corresponding db on the other environments. Here is an example... (4 Replies)
Discussion started by: stevie_g
4 Replies

3. UNIX for Dummies Questions & Answers

Compare two file sizes.

Hi everyone! I need to compare two file sizes. One of them (size) will be stored in a flat file and the other coming from a listed file. I can now get the first file size using: SIZE=`ls -l $DOCTYPE | awk '{print $5}'` 1. How can I store this value in a flat file? 2. How... (2 Replies)
Discussion started by: mrreds
2 Replies

4. HP-UX

compare file percent sizes

I need to get a file size and compare it to a previous day file size. If it's larger or smaller by 50 percent I'll replace the new with the old. I know how to get the file sizes but do not know how to calculate if it's 50 percent difference. Thanks for your help. (2 Replies)
Discussion started by: jkuchar747
2 Replies

5. UNIX for Dummies Questions & Answers

Checking file sizes in script

Hi, I'm trying to check a filesize within a script and then excute a relevant action. An example is below: if then rm $filename rm $filename2 elif then rm $filename2 fi Basically if $filename2 has a filesize of 0 then I want both files to be removed, but... (6 Replies)
Discussion started by: chris01010
6 Replies

6. Shell Programming and Scripting

how to compare file sizes

hi ls -l * | sed 's/\+/ /g' | cut -f5 -d " " >out1 ls -l * | sed 's/\+/ /g' | cut -f5 -d " " >out2 diff out1 out2 i tried this it will work fine and i can see difference but i need a script which should neglect, if the difference b/w files is small and it should display... (5 Replies)
Discussion started by: revenna
5 Replies

7. Shell Programming and Scripting

to compare total directory structure and get sizes of all f on two different servers

Hello every one, Iam newbie to this forum and shell programming &scripting. i needed to compare each and every folder of two separate servers. Actually I have copied some directory structure from one server to second server, to build on second server the files all should be copied... (3 Replies)
Discussion started by: mannam srinivas
3 Replies

8. Shell Programming and Scripting

Script to check and report database file sizes...

Need help for a script to check and report database file sizes. (2 Replies)
Discussion started by: marconi
2 Replies

9. Shell Programming and Scripting

Script for file names/sizes

How can I look at a certain directory and list all the file names, locations and sizes of each file in the current directory and all subdirectories? (2 Replies)
Discussion started by: ssmiths001
2 Replies

10. Shell Programming and Scripting

compare file sizes

Is there a command that will return the name of the largest file within a directory? If so, can I set the returned filename into a variable? (4 Replies)
Discussion started by: joli
4 Replies
Login or Register to Ask a Question