Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Files owned in a filesystem

by grinder (Bishop)
on May 31, 2002 at 14:58 UTC ( #170737=snippet: print w/replies, xml ) Need Help??
Description: Ever found yourself staring at a deep filesystem and wondering which users own files in it? This little snippet will grovel through it and display a list of users, and the number of files that they own.

An alternative would be to count the bytes occupied by the files owned by a given person. In this case you would want to accumulate (stat $_)[7] instead. You might also then want to pretty print the bytes.

update: changed use of stat to lstat as per Merlyn's excellent suggestion. As it turns out, I was looking at a Samba share, and ordinary lusers can't create symlinks. Still, it's a good habit to get into.

#! /usr/bin/perl -w

use strict;
use File::Find;

my %owned;
    sub { ++$owned{(lstat $_)[4]} unless $_ eq '.' or $_ eq '..' },
    shift || '.'

my %name;
$name{$_} = (getpwuid $_)[0] || "[uid $_]" foreach keys %owned;

print "$owned{$_}\t$name{$_}\n"
    foreach sort{ $name{$a} cmp $name{$b} }
    keys %owned;
Replies are listed 'Best First'.
•Re: Files owned in a filesystem
by merlyn (Sage) on May 31, 2002 at 15:50 UTC
    That should be lstat instead of stat, or else you'll count a symlinked file's size multiple times.

    And actually, you should use (lstat)[12], which accounts properly for holey files and indirect blocks, to give you a number like du instead of like adding up a bunch of ls -l values.

    -- Randal L. Schwartz, Perl hacker

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://170737]
[Discipulus]: ok.. where to look to serve a webpage composed by parts that fill themseves indipendently over time? no auto updates, just different times like (for a domain name) ip resolution, whois infos, lwp check ..

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-11-23 08:42 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (332 votes). Check out past polls.