Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comparing size

by splitOnce (Acolyte)
on Aug 19, 2002 at 14:00 UTC ( #191173=perlquestion: print w/ replies, xml ) Need Help??
splitOnce has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks , How do I compare the size of two files ,I am trying to grep for all the ".o" files in a file then compare the size with the ".o" in another file , each file contain value like this :
-rw-r----- 1 sa mllan 1309064 Aug 16 12:07 wirele/pqc/bcnnnMgr.o -rw-r----- 1 sa mllan 130064 Aug 16 12:07 wirele/pqc/bcnooonnMgr.o -rw-r----- 1 sa mllan 13096064 Aug 16 12:07 wirerrrle/pqc/bcgr.o
both files have the same lists but , what I am looking for is the size if had change ..
while (<INfile>) { if $_ =~ m/\w+\.o/; $fileOne = $_; while (<INfile2>) { if $_ =~ $fileOne; $_ = $fileTwo; 'cmp $fileOne $fileTow`; } }
can someone help me finding a good way to do this ,, thanks

Comment on comparing size
Select or Download Code
Re: comparing size
by gmpassos (Priest) on Aug 19, 2002 at 14:08 UTC
    Just use the stat() to get the size and other information of a file:

    my $file_size = (stat($file_path))[7] ;

    The full list:

    @FL_stats = stat ("") ; # Geting stats of file. $FL_Dev = $FL_stats[0] ; # Device that the file resides on. $FL_Inod = $FL_stats[1] ; # Inode for this file. $FL_Mode = $FL_stats[2] ; # Permissions for the file. $FL_NbLink = $FL_stats[3] ; # Number of hard links to the file. $FL_UID = $FL_stats[4] ; # Numerical user ID for the file owner +. $FL_GID = $FL_stats[5] ; # Numerical group ID for the file owne +r. $FL_TypDev = $FL_stats[6] ; # Device type if the file is a device. $FL_size = $FL_stats[7] ; # Size of the file in bytes. $FL_AcTime = $FL_stats[8] ; # When the file was last accessed. $FL_MdTime = $FL_stats[9] ; # When the file was last modified. $FL_ChTime = $FL_stats[10] ; # When the file status was last change +d. $FL_BlkSize = $FL_stats[11] ; # The optimal block size for i/o opera +tions on the file system containing the file. $FL_Blocks = $FL_stats[12] ; # The number of clocks allocated to th +e file.

    "The creativity is the expression of the liberty".
      I do not think you gave a good answer. First of all, how to get the size of a file wasn't the question, the question was about comparing values found in a file. Second, if you want the size of a file, no need to remember the order of all the things stat returns. Just use -s.

      Abigail

        Sorry, I read fast the question! Here are some code to compare the 2 list of files:

        my %size1 = &size_list('path/to/file1') ; my %size2 = &size_list('path/to/file2') ; if ( $size1{'wirerrrle/pqc/bcgr.o'} == $size2{'wirerrrle/pqc/bcgr.o' +} ) { print "OK\n" ; } ############# # SIZE_LIST # ############# sub size_list { my ( $file ) = @_ ; open (FILEIO,$file) ; my $data = join '' , <FILEIO> ; close (FILEIO) ; my (@list) = ( $data =~ /(\d+)\s+\w+\s+\d+\s+[\d:]+\s+(.*?)\s/gs ); my %sizes ; for (my $i = 0 ; $i <= $#list ; $i+=2) { $sizes{@list[$i+1]} = @list[$i] ; } return( %sizes ) ; }

        "The creativity is the expression of the liberty".
Re: comparing size
by Abigail-II (Bishop) on Aug 19, 2002 at 14:15 UTC
    I'd use something like this:
    use strict; use warnings 'all'; my ($first, $second) = qw /first second/; open my $f1h => $first or die "open $first: $!\n"; open my $f2h => $second or die "open $second: $!\n"; my %first = reverse map {/(\d+)\s+\w+\s+\d+\s+\d+:\d+\s+(.*)/} grep {/\.o$/} <$f1h>; my %second = reverse map {/(\d+)\s+\w+\s+\d+\s+\d+:\d+\s+(.*)/} grep {/\.o$/} <$f2h>; my ($file, $size); print "$file: $size <-> $second{$file}\n" while +($file, $size) = each + %first;
    Abigail
Re: comparing size
by gav^ (Curate) on Aug 19, 2002 at 14:19 UTC
    To find the size of a file you can use: my $size = -s $file;
    The -X file tests are documented here.

    You could also use the core module File::stat which provides dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, and block. I tend to find this easier than remembering what order stat returns things.

    use File::stat; my $st = stat($file); my $size = $st->size;

    gav^

Re: comparing size
by adrianh (Chancellor) on Aug 19, 2002 at 14:23 UTC

    Use "-s FILENAME" to return the size (in bytes) of a file, see perlfunc for more info. You'd want something like this

    if (-s $fileOne != -s $fileTwo) { print "have different sizes\n"; } else { print "the same size\n"; };

    Some other things to think about:

    • You are trying to read all of file2 every time find a match in file1. It's probably simpler to read file2 once and cache the results.
    • Since you're not re-opening INfile2 that inner loop is probably not doing what you want...
    • The regex you have will ignore the full path since it won't match the "/" character - this may or may not be what you want...

    Hope this helps.

      thanks all , :)
Re: comparing size
by waswas-fng (Curate) on Aug 19, 2002 at 16:08 UTC
    Hmm, to take a step back, what is the problem you are trying to solve with this code? Are you looking to manage size (growth shrinkage) or are you trying to notice if file contents have changed? If the latter is your goal may I suggest using a digest module (MD5) to make a digest of each file, maybe using File::Find along with it to generate the flat file db that you are using to verify file contents. File size does not tell you for sure if a file has changed, amd5 checksum would. Just my 2c

    -Waswas

      Indeed.

      An example of watching files for changes can be found at "Watching tests...".

      If that is what you're after then you might want to take a look at File::Modified, which packages up tests for file content changes quite nicely.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://191173]
Approved by SparkeyG
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-12-20 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls