本章将介绍 Expect 语法中 file 的相关操作。
判断
exists
| 1 | puts [file exists /usr/local/etc] # 1 | 
type
file type name 可以获取文件类型共 file, directory, characterSpecial, blockSpecial, fifo, link, socket 几种类型
| 1 | puts [file type /usr/local/etc] # directory | 
isdirectory
| 1 | puts [file isdirectory /usr/local/etc] # 1 | 
isfile
这里有一点需要注意,除了 type == file 全都会返回 0
| 1 | puts [file isfile /usr/local/etc/redis.conf] # 1 | 
判断
| 1 | if {[file isdirectory $f]} { | 
操作
link
使用 file link -link_type link_name target_file
-link_type 有两种参数软连接 -symbolic 和硬链接 -hard,当 link_name 已经存在或者 target_file 不存在时就会报错
软连接
| 1 | sh> ln -s target_file link_name | 
硬链接
| 1 | sh> ln target_file link_name | 
查看软连接的连接对象
| 1 | file readlink link_name | 
join
使用当前系统的路径分隔符将一个或多个相对目录文件名连接起来,如果有绝对路径,则前边的参数将会被舍弃掉
| 1 | puts [file join a b c] # a/b/c | 
delete
| 1 | file delete /usr/local/test/redis.conf | 
copy
用法 file copy -force source target
如果复制的是软连接文件,那么复制的也只是链接文件,而不是原文件
| 1 | sh> ll | 
覆盖目标
| 1 | file copy -force file1 file2 | 
复制多个文件目录中
| 1 | file copy file1 file2 dir1 | 
复制目录
| 1 | file copy dir1 dir2 | 
这里需要注意的是,如果 dir2 已经存在,那么 dir1 将会复制到 dir2 目录中,而在此执行则会报错,应该 dir2 中已经包含 dir1,此时即使有 -force 参数也不行
rename
相当于 sh 中的 mv,用法
| 1 | file rename ?-force? ?--? source target | 
使用
| 1 | sh> ll | 
覆盖
| 1 | sh> ll | 
移动到目录 dir1
| 1 | file rename host1 host2 dir1 | 
查看
dirname
获取最后一个路径分隔符以前的路径
| 1 | puts [file dirname ~/data/test] # ~/data | 
tail
获取最后一个路径分隔符以后的路径
| 1 | puts [file dirname ~/data/test] # test | 
rootname
查看文件名,去掉后缀
| 1 | puts [file rootname do.sh] # do | 
extension
获取文件名后缀,带有 .
| 1 | puts [file extension do.sh] # .sh | 
separator
获取系统分隔符
| 1 | put [file separator] # / | 
split
分割路径
| 1 | puts [file split ~/data] # ~ data | 
size
获取文件大小,单位 Bytes
| 1 | puts [file size host1] # 279 | 
atime
查看文件最后查看时间的时间戳
| 1 | puts [file atime host1] # 1527757243 | 
mtime
查看文件最后修改时间的时间戳
| 1 | puts [file mtime host1] # 1527757243 | 
 
             
		 
                      