List all Files with Specific Extension in R Last Updated : 17 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report R programming language contains a wide variety of method to work with directory and its associated sub-directories. There are various inbuilt methods in R programming language which are used to return the file names with the required extensions. It can be used to efficiently locate the presence of a file. Directory in use: Method 1 : Using list.files() method The list.files() method in R language is used to produce a character vector of the names of files or directories in the named directory. The regular expression is specified to match the files with the required file extension. The ‘$‘ symbol indicates the end-of-the-string, and the ‘\\‘ symbol before the '.' is used to make sure that the files match the specified extension exactly. The pattern is case-sensitive, and any matches returned are strictly based on the specified characters of the pattern. The file names returned are sorted in alphabetical order. Syntax: list.files(path = ".", pattern = NULL, full.names = FALSE, ignore.case = FALSE) Parameters : path - (Default : current working directory) A character vector of full path namespattern - regular expression to match the file names withfull.names - If TRUE, returns the absolute path of the file locationsignore.case - Indicator of whether to ignore the case while searching for files. Example: R # list all the file names of the # specified pattern fnames <- list.files(pattern = "\\.pdf$") print ("Names of files") print (fnames) Output [1] "Names of files" [1] "cubegfg.pdf" "maytravelform.pdf" The case of the file name can also be ignored by setting the attribute of ignore.case as the TRUE. Example: R # list all the file names of the # specified pattern fnames <- list.files(pattern = "\\.pdf$", ignore.case = TRUE) print ("Names of files") print (fnames) Output [1] "Names of files" [1] "cubegfg.pdf" "maytravelform.pdf" "pdf2.pDf" Method 2 : Using Sys.glob() method The Sys.glob() method in R is used to extract the file names with the matched pattern. This method is used to expand wild cards, termed as "globbing" in file paths. The special character "*" is used to find matches for zero or more characters, in the retrieved string. Syntax: Sys.glob ( pattern) Parameters : pattern - character vector of patterns for relative or absolute file paths Example: R # list all the file names of # the specified pattern fnames <- Sys.glob("*.png") print ("Names of files") print (fnames) Output [1] "Names of files" [1] "Screenshot 2021-06-03 at 4.35.54 PM.png" "cubegfg.png" [3] "gfg.png" Comment More infoAdvertise with us Next Article Bash Scripting - File Extension Y yippeee25 Follow Improve Article Tags : R Language R directory-programs Similar Reads Python - List files in directory with extension In this article, we will discuss different use cases where we want to list the files with their extensions present in a directory using python. Modules Usedos: The OS module in Python provides functions for interacting with the operating system.glob: In Python, the glob module is used to retrieve fi 3 min read Find all the Files in a Directory with .txt Extension in Python Directory traversal is a common operation performed by file locator in Operating Systems. The operating system offers elaborate methods for streamlining the search process and the choice to search for a selected filename/extension. This article will teach you how to find all the files in a directory 5 min read How to Find all Files Containing Specific Text (string) on Linux Suppose you are looking for a file in Linux, but you have forgotten its name. You only remember the contents of the file. How will you find the file in this case? Well, there are some useful methods that will help you find a file containing a specific text (or string) in Linux. The string needs to b 4 min read How to get a File Extension in PHP ? In this article, we will learn how to get the current file extensions in PHP. Input : c:/xampp/htdocs/project/home Output : "" Input : c:/xampp/htdocs/project/index.php Output : ".php" Input : c:/xampp/htdocs/project/style.min.css Output : ".css" Using $_SERVER[âSCRIPT_NAMEâ]: $_SERVER is an array o 2 min read Bash Scripting - File Extension Bash scripting is a powerful tool for automating tasks and working with files in the command line. One important aspect of working with files in bash is understanding how to handle file extensions. In bash, a file extension is the portion of a file name that follows the last period (.) in the file n 6 min read Read text File with Space as Delimiter in R In this article, we will discuss how to read a text file with spaces as delimiters in R programming language. Base R allows us to read and access the content within the text files with any number of characters as the delimiter. File in use: The read.table() method in R can be used to read data fro 2 min read Like