remove files;
- 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:
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:
- my $successful = unlink "slate", "bedrock", "lava";
-
print "I deleted $successful file(s) just now\n";
in order to find which files are deleted successfully.
- foreach my $file (qw(slate bedrock lava)) {
-
unlink $file or warn "failed on $file: $!\n";
-
}
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.- rename "old", "new";
-
rename "over_there/some/place/some_file", "some_file"; #move file around
-
-
-
foreach my $file (glob "*.old") {
-
my $newfile = $file;
-
$newfile =~ s/\.old$/.new/;
-
if (-e $newfile) {
-
warn "can't rename $file to $newfile: $newfile exists\n";
-
} elsif (rename $file, $newfile) {
-
## success, do nothing
-
} else {
-
warn "rename $file to $newfile failed: $!\n";
- }
- }
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:
- my $where = readlink "carroll"; # Gives "dodgson"
-
my $perl = readlink "/usr/local/bin/perl"; # Maybe tells where perl is
阅读(880) | 评论(0) | 转发(0) |