Change ownership of files and directories with Perl script

I came across this method of changing the ownership of files when working on a DataStage startup/shutdown script and changing the permissions on the logfiles the script creates. So I wrote a simple script that takes a username from input and a file name or multiple names from input and changes the owner and group of that directory.

#!/usr/bin/perl
#chowner.pl
#
#change ownership of files
#written by trizsolo
#~~~~~~~~~~~~~~~~~~~~

print "User: ";
chomp($user = );
print "Files: ";
chomp($pattern = );
($login,$pass,$uid,$gid) = getpwnam($user)
or die "$user not in passwd file";
@ary = glob($pattern); # expand filenames
chown $uid, $gid, @ary;

Portable between Linux and Windows, or atleast should be. Not tested in Windows…

One Response to “Change ownership of files and directories with Perl script”

  1. By the way… usage is simple. “./chowner.pl” or perl -e chowner.pl then the script asks for user and then file or files… even directories.

Leave a Reply