1、創建文件:
open(IN, ">", "test_file.txt");
#或者
`touch test_file.txt`;
2、刪除文件:
unlink "test_file.txt"
#如果想删除目录内的文件和子目录,而保留foo1目录自身,应该设置keep_root选项
rmtree '/test/foo1',{verbose => 1,keep_root => 1};
3、創建目錄:
use File::Path qw(make_path);
make_path "/test/foo/bar"; # 一次性创建3级目录
make_path "/test/foo1/bar1", {
chmod => 0777,
verbose => 1 }
#或者
`mkdir -p /test/a/b/c`;
4、刪除目錄:
use File::Path qw(rmtree);
rmtree '/test/foo1',{verbose => 1}; #顯示刪除信息
#或者
rmtree((glob '/test/foo1/*'),{verbose => 1});
#或者
`rm -rf /test/foo1/*`;
5、切換目錄
chdir #用于切换到指定目录,如果不给参数,则回到HOME
6、獲取當前補錄
use Cwd(getcwd,cwd);
print getcwd();
print cwd();
7、修改文件、目錄的權限
#chmod函数修改文件、文件列表的权限值,它会直接发起系统调用,所以错误的话会设置$!
#只接受8进制的权限值,不允许使用rwx的权限字符
#它返回成功设置权限的数量
chmod 0700,qw(/test/foo /test/foo1/a.log);
chmod 0700,'/test/foo','/test/foo1/a.log';
8、修改文件的時間戳屬性
#在unix系统中,要求操作系统维护atime/mtime/ctime三种文件的时间戳属性:
#atime:access time,文件最近一次被访问时的时间戳
#mtime:modify time,文件内容最近一次被修改时的时间戳
#ctime:inode change time,文件inode数据最近一次被修改的时间戳
utime(Atime,Mtime,FILE_LIST)
#Atime和Mtime可以同时定义为undef,表示修改为当前时间
#实现touch文件的等同功能:将文件时间戳设置为当前时间:
utime undef,undef,'a.txt'
or die "touch file failed: $!";
#如果只想修改atime,不想修改mtime,则使用stat函数先将当前的mtime属性值取出保存下来:
$mtime = (stat('a.txt'))[9];
utime time,$mtime,'a.txt'
or die "touch failed: $!";