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

Greetings Monks,
I need to know the owner of the file in unix machine. I am using perl version 5.00503 and hp unix. Is thr any specific function to find it out.
Thanks in advance
-prasanna.k
  • Comment on how to know the owner of the file in unix

Replies are listed 'Best First'.
Re: how to know the owner of the file in unix
by Roger (Parson) on Aug 29, 2005 at 12:27 UTC
    You can use the stat function to retrieve the uid of the file.

    perldoc -f stat stat FILEHANDLE stat EXPR stat Returns a 13-element list giving the status info for a file, either the file opened via FILEHANDLE, or named by EXPR. If EXPR is omitted, it stats $_. Returns a null list if the stat fails. Typically used as follows: ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename); Not all fields are supported on all filesystem types. Here are the meanings of the fields: 0 dev device number of filesystem 1 ino inode number 2 mode file mode (type and permissions) 3 nlink number of (hard) links to the file 4 uid numeric user ID of file's owner 5 gid numeric group ID of file's owner 6 rdev the device identifier (special files only) 7 size total size of file, in bytes 8 atime last access time in seconds since the epoch 9 mtime last modify time in seconds since the epoch 10 ctime inode change time in seconds since the epoch + (*) 11 blksize preferred block size for file system I/O 12 blocks actual number of blocks allocated


    Example:
    open MYFILE, '<filename' or die "Can not open: $!"; my $uid = (stat MYFILE)[4]; close MYFILE; print "UID = $uid\n";
Re: how to know the owner of the file in unix
by polettix (Vicar) on Aug 29, 2005 at 12:41 UTC
    In addition to the stat function suggested by Roger above, you could also be interested in the getpwuid function to translate the uid into the associated info (e.g. the login name of the user).

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.