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

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-14 13:04:31

remove files;
  1. unlink "slate", "bedrock", "lava";
Now, since unlink takes a list, and the glob function returns a list, we can combine th
two to delete many files at once:
  1. unlink glob "*.o";
The return value from unlink tells us how many files have been successfully deleted.
So, going back to the first example, we can check its success:
  1. my $successful = unlink "slate", "bedrock", "lava";
  2. print "I deleted $successful file(s) just now\n";
in order to find which files are deleted successfully.
  1. foreach my $file (qw(slate bedrock lava)) {
  2.   unlink $file or warn "failed on $file: $!\n";
  3. }
Now, here’s a little-known Unix fact. It turns out that you can have a file that you can’t
read, you can’t write, you can’t execute, maybe you don’t even own the file—that is,
it’s somebody else’s file altogether—but you can still delete it. That’s because the
permission to unlink a file doesn’t depend upon the permission bits on the file itself;
it’s the permission bits on the directory that contains the file that matters.

  1. rename "old", "new";
  2. rename "over_there/some/place/some_file", "some_file"; #move file around


  3. foreach my $file (glob "*.old") {
  4.   my $newfile = $file;
  5.   $newfile =~ s/\.old$/.new/;
  6.   if (-e $newfile) {
  7.     warn "can't rename $file to $newfile: $newfile exists\n";
  8.   } elsif (rename $file, $newfile) {
  9.     ## success, do nothing
  10.   } else {
  11.     warn "rename $file to $newfile failed: $!\n";
  12.   }
  13. }
To find out where a symbolic link is pointing, use the readlink function. This will tell
you where the symlink leads, or it will return undef if its argument wasn’t a symlink:
  1. my $where = readlink "carroll"; # Gives "dodgson"
  2. my $perl = readlink "/usr/local/bin/perl"; # Maybe tells where perl is



阅读(880) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~