John O
John O

Reputation: 5423

How can I check if a directory is empty?

I'm aware that expect is tcl-in-sheep's-clothing, but even when I google with that in mind I'm coming up empty.

Is there a way to determine if a particular directory is empty (or not empty, I can just use negation)?

Upvotes: 3

Views: 3924

Answers (2)

Daniel
Daniel

Reputation: 61

Use:

[glob -nocomplain -dir $dir *]

instead of

[glob -nocomplain "$dir/*"]

If you don't specify the directory in some cases your command will fail (like my Windows 8.1 with Tcl8.6). Specifying the directory the command works fine in Windows and Linux.

Upvotes: 0

Dinesh
Dinesh

Reputation: 16428

With glob, try searching for the pattern * in it.

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"
}

Upvotes: 5

Related Questions