compare between files size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare between files size
# 1  
Old 07-22-2011
compare between files size

Hello

i have file A created yesterday and file B created today

thanks to help create script compare between 2 files according to size
so if file B is less than 10% of file A i should have echo message with warning
also check if file B exist or not.

this should be run in loop for list of files
i really dont know how to so such compare depending on percentage Smilie
# 2  
Old 07-22-2011
What is your system?

What is your shell?
# 3  
Old 07-22-2011
Quote:
Originally Posted by Corona688
What is your system?

What is your shell?
it will be run remotely using sshpass , the systems would be even redhat enterprise or sun solaris

the shell most of time is bash
# 4  
Old 07-22-2011
Use 'ls -l' to find out the size, then 'bc' to do the math, but stay away from floating point numbers (bash at least, can't compare floats in tests)

Code:
sizeA=`ls -l A | awk '{print $5}'`
sizeB=`ls -l B | awk '{print $5}'`

rat=`echo "$sizeB * 100 / $sizeA" | bc` #integer result

if [ $rat -lt 10 ] ; then
  #do whatever
fi

# 5  
Old 07-22-2011
Quote:
Originally Posted by mirni
Use 'ls -l' to find out the size, then 'bc' to do the math, but stay away from floating point numbers (bash at least, can't compare floats in tests)

Code:
sizeA=`ls -l A | awk '{print $5}'`
sizeB=`ls -l B | awk '{print $5}'`

rat=`echo "$sizeB * 100 / $sizeA" | bc` #integer result

if [ $rat -lt 10 ] ; then
  #do whatever
fi

thanks for your reply

what if i have list of files need to compare ?
as A and B
C and D
X and Y

also how to check existing of new file ?
# 6  
Old 07-22-2011
Quote:
what if i have list of files need to compare ?
make your list neat and tidy, with filenames grouped in pairs:
Code:
A.txt B.txt
C.txt D.txt

etc...

Then you can loop through the list easily:
Code:
while read A B ; do 
  sizeA=`ls -l $A | awk '{print $5}'`
  sizeB=`ls -l $B | awk '{print $5}'` 
  rat=`echo "$sizeB * 100 / $sizeA" | bc` #integer result
  if [ $rat -lt 10 ] ; then
    #do whatever
  fi
done < yourList.lst

That is assuming your filenames don;t contain whitespace characters. Otherwise, quote all variables.

Quote:
also how to check existing of new file ?
You could, for example, check the number of files present against the number of files in your list.
# 7  
Old 07-22-2011
Quote:
Originally Posted by mirni
make your list neat and tidy, with filenames grouped in pairs:
Code:
A.txt B.txt
C.txt D.txt

etc...

Then you can loop through the list easily:
Code:
while read A B ; do 
  sizeA=`ls -l $A | awk '{print $5}'`
  sizeB=`ls -l $B | awk '{print $5}'` 
  rat=`echo "$sizeB * 100 / $sizeA" | bc` #integer result
  if [ $rat -lt 10 ] ; then
    #do whatever
  fi
done < yourList.lst

That is assuming your filenames don;t contain whitespace characters. Otherwise, quote all variables.


You could, for example, check the number of files present against the number of files in your list.
thanks a lot for ur help

i think i dont need to check coz the value anyway will be less than 10%

thanks again for ur support
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare file size and then copy/overwrite

// Redhat I have this code working, but need to add one more qualification so that I don't overwrite the files. #!/bin/sh cd /P2/log/cerner_prod/millennium/archive/ for f in * do || continue #If this isn't a regular file, skip it. && continue #If a backup already... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

2. Shell Programming and Scripting

Script to Compare file size and delete the smaller

I am pretty new to scripting, so I appreciate your advice in advance. The problem: 100 directories each containing 2 files that have the same extension with random names. The only attribute that discriminates the files is size. I would like to write a script that compares the files for size... (6 Replies)
Discussion started by: JC_1
6 Replies

3. Shell Programming and Scripting

How to compare if the size of the directory is more than 500MB in unix

I use du -sk command to find the size of the directory but when i use the result of 'du -sk' into if statement its throwing error.. Could u solve with this..? (14 Replies)
Discussion started by: shaal89
14 Replies

4. UNIX for Dummies Questions & Answers

PSFTP- Compare file size

Hi, I'm using PSFTP to transfer files from one machine to a virtual machine with UBUNTU OS installed on it. I'm trying to find a way to make sure the files that I'm uploading / downloading are being uploaded/ downloaded properly. I want to compare the size of the local file and the remote... (0 Replies)
Discussion started by: sessie
0 Replies

5. Shell Programming and Scripting

zsh compare size pdf and delete bigger?

I have used an script to reduce the size of multiples pdf. This script creates files with the same name but with different extension. The extension of the compressed files is xpdf. Sometimes the "compressed" xpdf are bigger than the "uncompressed"pdf. I want to create a zsh script to compare each... (1 Reply)
Discussion started by: AMDx64BT
1 Replies

6. Shell Programming and Scripting

Compare list [ names and size files ]

Hello, I've downloaded a huge amont of files I've got a list of files from a remote server. -rw-r--r-- 1 str661 strem 453465260 Dec 16 15:54 SATRYS2V1_20021218_temp_bias.nc -rw-r--r-- 1 str661 strem 17669468 Dec 16 18:01 SATRYS2V1_20021225_hdyn_bias.nc -rw-r--r-- 1... (9 Replies)
Discussion started by: Aswex
9 Replies

7. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

8. Shell Programming and Scripting

How to compare size of two file which is in one directory

I have two file in a Directory.I want a script which will compare the Size of Two file. Can Anyone Help me on this: linasplg11:/opt/dataout/kk/linasplg11 # cat size -rwxrwxrwx 1 root root 16658 Jan 8 13:58 lina_IP_SIP_1231325621210.xml -rwxr-xr-x 1 root root 16672 Jan 8 14:30... (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

9. Shell Programming and Scripting

How to compare file size after ftp?

Is possible if I want to campare file size on source and destination after ftp transfer? If anybody know, please explain to me. (1 Reply)
Discussion started by: icemania
1 Replies

10. Shell Programming and Scripting

bash script working for small size files but not for big size files.

Hi, I have one file stat. Stat file contents are as follows: for example. H50768020040913,00260100,507680,13,0000000643,0000000643,00000,0000 H50769520040808,00260100,507695,13,0000000000,0000000000,00000,0000 H50770620040611,00260100,507706,13,0000000000,0000000000,00000,0000 Now i... (1 Reply)
Discussion started by: davidpreml
1 Replies
Login or Register to Ask a Question