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; find( 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;

In reply to Files owned in a filesystem by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.