Symbolic Links and Sorting 
Author Message
 Symbolic Links and Sorting

In comp.lang.perl,

:Hi,
:
:Newbie question time :
:
:I'm trying to write what should theoretically be a quite straightforward
:program : listing the ten (later expansion to n) largest files in your
:directory (including subdirectories).
:
:OK - quite a simple recursive subroutine to go down the directories and
:get the size of every file ...
:
:Problem 1 :
:
:   I can't get the test for symbolic links to work.
:
:Problem 2 :  (well more of a query cos I haven't really tried yet)
:
:   What is the best way to sort the result?
:
:   o   Write the whole output to a file and then use and exec'ed sort?
:   o   Keep everything in a mad 2 dimensional array and sort it?
:   o   Some much better way I haven't thought of?
:

Use
    lst -s | head -10
where the lst program is the following.  It's v4.036 perl.  If
you have v5, you can make it better by using real structures.

--tom

#!/usr/bin/perl

require 'find.pl';
require 'ctime.pl';
require 'getopts.pl';
require 'stat.pl';

&Getopts('lusrcmi') || die <<DEATH;
Usage: lst [-lmucsr] [dirs ...]

Output format:
    -l  long listing

Sort on:
    -m  use mtime (modify time) [DEFAULT]
    -u  use atime (access time)
    -c  use ctime (inode change time)
    -s  use size for sorting

Ordering:
    -r  reverse sort

    NB: You may only use select one sorting option at a time.
DEATH


if ($opt_c + $opt_u + $opt_s + $opt_m > 1) {
    die "can only sort on one time or size";

Quote:
}

$IDX = $ST_MTIME;      
$IDX = $ST_ATIME if $opt_u;
$IDX = $ST_CTIME if $opt_c;    
$IDX = $ST_SIZE  if $opt_s;    

$TIME_IDX = $opt_s ? $ST_MTIME : $IDX;

if ($opt_i) {
     *name = *_;  # $name is now an alias for $_
     while (<>) { chop; &wanted; }

Quote:
}  else {

}



    if ($opt_l) {

        chop($now = &ctime($stats[$TIME_IDX]));
        printf "%6d %04o %6d %8s %8s %8d %s %s\n",
                $stats[$ST_INO],
                $stats[$ST_MODE] & 07777,
                $stats[$ST_NLINK],
                &user($stats[$ST_UID]),
                &group($stats[$ST_GID]),
                $stats[$ST_SIZE],
                $now,
                $_;
    } else {
        print "$_\n";
    }

Quote:
}

sub wanted {

    -f _ || return;
    $time{$name} = $stats[$IDX];

Quote:
}

sub user {
    local($uid)= shift;
    $user{$uid} = (getpwuid($uid))[0] || "#$uid"
        unless defined $user{$uid};
    return $user{$uid};

Quote:
}

sub group {
    local($gid)= shift;
    $group{$gid} = (getgrgid($gid))[0] || "#$gid"
        unless defined $group{$gid};
    return $group{$gid};
Quote:
}

--

BASIC: A programming language.  Related to certain social diseases in
that those who have it will not admit it in polite company.



Thu, 17 Jul 1997 02:14:05 GMT  
 Symbolic Links and Sorting


:I'm trying to write what should theoretically be a quite straightforward
:program : listing the ten (later expansion to n) largest files in your
:directory (including subdirectories).
:
:OK - quite a simple recursive subroutine to go down the directories and
:get the size of every file ...
:
:Problem 1 :
:
:   I can't get the test for symbolic links to work.
:
:Problem 2 :  (well more of a query cos I haven't really tried yet)
:
:   What is the best way to sort the result?
:
:   o   Write the whole output to a file and then use and exec'ed sort?
:   o   Keep everything in a mad 2 dimensional array and sort it?
:   o   Some much better way I haven't thought of?

For instance, the following might work:

    &do_dir('.');

    for ($i=0; $i<10; $i++) {
        print "$files[$i]: $filsiz{$files[$i]}\n";
    }

    sub do_dir {
        local($dirname) = shift;

        opendir(D, $dirname);

        closedir(D);


            $file = "$dirname/$file";
            if (-d $file) {
                &do_dir($file);
            }
            elsif (-f _) {
                $filsiz{$file} = (-s _);
            }
        }
    }

However, it isn't efficient in time to test large home directory
which contains numerous subdirectories and files including e-mail messages.

Using du(1) and sorting the output would be a good solution.  Durf,

of good formatter, I remember.

--
Toru "devil-may-care" Shiono          Sony Corporation, JAPAN



Fri, 18 Jul 1997 20:33:39 GMT  
 Symbolic Links and Sorting

Tony> Ah ... but the "require 'find.pl'" is a bit of a cheat ... :)

Tony> a) I don't have that cos Perl wasn't installed properly here (they keep  
Tony>    promising to do it properly ....)

I don't consider it cheating to use a built-in part of the language.
Are you going to restrict yourself from using associative arrays for
the experience?

And, I'd shoot your installer.  That's downright mean to not perform
a proper installation.  Gives Perl a bad rep.

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>



Thu, 24 Jul 1997 03:18:08 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Chown'ing a symbolic link

2. File::Find doesn't follow symbolic links

3. find2perl doesn't follow symbolic links

4. SOURCE: Relink symbolic links (enhanced version)

5. handling symbolic links

6. perl script to create symbolic links on alt.sources

7. -d following symbolic links

8. Going nutty trying to see if this SYMBOLIC link exists

9. slinky.pl -- a symbolic link walker

10. HELP: remove directory and symbolic link to directory

11. symbolic links are plain old files?

12. Reading symbolic links

 

 
Powered by phpBB® Forum Software