list.files(path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = TRUE) dir(path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = TRUE) list.dirs(path = ".", full.names = TRUE, recursive = TRUE)
path | a character vector of path names. The default corresponds to the working directory returned by getwd(). You can specify more than one directory path. |
pattern | a character string containing a regular expression. If the basename of a file matches the regular expression, it is returned. See the regexpr function for details on regular expressions. By default, NULL, specifying returning all files found in path. |
all.files | a logical value. If TRUE, the entire contents of directory are returned. If FALSE (the default, only visible (non-hidden) file or directory names are returned. (Hidden files or directories are those with names that begin with a period.) |
full.names | a logical value. If TRUE, the directory path is prepended to the returned names. If FALSE (the default), only the file or directory name is in the return value. |
recursive | a logical value. If TRUE, the file names in all directory paths are listed recursively. When include.dirs=TRUE, the names of subdirectories are also returned. The default is FALSE. |
ignore.case | a logical value. If TRUE, uppercase and lowercase characters are considered equivalent when matching. The default is FALSE. |
include.dirs | a logical value. Only used when recursive=TRUE. If TRUE, both names of files and subdirectories are returned. Otherwise, only names of files are returned. |
no.. | a logical value. This argument is ignored. Unlike R, list.files and dir will not include the special directory entries "." and ".." in their lists of items in a directory. |
# Do a recursive listing of the TERR doc directory, including # both file and directory names dir(R.home("doc"), recursive = TRUE, include.dirs=TRUE)# Find possible TERR or R startup files dir(c("~", R.home("etc"), getwd()), pattern="Rprofile\\>", all.files=TRUE, full.names=TRUE)
# Find all directories called 'Meta' in the TERR library directory Meta.dirs <- list.dirs(R.home("library"), full.names=TRUE) Meta.dirs <- Meta.dirs[grep(pattern="^Meta$", basename(Meta.dirs))] head(Meta.dirs) # Find all '.rds' files in those Meta directories Meta.rds.files <- list.files(Meta.dirs, pattern="\\.rds", full.names=TRUE) head(Meta.rds.files)