chown 函数用来更改文件的拥有者和所属组,使用 chown 时拥有者和组参数是以数字形式出现的,如:# ls -l *.txt
-rw-r--r-- 1 root root 0 Oct 12 13:29 test1.txt
-rw-r--r-- 1 root root 0 Oct 12 13:29 test2.txt
-rw-r--r-- 1 root root 0 Oct 12 13:29 test3.txt
-rw-r--r-- 1 root root 0 Oct 12 13:29 test4.txt
-rw-r--r-- 1 root root 0 Oct 12 13:29 test5.txt 运行下面程序:
[Perl] 纯文本查看 复制代码 #!/usr/bin/perl
my $user = 500;
my $group = 500;
chown $user, $group, glob "*.txt";
处理结果:ls -l *.txt
-rw-r--r-- 1 beyes beyes 0 Oct 12 13:29 test1.txt
-rw-r--r-- 1 beyes beyes 0 Oct 12 13:29 test2.txt
-rw-r--r-- 1 beyes beyes 0 Oct 12 13:29 test3.txt
-rw-r--r-- 1 beyes beyes 0 Oct 12 13:29 test4.txt
-rw-r--r-- 1 beyes beyes 0 Oct 12 13:29 test5.txt 记住数字毕竟不太容易,Perl 提供了 getpwnam 和 getgrnam 两个函数,它们分别用来将用户名和组名转换为相应的数字形式,这样就方便多了:
[Perl] 纯文本查看 复制代码 #!/usr/bin/perl
defined (my $user = getpwnam "beyes") or die "bad user";
defined (my $group = getgrnam "beyes") or die "bad group";
chown $user, $group, glob "*.txt";
|