List the Files in a Directory/Folder

These functions produce a character vector of the names of files or directories in the named directory.

list.files(path = ".", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = FALSE,
  = FALSE, include.dirs = FALSE, no.. = FALSE)

dir(path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, recursive = FALSE, = FALSE, include.dirs = FALSE, no.. = FALSE)

list.dirs(path = ".", full.names = TRUE, recursive = TRUE)


a character vector of full path names; the default corresponds to the working directory, getwd(). Tilde expansion (see path.expand) is performed. Missing values will be ignored.


an optional regular expression. Only file names which match the regular expression will be returned.


a logical value. If FALSE, only the names of visible files are returned. If TRUE, all file names will be returned.


a logical value. If TRUE, the directory path is prepended to the file names to give a relative file path. If FALSE, the file names (rather than paths) are returned.


logical. Should the listing recurse into directories?

logical. Should pattern-matching be case-insensitive?


logical. Should subdirectory names be included in recursive listings? (They always are in non-recursive ones).


logical. Should both "." and ".." be excluded also from non-recursive listings?


A character vector containing the names of the files in the specified directories, or "" if there were no files. If a path does not exist or is not a directory or is unreadable it is skipped, with a warning.

The files are sorted in alphabetical order, on the full path if full.names = TRUE.

list.dirs implicitly has all.files = TRUE, and if recursive = TRUE, the answer includes path itself (provided it is a readable directory).


File naming conventions are platform dependent. The pattern matching works with the case of file names as returned by the OS.

On a POSIX filesystem recursive listings will follow symbolic links to directories.

path must specify paths which can be represented in the current codepage, and files/directories below path whose names cannot be represented in that codepage will most likely not be found.

See Also, file.access and files for many more file handling functions and file.choose and choose.files for interactive selection.

glob2rx to convert wildcards (as used by system file commands and shells) to regular expressions.

Sys.glob for wildcard expansion on file paths.

  • list.files
  • dir
  • list.dirs
library(base) # NOT RUN { list.files(R.home()) ## Only files starting with a-l or r ## Note that a-l is locale-dependent, but using case-insensitive ## matching makes it unambiguous in English locales dir("../..", pattern = "^[a-lr]", full.names = TRUE, = TRUE) list.dirs(R.home("doc")) list.dirs(R.home("doc"), full.names = FALSE) # }
Documentation reproduced from package base, version 3.5.1, License: Part of R 3.5.1

Community examples

Looks like there are no examples yet.