Reputation: 1446
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
Reputation: 56249
Get filenames then extract directory name:
unique(dirname(list.files(full.names = TRUE, recursive = TRUE)))
Upvotes: 6
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
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