Aquilo has asked for the wisdom of the Perl Monks concerning the following question:
I *just* started using perl but I trying to learn. Explainations of what is wrong are more useful than simple fixing the problem for me.
I have seen many posts about proforming and function on every file in a directory but have seen none about working on the directory level.
I need a script that:
(UNIX only) Recurses through a directory structure and checks if more than half of the file in that directory have been used in the past 180 days. The path of directory which are predominately unused is apprended to a list of directory which will be used to archive them.
There area I'm most concerned with is my recurse functions itself. And if there is a more efficent way of doing this please explain that as well.
#!usr/bin/perl # Author: Ryan Scadlock # Date: 6 July 2002 use strict; my $path = './'; die "The file $base_file does not exist!\n" if (!-f $base_file); # Initiate the recursion &RecurseDirs($path); rm temp; print "The result can be found at Results file in this dir"; #### SUBROUTINES SECTION #### # Function that recurses through the directory tree sub RecurseDirs { my ($path) = @_; my $file; #Variable for a file foreach $path($path){ opendir (DIRECTORY, $path) || die "Can't read $path\n"; if (-d "$path$file/") { #If it's a directory... # Recurse again through this directory &RecurseDirs("$path$file/"); my $unused = 0; #Counter for how many files are accessed my $count = 0; #Counter for how many files are in dir # Count unused files in dir $path find -type f -atime +180 >temp.txt; $str = wc temp -l; $unused = int::substr($str, 0, 1); #returns the first characte +r as an int # Count files in dir $path find -type f >temp.txt; $str = wc temp -l; $count = int::substr($str, 0, 1); #returns the first character + as an int # Compare # Yes: write full dir path to file if (($unused * 2) > $count){ $path >>result.txt; } } closedir (DIRECTORY); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: recursive directory question
by merlyn (Sage) on Jul 08, 2002 at 04:38 UTC | |
by Fletch (Bishop) on Jul 08, 2002 at 11:34 UTC | |
by flounder99 (Friar) on Jul 08, 2002 at 12:09 UTC | |
by merlyn (Sage) on Jul 08, 2002 at 12:47 UTC | |
by flounder99 (Friar) on Jul 08, 2002 at 13:57 UTC | |
|
Re: recursive directory question
by jarich (Curate) on Jul 08, 2002 at 08:17 UTC | |
by Aristotle (Chancellor) on Jul 08, 2002 at 13:25 UTC | |
|
Re: recursive directory question
by Aquilo (Initiate) on Jul 08, 2002 at 16:31 UTC | |
by jarich (Curate) on Jul 09, 2002 at 01:02 UTC | |
by merlyn (Sage) on Jul 09, 2002 at 01:07 UTC | |
|
Re: recursive directory question
by gooch (Monk) on Sep 27, 2002 at 21:08 UTC |