In this article, we will learn about the ‘dir’ function in R and how to use it to list files and folders in a directory. Let’s go into detail now.
What is the ‘dir’ function in R?
The ‘dir()’ function generates a character vector of the names of files or folders in the specified directory.
Syntax:
dir(path)
Parameter:
- path: a character vector of the full pathname. The default is the current working directory.
In addition, the R language also provides the dir()
function with many other parameters so that the user can choose the different results that the dir()
function will return. To see more of these parameters, run the following command:
?dir
Output:

You can use the list.files()
function with the same parameters and functions as the dir()
function.
How to use the ‘dir()’ function?
We will give examples of using the dir()
function and its parameters in listing files and folders in a directory/folder.
List files and directories in the working directory
The dir()
function will default list files and folders in the working directory if the user omits the path
parameter.
Example:
cat("The current working directory\n") getwd() cat("\nAll files and folders in the current working directory\n") dir() # Returns the same result # list.files()
Output:
The current working directory
[1] "D:/R Project/dir_R"
All files and directories in the current working directory
[1] "dir.R" "dir_R.Rproj"
Our current working directory is D:/R Project/dir_R
. The dir()
function returns a character vector containing all the files and directories in this working directory. The results will differ on each device, but the code will generally run when copied.
List files and folders in any directory
First, you must have the correct path to any directory where you want to execute the dir()
function. Then you have to set that path for the path
parameter.
Example:
cat("List files and directories in the 'D:/Documents/LearnShareIT' path\n") dir("D:/Documents/LearnShareIT")
Output:
List files and directories in the 'D:/Documents/LearnShareIT' path
[1] "C" "C++" "Java" "JavaScript" "LearnShareIT.JPG" "LOGO.JPG" "Python" "R"
In the above example, we have set the path
parameter to the D:/Documents/LearnShareIT
path, so the dir()
function returns all the files and directories contained in this directory.
In addition, you can also use other parameters to customize the results that the dir()
function returns.
Example:
cat("List full path of the files containing the '.JPG' string\n") dir("D:/Documents/LearnShareIT", pattern = ".JPG", full.names = TRUE)
Output:
List full path of the files containing the '.JPG' string
[1] "D:/Documents/LearnShareIT/LearnShareIT.JPG" "D:/Documents/LearnShareIT/LOGO.JPG"
In the above example, we have listed the full path of the files containing the .JPG
string by setting the pattern
parameter to .JPG
and the full.names
parameter to TRUE.
Summary
This article shares the dir()
function in R and how to use it to list files and directories in a directory. You need to set the path parameter as the path
to the directory where you want to execute the dir()
function or omit this parameter if you want to execute the dir()
function in the working directory. In addition, you can also use other parameters to customize the result returned by the dir()
function. Thanks for reading.
Maybe you are interested:
- How To Use The colMeans In R
- The rbind() Function In R: How To Use rbind() In R
- cumsum() Function In R: Sum of a Numeric Object’s Cumulative Value

Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP