Table of Contents
The list.files() function in R is a useful tool for accessing and manipulating files within a specified directory. To use this function, first specify the directory you want to work with using the “path” parameter. Then, you can use additional parameters such as “pattern” to filter the files based on a specific pattern or “full.names” to include the full file path in the results. The output of the list.files() function is a character vector containing the names of the files in the specified directory. This function can be helpful for tasks such as importing multiple files into R for analysis.
Use list.files() Function in R (4 Examples)
You can use the list.files() function in R to list out every file in a specific folder.
The following examples show how to use this function in different scenarios with a folder called my_data_files that contains three CSV files and two TXT files:

Example 1: List All Files in Directory
We can use the following syntax to list out every file in this folder:
#display all files in my_data_files folder list.files('C:/Users/bob/Documents/my_data_files') [1] "df1.csv" "df2.csv" "df3.csv" "more_data.txt" "some_data.txt"
We can see the names of all five files in this folder.
We could also use the length() function if we just wanted to know how many files were in the folder:
#display total number of files in my_data_files folder length(list.files('C:/Users/bob/Documents/my_data_files')) [1] 5
Example 2: List First N Files in Directory
We can use the following syntax to list out just the first three files in this folder:
#display first three files in my_data_files folder list.files('C:/Users/bob/Documents/my_data_files')[1:3] [1] "df1.csv" "df2.csv" "df3.csv"
We can see the names of just the first three files in this folder.
Example 3: List All Files in Directory with Specific Extension
We can also use the pattern argument to only list the files that have a specific extension:
#display all files with CSV extension in my_data_files folder list.files('C:/Users/bob/Documents/my_data_files', pattern='csv') [1] "df1.csv" "df2.csv" "df3.csv"
We can see all three files that have a .csv extension.
Example 4: List All Files in Directory that Contain String
We can also use the pattern argument to only list files that contain a certain string:
#display all files that contain 'data' in file name list.files('C:/Users/bob/Documents/my_data_files', pattern='data') [1] "more_data.txt" "some_data.txt"
We can see the two files that both contain ‘data’ in the file name.
Additional Resources
The following tutorials explain how to perform other common tasks in R:
Cite this article
stats writer (2024). How to use the list.files() function in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-use-the-list-files-function-in-r/
stats writer. "How to use the list.files() function in R?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-to-use-the-list-files-function-in-r/.
stats writer. "How to use the list.files() function in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-to-use-the-list-files-function-in-r/.
stats writer (2024) 'How to use the list.files() function in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-use-the-list-files-function-in-r/.
[1] stats writer, "How to use the list.files() function in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How to use the list.files() function in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
