List of non-empty directories

Is there an easy way to list only non-empty directories?

I know list.dirs , but I could not find a way to list only non-empty directories.

+6
source share
3 answers

You can use list.files for 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)] 
+1
source

Get the file names, then extract the directory name:

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

Here's a one line solution:

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

Source: https://habr.com/ru/post/1012190/


All Articles