R: get list of files but not of directories

The fact that base R does not have a direct method to do this is somewhat appalling. The fact that BASH doesn't have a direct way is also a bit odd.

In my opinion, the best R solution is to simply appeal to the shell:

filenames = system('ls -p | grep -v /', intern=T)

Explanation:

ls -p     Append "/" to end of directory names
grep -v   Exclude strings matching "/"
intern=T  store the output in the variable rather then printing to stdout

Here's one possibility:

all.files <- list.files(rec=F)
all.files[!file.info(all.files)$isdir]

Another option (pattern for files with extensions, not so universal, of course):

Sys.glob("*.*")

setdiff(list.files(), list.dirs(recursive = FALSE, full.names = FALSE))

will do the trick.

Tags:

List

File

R