如何检查目录是否为空?

How can I check if a directory is empty?

我知道 expect 是披着羊皮的 tcl,但即使我 google 想到这一点,我还是一头雾水。

有没有办法确定特定目录是否为空(或不为空,我可以使用否定)?

使用 glob,尝试在其中搜索模式 *

set dir "/home/dinesh/stack"
set file_list [glob -nocomplain "$dir/*"]
if {[llength $file_list] != 0} {
    puts "$dir is not empty"
} else {
    puts "$dir is empty"
}

使用:

[glob -nocomplain -dir $dir *]

而不是

[glob -nocomplain "$dir/*"]

如果您不指定目录,在某些情况下您的命令将失败(例如我的 Windows 8.1 和 Tcl8.6)。指定命令在 Windows 和 Linux.

中正常工作的目录