Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

File Statistics

by sweetblood (Prior)
on Jan 11, 2005 at 13:16 UTC ( [id://421266]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info Sweetblood
Description:
fstat [-a] [-s] [-m] [-p] [-u] [-h] file ... -a Print files access time. -m Print files modify time. -s Print files size in bytes. -p Print files permissions(mode). -u Print files owner user id. -h Print help(this screen).
If no options are present on the command line all statistics will be returned.
#!/usr/bin/perl -w

use strict;
use Getopt::Std;
use File::Basename;
use Time::localtime;
use File::stat qw(:FIELDS);

$|=1;
my %opts;
my $me = basename($0);

getopts('amspuh', \%opts);
do { usage();exit } if exists $opts{h};
usage() if (@ARGV < 1);

if (keys %opts) {
    for my $file (@ARGV) {
        do { warn "File: $file not found\n"; next } unless -f $ARGV[0]
+;
        print "\nFile: $file\n";
        stat($file) or do {warn "Cannot get statistics on $file", $!;n
+ext };
        SWITCH:for (keys %opts) {
            
                /^a$/i    && do {
                                    print "\tatime: ", ctime($st_atime
+), "\n";
                                    next SWITCH
                                };
    
                /^m$/i    && do {
                                    print "\tctime: ", ctime($st_mtime
+), "\n";
                                    next SWITCH
                                };
                
                /^s$/i    && do {
                                    print "\tsize:  $st_size\n";
                                    next SWITCH
                                };
                
                /^p$/i    && do {
                                    my $fmode = sprintf "%o", $st_mode
+;
                                    $fmode =~ s/^.{3}(.{3}$)/$1/;
                                    print "\tmode:  $fmode\n" ;
                                    next SWITCH
                                };
    
                /^u$/i    && do {
                                    
                                    printf "\towner: %s\n", ($^O =~ /^
+MSW/) ?
                                        "Unsupported in MS Windows"
                                        :
                                        (getpwuid $st_uid)[0];
                                    next SWITCH
                                };    
        }
    }    
} else {
    do_all($_) foreach (@ARGV)
}

sub do_all {
    my $file = shift;
    print "\nFile: $file\n";
    stat($file) or do {warn "Cannot get statistics on $file", $!;next 
+};
    print "\tatime: ", ctime($st_atime), "\n";
    print "\tctime: ", ctime($st_mtime), "\n";
    print "\tsize:  $st_size\n";
    my $fmode = sprintf "%o", $st_mode;
    $fmode =~ s/^.{3}(.{3}$)/$1/;
    print "\tmode:  $fmode\n" ;
    printf "\towner: %s\n", ($^O =~ /^MSW/) ?
        "Unsupported in MS Windows"
        :
        (getpwuid $st_uid)[0];    
}

sub usage {
    die <<EOM

    Usage: $me [-a] [-s] [-m] [-p] [-u] [-h] file

                -a  Print files access time.
                -m  Print files modify time.
                -s  Print files size in bytes.
                -p  Print files permissions(mode).
                -u  Print files owner user id.
                -h  Print help(this screen).
        
        Any combination of options may be used with the exception of
        -h The help command is exclusive.
        If no options are present on the command line all statistics
        will be returned.

EOM
}
Replies are listed 'Best First'.
Re: File Statistics
by Albannach (Monsignor) on Jan 11, 2005 at 15:19 UTC
    You could use Roth's Win32::Perms to get the Owner() under Win32 if you like.

    --
    I'd like to be able to assign to an luser

      Hey good idea. Thanks!

      Sweetblood

Re: File Statistics
by ambrus (Abbot) on Jan 12, 2005 at 16:29 UTC

    A new stat program that offers similar functions is now part of GNU coreutils.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://421266]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-20 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found