user2380782
user2380782

Reputation: 1446

List non empty directories

Is there any easy way to list only directories that are not empty?

I am aware of list.dirs but I couldn't find the way to list only directories that are not empty.

Upvotes: 6

Views: 1064

Answers (3)

zx8754
zx8754

Reputation: 56249

Get filenames then extract directory name:

unique(dirname(list.files(full.names = TRUE, recursive = TRUE)))

Upvotes: 6

Megatron
Megatron

Reputation: 17099

Here's a one-liner solution:

nonempty <- list.dirs(recursive=F)[which(lengths(lapply(list.dirs(recursive=F), list.files)) > 0)]

Upvotes: 1

HubertL
HubertL

Reputation: 19544

You can use list.files on the result of list.dirs:

dirlist <- list.dirs("./R/R-3.3.1/library/zoo")
dirlist [sapply(dirlist, function(x) length(list.files(x))>0)]

Upvotes: 2

Related Questions