1.目录句柄
从目录里获得文件名列表,可使用目录句柄。它不会启用另一个进程。
下面程序的运行结果包含目录中所有的文件,包括.和..文件,并且没有排序。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my $dir_to_process = "/etc";
opendir DH, $dir_to_process or die "Can not open $dir_to_process:$!";
foreach my $file (readdir DH){
print "one file in $dir_to_process is $file\n";
}
closedir DH;
去除所有以点开始的文件:
my $dir_to_process = "/etc";
opendir DH, $dir_to_process or die "Can not open $dir_to_process:$!";
foreach my $file (readdir DH){
next if $file =~ /^\./;
#next if $file eq "." or $file eq "..";//去除.和..文件
#next unless $file =~ /\.conf$/; 找出所有以.conf为结尾的文件
print "one file in $dir_to_process is $file\n";
}
closedir DH;
找出某个目录下的只读文件,并输出文件的全名,
my $dirname = "/etc";
opendir SOMEDIR, $dirname or die "Can not open $dirname:$!";
while (my $file = readdir SOMEDIR) {
next if $file =~ /^\./;
$file = "$dirname/$file";
next unless -f $file and -r $file;
print "$file\n";
}
closedir SOMEDIR;
2.perl使用unlink删除文件
unlink "bb", "cc", "dd";
unlink glob "*.conf";
3.重命名文件
rename "old" "new"
下面的程序是批量的把以.old结尾的文件命名为以.new结尾的文件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){
}else{
warn "rename $file to $newfile failed:$!\n";
}
}
4.链接与文件 link, symlink, unlink
link函数创建一个硬链接:
link "bb.new", "bbb" or warn "can not link bb.new to bbb:$!";
symlink函数创建一个符号链接:
symlink "cc.new", "ccc" or warn "can not symlink cc.new to
ccc:$!";
if (-l "ccc"){
print "ccc is a symlink.\n";
}elsif(-e "ccc"){
print "ccc is a file\n";
}else{
print "ccc is not existed.\n";
}
unlink可以删除上面的两种连接:
unlink "ccc", "bbb" or warn "ccc or bbb not exist:$!";
readlink:返回符号链接指向的位置,或在参数不是符号链接时返回undef。
my $where = readlink "ccc";
print "$where\n";
5.建立及移除目录mkdir and rmdir
建立目录调用mkdir函数:
mkdir "crl", 0755 or warn "can't make crl directory:$!";
oct函数:强行把字符串当成八进制数处理,无论它是否以零开头。
my ($name, $perm) = @ARGV;
mkdir $name, oct($perm) or die "can not create $name: $!";
...........
6.修改权限chmod(只能接受数字)
chmod 0755, "fred", "barney";
7.更改隶属关系
chown同时改变文件的拥有者和所属的组,但是用户标识和组标识必须以数字的形式来时定,如果不是数字的形式,则要用getpwnam将用户翻译成数字,getgrnam把组名翻译成数字:
my $user = 101;
my $group = 104;
chown $user, $group, "crl";
将/opt目录下所有的文件所属的用户名和组全部改为 apache和apache:
defined(my $user = getpwnam "apache") or die "bad user";
defined(my $group = getgrnam "apache") or die "bad group";
chown $user, $group, glob "/opt/*";
8.文件句柄
文件句柄:程序里代表进程与外界之间的I/O联系的名字。
open的用法:
open CONFIG, "dino";
open CONFIG, "<dino";
open CONFIG, ">dino";
open CONFIG, ">>dino";
perl5.6以后,open的另一种新的用法
open CONFIG, "<", "dino";
open CONFIG, ">", $file_name;
open CONFIG, ">>", &logfile_name();
作业题目:
用户键入一个目录名称,并从当前目录切换过去,如果输入的一行空白字符,则会以主目录作为默认目录,所以应切换到他本人的主目录中,切换后输出目录的内容(不含以点开头的文件)并按照英文字母顺序排列,若切换目录失败,则有相应的警告信息,但不必输出目录的内容。
print "which directory?\n";
chomp(my $dir = <>);
if ( $dir =~ /^\s*$/ ){
chdir or die "Can't chdir to your home directory:$!";
}else{
chdir $dir or die "can't change to $dir:$!\n";
}
my @files = <*>; #
my @files = <.* *>; 包含隐藏文件
foreach(sort @files){
print "$_\n";
}
运行结果:
/opt/script
1
7
aa.odd
bb
bb.new
bbb
cc.new
ccc
chai.pl
chapter10.pl
chapter13.pl
chapter14.pl
chapter15.pl
crl
dd.old
hash
hw.pl
learn.pl
shuzu.pl
使用文件句柄做上面的题目:
print "which directory?\n";
chomp(my $dir = <>);
if ( $dir =~ /^\s*$/ ){
chdir or die "Can't chdir to your home directory:$!";
}else{
chdir $dir or die "can't change to $dir:$!\n";
}
opendir DIRS, "." or die "can't open $dir:$!";
foreach (sort readdir DIRS){
#next if $_ =~ /^\./; 不打印以.开头的文件
print "$_\n";
}
编写类似于rm的功能的程序
......