perl使用一例
#!/usr/bin/perl
################################################################
#目标: 将机器A的/export/data目录结构完全移植到机器B
# 要求目录所有者(owner)和访问权限(permission)不变
#方案:1.shell编程 利用find命令可以列出目录结构,find . -type d
# 但是如果想建立所有者和权限都不变的目录结构,则不太容易
# 2.编写perl脚本,递归调用获得目录层次,
# 对每个目录生成以下shell命令
# mkdir $dir
# chmod $mode $dir
# chown owner:group $dir
# 其中,调用File::stat函数得到有关所有者和权限
# 3.更好的方法:征求中(???)
###############################################################
#使用方法: copydir.pl [你要复制的目录的名称(不要用全路径)]
# 例如你要复制/export/data/log下的目录结构
# cd到/export/data
# 运行 copydir.pl log > createdir.sh
# 调目标机器运行createdir.sh
###############################################################
use File::stat;
$ARGV[0]="." unless defined($ARGV[0]);
downward(@ARGV);
sub downward {
my ($file, @list);
FILE: while (defined ($file = shift(@_))) {
unless( opendir(DIR, $file) ) {
warn "$Me: can't opendir $file:$!\n";
next FILE;
}
@list = ();
for ( readdir(DIR) ){
if (-d "$file/$_" && not(/^\.{1,2}/)) {
$sb = stat($file);
print "mkdir $file/$_\n";
printf "chmod %04o %s\n", $sb->mode&07777,"$file/$_";
printf "chown %s:%s %s\n",$sb->uid,$sb->gid,"$file/$_";
push(@list, "$file/$_");
}
}
closedir(DIR);
downward(@list);
}
}