Chinaunix首页 | 论坛 | 博客
  • 博客访问: 855434
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-14 14:21:16

  1. mkdir "test", 0755 or warn"$!";
use the oct function, which forces octal interpretation
of a string whether or not there’s a leading zero:
mkdir $name, oct($permissions);

The need for the extra oct function shows up most
often when the value comes from user input. For example, suppose we take the argu-
ments from the command line:
  1. my ($name, $perm) = @ARGV; # first two args are name, permissions
  2. mkdir $name, oct($perm) or die "cannot create $name: $!";
To remove empty directories, use the  rmdir function in a manner similar to the
unlink function, although it can only remove on directory per call:
  1. foreach my $dir (qw(fred barney betty)) {
  2.   rmdir $dir or warn "cannot rmdir $dir: $!\n";
  3. }
The rmdir operator fails for nonempty directories. As a first pass, you can attempt to
delete the contents of the directory with unlink, then try to remove what should now
be an empty directory.
  1. my $temp_dir = "/tmp/scratch_$$"; # based on process ID; see the text
  2. mkdir $temp_dir, 0700 or die "cannot create $temp_dir: $!";
  3. ...
  4. # use $temp_dir as location of all temporary files
  5. ...
  6. unlink glob "$temp_dir/* $temp_dir/.*"; # delete contents of $temp_dir
  7. rmdir $temp_dir; # delete now-empty directory
 For a more robust solution, check out the rmtree function
provided by the File::Path module of the standard distribution.

chmod 0755, "fred", "barney";
As with many of the operating system interface functions, chmod returns the number of
items successfully altered, and when used with a single argument, sets $! in a sensible
way for error messages when it fails.

If the operating system permits it, you may change the ownership and group member-
ship of a list of files (or filehandles) with the chown function. The user and group are
both changed at once, and both have to be the numeric user-ID and group-ID values.
For example:
my $user = 1004;
my $group = 100;
chown $user, $group, glob "*.o";
What if you have a username like merlyn instead of the number? Simple. Just call the
getpwnam function to translate the name into a number, and the corresponding
getgrnam† to translate the group name into its number:
  1. defined(my $user = getpwnam "merlyn") or die "bad user";
  2. defined(my $group = getgrnam "users") or die "bad group";
  3. chown $user, $group, glob "/home/merlyn/*";

The defined function verifies that the return value is not undef, which will be returned
if the requested user or group is not valid.
The chown function returns the number of files affected, and it sets $! on error.
阅读(660) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~