To list all files in a directory in R programming language we use list.files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories. If no files are present in the directory, it returns “”. If a path does not exist it is skipped and a warning is shown.
Note: File naming conventions depend on the platform.
Syntax:
list.files(path, pattern, all.files, full.names)
Parameter:
Path: It is a character vector that contains pathname to directories.
Pattern: It is an optional regular expression. If this argument is provided then the function only returns the files that have given pattern in their name.
all.files: It is a logical value.
- FALSE: Only the names of visible files are returned.
- TRUE: All file names will be returned irrespective of whether they are visible or hidden.
full.names: It is a logical value.
- TRUE: The filename also contains the full path to the directory.
- FALSE: The filename doesn’t contain the path to the directory.
Directory in use:

Directory used
Example 1:
R
list.files (path= "." , pattern= NULL , all.files= FALSE ,
full.names= FALSE )
|
Output:

Output
It is also possible to produce only some specific files for that the extension of those files is passed as the value to parameter pattern.
Example 2:
R
list.files (path= "." , pattern= ".pdf" , all.files= TRUE ,
full.names= TRUE )
|
Output:

Output
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Jun, 2021
Like Article
Save Article