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); } }
In reply to recursive directory question by Aquilo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |