Sys.glob Function in R (Example) | Wildcard Expansion & Globbing on File Paths


In this R tutorial you’ll learn how to conduct a wildcard expansion (or globbing) on file paths using the Sys.glob function.

The article will consist of one example for the application of the Sys.glob function. To be more precise, the article is structured as follows:

Let’s get started!


Example: Perform Wildcard Expansion on File Paths Using Sys.glob() Function

The code below shows how to conduct globbing on file directory paths using the Sys.glob and the file.path functions.

For this, I have created a directory and several subdirectories containing different TXT files.

We can extract the paths to all of these TXT files as shown below:

Sys.glob(file.path("C:/Users/Joach/Desktop/my directory", "*", "*.txt"))  # Apply Sys.glob function
# [1] "C:/Users/Joach/Desktop/my directory/folder 1/my file 1.txt"
# [2] "C:/Users/Joach/Desktop/my directory/folder 1/my file 2.txt"
# [3] "C:/Users/Joach/Desktop/my directory/folder 2/my file 3.txt"
# [4] "C:/Users/Joach/Desktop/my directory/folder 3/my file 4.txt"

The previous R code has returned all paths to my TXT files based on a wildcard pattern in our file paths.


Video, Further Resources & Summary

I have recently published a video on the Statistics Globe YouTube channel, which illustrates the R programming syntax of this article. You can find the video below.


The YouTube video will be added soon.


Furthermore, you may want to have a look at the other articles on my homepage. I have published numerous other tutorials already.


To summarize: In this article, I have demonstrated how to apply the Sys.glob function in R programming. Don’t hesitate to tell me about it in the comments section, in case you have any additional questions.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.