Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to find the group ownership of a file on my web site, how do I do that? Also if it's wrong, I'd also need a way to be able to reset it.

I know file permissions, but I need to do group ownership.

2006-02-27 Retitled by g0n, as per Monastery guidelines
Original title: 'how to check chown of file'

Replies are listed 'Best First'.
Re: How to get group ownership of a file
by brian_d_foy (Abbot) on Feb 26, 2006 at 22:48 UTC

    The stat function can give you that information. Additionally, thanks to pileofrogs, Test::File has owner_is and owner_isnt function. Even if you don't want to use the module, you can look at the code. Those functions handle either the numeric UID or the name version. Although they are for users, the group stuff works in much the same way (and pileofrogs actually has the patch I need to apply to get the group equivalents.

    The stat and chown functions will need the numeric versions, which you can get with getgrnam if you have the name (although I'm not sure how this works on Windows.) The first argument to chown is the owner, and the second is the group.

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: How to get group ownership of a file
by ambrus (Abbot) on Feb 26, 2006 at 22:30 UTC

    You have to use the stat function to query group ownership and the chown function to change it.

      Actually, I meant of the directory, not the file.

      This doesn't seem to be right.

      #!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use warnings; #use strict; use CGI qw/:standard/; print header, start_html("test"); #print test"; my $filename = "/home/name/public_html/test/"; ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename); print "permissions for $filename are..<p>"; print <<"END"; dev $dev <br> ino $ino <br> mode $mode <br> nlink $nlink <br> uid $uid <br> gid $gid <br> rdev $rdev <br> size $size <br> atime $atime <br> mtime $mtime <br> ctime $ctime <br> blk size $blksize <br> blocks $blocks END
      As it returns
      permissions for /home/name/public_html/test/ are.. dev 2053 ino 6916114 mode 16877 nlink 7 uid 33197 gid 33200 rdev 0 size 4096 atime 1140992338 mtime 1140911929 ctime 1140911929 blk size 4096 blocks 8
      It can't be all numbers, right? the group id should be a name of sorts.
        It can't be all numbers, right?

        On the contrary (for unix-like systems, at least), the uid and gid are always numeric. To get the name (string) associated with a given numeric gid value, you need to grep for that number in /etc/groups (and to get the user login name for a given uid, you look up the uid in /etc/passwd).

Re: How to get group ownership of a file
by spiritway (Vicar) on Feb 26, 2006 at 23:00 UTC

    First, have you tried reading the documentation? Check out chown.

    If you've got access to your Website's file system (and if it's Linux or Unix), then type ls -l to display a listing that includes owner and group information.