Re: Help with scripting
On Thu, Apr 07, 2005 at 04:01:00PM +0000, Joe wrote:
>
> /fix1/user1/fix2/user2/fix3
>
> where fix1 and fix3 are the same for all users, and fix2 can take 2
> predetermined fixed values. User1 is the user name and user2 is a name
> the user determines (there can be unlimited numbers of these).
>
> I want to write some sort of a script that will work out the number
> of user2 directories each user has, and the number of files each user
> has. Files are only stored at the lowest directory level, ie, fix3.
Yet another suggestion:
#!/usr/bin/perl
for (glob "/fix1/*/*/*/fix3") {
my $u = (split /\//)[1];
$dat{"$u dirs"}++; # count dirs
$dat{"$u files"} += `ls -1 $_ | wc -l`; # sum up number of files
$dat{"$u kb used"} += `du -sk $_`; # sum up disk usage
}
for my $u (sort keys %dat) {
printf "%-20s : %s\n", $u, $dat{$u};
}
...would print a summary list of the following kind:
user1 dirs : 5
user1 files : 18
user1 kb used : 20
user2 dirs : 6
user2 files : 11
user2 kb used : 24
...
You can of course remove the disk usage summary -- this is just meant
to show how easily you could extend the script to collect other info,
if needed...
Note 1: the glob expression assumes that at the level of the second '*',
there are only your two predetermined fixed values. In case there are
others dirs as well at this level, you'd need to modify the expression
accordingly, to only expand to the values you're interested in, e.g.
"fix1/*/fix2[AB]/*/fix3", if your predetermined values were "fix2A" and
"fix2B".
Note 2: only entries directly under fix3 will be counted -- in case
this is a recursive structure with subdirs, you'd have to change the
`ls -1 $_ | wc -l` expression to extract whatever else you need.
Cheers,
Almut
Reply to: