# list.files in directory R: How to list files in a directory or a folder?

In this article, we will share with you how to use the list.files in directory R. The list.files() function in R can help you list all files in the directory or the folder. Let’s follow this guide to learn more about what the list.files() in R does and how to use it with the explanation below.

## What does the list.files() function do in R?

The list.files() function in R helps you get the files or folders in your computer’s directory or folder. This function will return the character vector. Let’s take a look at the syntax of this function.

Syntax:

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

Parameters:

• path: The directory of the folder.
• pattern: The default is NULL. The regular expression.
• all.files: The default is FALSE. If TRUE, the function will return all the file names.
• full.names: The default is FALSE. If TRUE, the file path will be returned. Otherwise, the function will only return the file name.
• recursive: The default is FALSE. The listing recurs into the directory or not.
• ignore.case: The default is FALSE. Case-insensitive or not.
• include.dirs: The default is FALSE. List the subdirectory or not.

Return value

The character vector.

After learning what the list.files() function does in R and the syntax of this function. You will learn how to use it in the next title below.

## How to use the list.files in directory R?

As you learned in the previous part, the list.files() function is used to list the files in the directory of your computer. Follow us to learn how to use it with the examples below.

### Get all the files in the directory

You can get all the files in the directory by the list.files() function.

Look at the example below.

# List all the files in the directory: C:/Users/DELL/Documents/LearnShareIT
list.files('C:/Users/DELL/Documents/LearnShareIT')

Output

[1] "b1.java" "Downloads" "function.js" "index.css"    

Note that I am using this function with my computer so if you try it with your computer, the result is different.

### Get all the files in the directory with the specified amount

You can get all the files in the directory with the specified amount in the list.files() function.

Look at the example below.

# List the first two files in the directory: C:/Users/DELL/Documents/LearnShareIT
list.files('C:/Users/DELL/Documents/LearnShareIT')[1:2]

Output

[1] "b1.java" "Downloads"

### Get all the files in the directory with the specified extension

You can get all the files in the directory with the specified extension in the list.files() function, and you also need to assign the value of the parameter name ‘pattern’. The value of this parameter is the specified extension.

Look at the example below.

# List the java files in the directory: C:/Users/DELL/Documents/LearnShareIT
list.files('C:/Users/DELL/Documents/LearnShareIT', pattern = 'java')

Output

[1] "b1.java" 

Beside listing the files with the list.files() function, you can also learn how to choose the files in your computer with the choose.files() function here.

## Summary

We have shown you the list.files() function does, and how to perform the list.files in directory R. You can use this function to list all the files in the specified directory. Also, you can customize the result by assigning the value to the parameter of this function. We hope this guide is useful for you. Thanks!

Maybe you are interested:

Posted in R