linux判断目录是否为空_Perl学习09之文件目录操作
本文介绍Perl语言中文件和目录操作;File::Basename模块;stats函数。目录欢迎微信随缘关注@pythonic生物人1、文件目录操作常用操作符2、stat函数3、文件目录常用操作创建文件删除文件创建目录删除目录切换目录File::Basename模块操控路径1、文件目录操作常用操作符-s 判断文件存在且非空返回1;-e -s 均能判断文件,目录是否存在,存在返...
本文介绍Perl语言中文件和目录操作;
File::Basename模块;
stats函数。
目录
欢迎微信随缘关注@pythonic生物人
1、文件目录操作常用操作符
2、stat函数
3、文件目录常用操作
创建文件
删除文件
创建目录
删除目录
切换目录
File::Basename模块操控路径
1、文件目录操作常用操作符
-s 判断文件存在且非空返回1;
-e -s 均能判断文件,目录是否存在,存在返回1;
-l 文件是软链接,返回1;
-d 是目录返回1;
-z 文件为空返回1;
-M 最后一次修改时间;
-T 最后一次访问时间;
例如,es.pl
#!/usr/bin/perl
use strict;
use warnings;
my $filesize= -s "./BGI_basicperl";#该目录不为空,返回目录大小字节
print "$filesizen";
#-s 判断文件存在且不为空返回1,否则返回0
print "file existsn" if (-s "./a.txt");#文件存在返回1
print "empty filen" if (-s "./hha.test");#文件不存在返回0
print "file not existn" if (-s "./hha.test1");#文件不存在返回0
#-s 判断目录存在,无论空或者不空均返回1,不能判断目录是否为空
print "directory existsn" if (-s "./BGI_basicperl");#目录存在非空返回1
print "zerosize directoryn" if (-s "./estest");#空目录存在返回1
print "directory not existn" if (-s "./BGI_basicperl1");#目录不存在返回0
#-e 判断文件存在,无论空或者不空均返回1,不能判断文件是否为空
print "file existsn" if (-e "./a.txt");#文件存在返回1
print "empty file existsn" if (-e "./hha.test");#空文件存在返回1
print "file not existn" if (-e "./a.txt1");#文件不存在返0
#-e 判断目录存在,无论空或者不空均返回1,不能判断目录是否为空
print "directory existsn" if (-e "./BGI_basicperl");#目录存在非空返回1
print "zerosize directoryn" if (-e "./estest");#空目录存在返回1
print "directory not existn" if (-e "./BGI_basicperl1");#目录不存在返回0
#综上
#-s 判断文件存在且非空返回1;
#-e -s 均能判断文件,目录是否存在,存在返回1
#-l 文件是软链接,返回1
print "a ln irectoryn" if (-l "./BGI_basicperl");
#-d 是目录返回1
print "a directoryn" if (-d "./BGI_basicperl");
#-z 文件为空返回1
print "a file existsn" if (-z "./a.txt");#非空文件返回0
print "empty file existsn" if (-z "./hha.test");#空文件存在返回1
#-M 最后一次修改时间
my $last_c_days=-M "./a.txt";
#-T 最后一次访问时间
my $last_r_days=-T "./a.txt";
print "$last_c_dayst$last_r_daysn";
perl es.pl
4096
file exists
directory exists
zerosize directory
file exists
empty file exists
directory exists
zerosize directory
a ln irectory
a directory
empty file exists
1.11414351851852
2、stat函数
返回文件的13项属性信息。例如, stat.pl
#!/usr/bin/perl
use strict;
use warnings;
my @arr_stat=stat($ARGV[0]);
my @name_stat=qw/$dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
$size, $atime, $mtime, $ctime, $blksize, $blocks/;
foreach my $i (0..$#arr_stat){
print "$name_stat[$i]t$arr_stat[$i]n"}
perl stat.pl substr1.pl
Possible attempt to separate words with commas at stat.pl line 7.
$dev, 2113 #
$ino, 151030016306
$mode, 33204
$nlink, 1
$uid, 1024
$gid, 1024
$rdev, 0
$size, 269
$atime, 1586604409
$mtime, 1586604405
$ctime, 1586604405
$blksize, 4096
$blocks 8
每一行详细意义参考小骆驼书:
3、文件目录常用操作
直接可以和linux交互调用linux命令
创建文件
#方法一,调用linux命令
`touch creat.txt`;
#方法二,open打开文件句柄和输出创建文件
open OUT,">>","./creat1.txt";
print OUT "test";
close OUT;
删除文件
unlink "./creat.txt";
unlink ("./creat1.txt","./hha.test");
创建目录
`mkdir test_dir`;
`mkdir -p test_dir/test`;
删除目录
`rm -rf ./11`;
`rm -rf ./test_dir`;
切换目录
chdir "./";
File::Basename模块操控路径
例如,一个文件绝对路径,/home/littleperl/max.pl,可轻松获取如下内容例,path.pl
#! /usr/bin/perl
use strict;
use warnings;
use File::Basename;
my $path = $ARGV[0];
my($filename, $dirs, $suffix) = fileparse($path,qr/.[^.]*/);
#以上分别为文件名,文件绝对路径,文件后缀名
print"$filenamet$dirst$suffixn";
my $filename1=basename($path);#默认返回带后缀文件名
my $dirname1=dirname($path);#返回绝对路径
print "$filename1t$dirname1n";
perl path.pl /home/study/max.pl
max /home/study/ .pl
max.pl /home/study
欢迎微信随缘关注@pythonic生物人
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)