最近,看到redis自己的测试代码,于是简单了解一下,总结一下file命令的简单实用,不废话,上代码
#!/usr/bin/tclsh
set path $argv0
# atime: 以UNIX时间戳形式,返回/设置文件的访问时间
puts "atime: [file atime $path]"
# 返回/设置文件属性信息
puts "attributes: [file attributes $path]"
# 返回单个文件属性信息
puts "attibutes, permission: [file attributes $path -permission]"
# 设置单个文件属性
set ret [file attributes $path -permission 00777]
puts "attibutes, set permission: [file attributes $path -permission]"
# 查看文件信息
unset ret
file stat $path ret
puts "file stat: "
parray ret
# 删除文件
file delete a.txt
puts "del file, name: a.txt"
# 复制文件
file copy $path a.txt
puts "copy file, src: $path, dst: a.txt"
# 复制文件,但是文件已存在则覆盖
file copy -force $path a.txt
puts "force copy file, src: $path, dst: a.txt"
# 连续--用于预设参数部分结束,后续全是文件名,用于文件名以-开头的字符串
file copy -force $path -a.txt
file copy -force -- -a.txt xxx.txt
# 删除文件/目录
file delete -force -- a.txt -a.txt xxx.txt
puts "delete file"
# 级联创建目录
file mkdir X/XX/XXX
puts "mkdir X/XX/XXX"
# 返回原生路径,不识别文件是否存存在
unset ret
set ret [file nativename ~/ok/fish.txt]
puts "native, source: ~/ok/fish.txt, native: $ret"
# 返回文件绝对路径,唯一标准化路径
unset ret
set ret [file normalize $path]
puts "normalize, source: $argv0, native: $ret"
# 返回目录名称
unset ret
set ret [file dirname [file normalize $path]]
puts "dirname, path: [file normalize $path], dir: $ret"
# 验证文件是否可执行
puts "path exec? path: $path, exec: [file executable $path]"
# 验证文件是否存在
puts "path exists? path: $path, exist: [file exists $path]"
# 返回文件扩展名
puts "path extension, path: $path, extension: [file extension $path]"
# 检查是否是目录
set dir [file dirname [file normalize $path]]
puts "is dir? path: $dir, isdir: [file isdirectory $dir]"
# 创建链接
set linkfile file.ln
file delete -force $linkfile
file link -symbolic $linkfile $path
puts "create link, src: $path, dst: $linkfile, return link source file: [file readlink $linkfile]"
# 符号链接信息,链接本身,非链接指向的文件
unset ret
set linkfile file.ln
file lstat $linkfile ret
puts "symbol: "
parray ret
# 多个字符串串成路径
puts "join str: [file join xxx yyy zzz ddd wu]"
# 判定是否是文件
puts "is file: [file isfile $path]"
# 返回通道
puts "channels: [file channels], match channels: [file channels "*err"]"
# 创建具有临时读写权限的文件, 当前不支持
# unset ret
# file tempfile ret "fish.tmp"
# puts "temp file: $ret"
# 返回修改时间
puts "path: $path, mtime: [file mtime $path]"
# 文件大小,单位: B
puts "path: $path, size: [file size $path]"
# 文件系统
puts "path: $path, file system: [file system $path]"
# 是否文件所有者
puts "path: $path, file owned: [file owned $path]"
# 相对路径、绝对路径、volumerelative判断
puts "path: $path, abs: [file normalize $path], re: [file pathtype $path], abs: [file pathtype [file normalize $path]]"
# 重命名、移动、同名覆盖
# file rename X Y
# 返回rootname,仅仅去掉后缀名,意义在哪?
puts "rootname, path: $path, rootname: [file rootname [file normalize $path]]"
# 拆分路径
puts "split path, path: [file normalize $path], split: [file split [file normalize $path]]"
# 路径最后一个部分
puts "path tail, path: [file normalize $path], tail: [file tail [file normalize $path]]"
# 路径分隔符
puts "file separator: [file separator]"
# 卷
puts "volumes: [file volumes]"
# 类型, 还有可能是socket
puts "type, [file type $path]"
# 可读,可写权限
puts "path: $path, read: [file readable $path], writable: [file writable $path]"
执行结果
fh@Feihu-3 learn % ./run.tcl
atime: 1636212405
attributes: -group staff -owner fh -permissions 00777 -readonly 0 -creator {} -type {} -hidden 0 -rsrclength 0
attibutes, permission: 00777
attibutes, set permission: 00777
file stat:
ret(atime) = 1636212405
ret(blksize) = 4096
ret(blocks) = 8
ret(ctime) = 1636212604
ret(dev) = 16777222
ret(gid) = 20
ret(ino) = 22849742
ret(mode) = 33279
ret(mtime) = 1636212403
ret(nlink) = 1
ret(size) = 3815
ret(type) = file
ret(uid) = 501
del file, name: a.txt
copy file, src: ./run.tcl, dst: a.txt
force copy file, src: ./run.tcl, dst: a.txt
delete file
mkdir X/XX/XXX
native, source: ~/ok/fish.txt, native: /Users/fh/ok/fish.txt
normalize, source: ./run.tcl, native: /Users/fh/fh_data/workspace/tcl/learn/run.tcl
dirname, path: /Users/fh/fh_data/workspace/tcl/learn/run.tcl, dir: /Users/fh/fh_data/workspace/tcl/learn
path exec? path: ./run.tcl, exec: 1
path exists? path: ./run.tcl, exist: 1
path extension, path: ./run.tcl, extension: .tcl
is dir? path: /Users/fh/fh_data/workspace/tcl/learn, isdir: 1
create link, src: ./run.tcl, dst: file.ln, return link source file: ./run.tcl
symbol:
ret(atime) = 1636212604
ret(blksize) = 4096
ret(blocks) = 0
ret(ctime) = 1636212604
ret(dev) = 16777222
ret(gid) = 20
ret(ino) = 22858687
ret(mode) = 41453
ret(mtime) = 1636212604
ret(nlink) = 1
ret(size) = 9
ret(type) = link
ret(uid) = 501
join str: xxx/yyy/zzz/ddd/wu
is file: 1
channels: stdin stdout stderr, match channels: stderr
path: ./run.tcl, mtime: 1636212403
path: ./run.tcl, size: 3815
path: ./run.tcl, file system: native
path: ./run.tcl, file owned: 1
path: ./run.tcl, abs: /Users/fh/fh_data/workspace/tcl/learn/run.tcl, re: relative, abs: absolute
rootname, path: ./run.tcl, rootname: /Users/fh/fh_data/workspace/tcl/learn/run
split path, path: /Users/fh/fh_data/workspace/tcl/learn/run.tcl, split: / Users fh fh_data workspace tcl learn run.tcl
path tail, path: /Users/fh/fh_data/workspace/tcl/learn/run.tcl, tail: run.tcl
file separator: /
volumes: /
type, file
path: ./run.tcl, read: 1, writable: 1
fh@Feihu-3 learn %