list.files() in R: list files in a directory

In tutorial, we will learn how to list all the files in a directory in R. With list.files() function in R we can get all the files in a folder or directory as a vector. Here we will see multiple examples of using list.files() to get the files in a directoy, files matching a pattern in a directory and list all the files recursively in a directory and the directories within it.

The basic usage of list.files() is as shown below. It can take a number of arguments to list the files in a dir under different conditions.

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

Get a list of files in the current directory

Using list.files() with default arguments will give us the list of files in the current working directory. Note that it is also lists directories as files.

list.files()

[1] "how_to_list_filenames_in_directory.pdf"
[2] "how_to_list_filenames_in_directory.png"
[3] "list_files_in_R_examples_files"        
[4] "list_files_in_R_examples.html"         
[5] "list_files_in_R_examples.qmd"          
[6] "list_files_in_R_examples.rmarkdown"    

Get a list of files in a specific directory

Instead of listing files in the current working directory, we can list all the files in a specific path using path argument. In the example below, we list all the files in a given path.

list.files(path="../R")

 [1] "all_equal.Rmd"                               
 [2] "Available_Datasets_in_specific_R_package.png"
 [3] "colMean.html"                                
 [4] "colMean.Rmd"                                 
 [5] "colSums.html"                                
 [6] "colSums.Rmd"                                 
 [7] "combine_list_of_dataframes.html"             
 [8] "combine_list_of_dataframes.Rmd"              
 [9] "compute_time_with_Sys_time.Rmd"  

If we use all.files=TRUE argument with list.files(), we get all the files, including any hidden files in the directory. In this example below we see “.” directories which are hidden.

list.files(all.files=TRUE)

[1] "."                                     
[2] ".."                                    
[3] "how_to_list_filenames_in_directory.pdf"
[4] "how_to_list_filenames_in_directory.png"
[5] "list_files_in_R_examples_files"        
[6] "list_files_in_R_examples.html"         
[7] "list_files_in_R_examples.qmd"          
[8] "list_files_in_R_examples.rmarkdown"   

Get list of files recursively in R

With recurisve=TRUE argument, list.files() recurses through all the directories in the current path and get all the files. In the example below, we can see that list.files() is listing files in the “list_files_in_R_examples_files” directory.

list.files(recursive=TRUE)

 [1] "how_to_list_filenames_in_directory.pdf"                                        
 [2] "how_to_list_filenames_in_directory.png"                                        
 [3] "list_files_in_R_examples_files/figure-html/unnamed-chunk-2-1.png"              
 [4] "list_files_in_R_examples_files/figure-html/unnamed-chunk-3-1.png"              
 [5] "list_files_in_R_examples_files/libs/bootstrap/bootstrap-icons.css"             
 [6] "list_files_in_R_examples_files/libs/bootstrap/bootstrap-icons.woff"            
 [7] "list_files_in_R_examples_files/libs/bootstrap/bootstrap.min.css"               
 [8] "list_files_in_R_examples_files/libs/bootstrap/bootstrap.min.js"                
 [9] "list_files_in_R_examples_files/libs/clipboard/clipboard.min.js"                
[10] "list_files_in_R_examples_files/libs/quarto-html/anchor.min.js"                 
[11] "list_files_in_R_examples_files/libs/quarto-html/popper.min.js"                 
[12] "list_files_in_R_examples_files/libs/quarto-html/quarto-syntax-highlighting.css"
[13] "list_files_in_R_examples_files/libs/quarto-html/quarto.js"                     
[14] "list_files_in_R_examples_files/libs/quarto-html/tippy.css"                     
[15] "list_files_in_R_examples_files/libs/quarto-html/tippy.umd.min.js"              
[16] "list_files_in_R_examples.html"                                                 
[17] "list_files_in_R_examples.qmd"                                                  
[18] "list_files_in_R_examples.rmarkdown"       

List files in a directory matching a pattern in R

We can use pattern argument to select files and list only those matches the pattern. Here we have use pattern argument to list the files that starts with the prefix “how”.

list.files(pattern="how*")

[1] "how_to_list_filenames_in_directory.pdf"
[2] "how_to_list_filenames_in_directory.png"

In the second example using pattern and recursive, we are listing all the image files in png format in the current working directory recursively.

list.files(pattern="*png", recursive=TRUE)

[1] "how_to_list_filenames_in_directory.png"                          
[2] "list_files_in_R_examples_files/figure-html/unnamed-chunk-3-1.png"
Exit mobile version