chown

chown LISTThis function changes the owner and group of a list of files.
The first two elements of the list must be the
numeric UID and GID, in that order. A value of
–1 in either position is interpreted
by most systems to leave that value unchanged. The function returns the
number of files successfully changed. For example:
chown($uidnum, $gidnum, "file1", "file2") == 2
|| die "can't chown file1 || file2: $!";will set $cnt to 0, 1, or
2, depending on how many files got
changed (in the sense that the operation succeeded, not in the sense
that the owner was different afterward). Here’s a more typical
usage:
chown($uidnum, $gidnum, @filenames) == @filenames
|| die "can't chown @filenames: $!";Here’s a subroutine that accepts a username, looks up the user and
group IDs for you, and does the chown:
sub chown_by_name {
my($user, @files) = @_;
chown((getpwnam($user))[2,3], @files) == @files
|| die "can't chown @files: $!";
}
chown_by_name("fred", glob("*.c"));However, you may not want the group changed as the previous function does, because the /etc/passwd file associates each user with a single group even though that user may be a member of many secondary groups according to /etc/group ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access