Home > database >  List files that end with pattern and lack an extension
List files that end with pattern and lack an extension

Time:11-28

I have a directory with multiple subdirectories that contain files. The files themselves have no extension; however, each file has an additional header file with the extension ".hdr".

In R, I want to list all file names that contain the string map_masked and end with the pattern "masked", but I only want the files without an extension (the ones that end with the pattern, not the header files).

As suggested in this answer, I tried to use the $ sign to indicate the pattern should occur at the end of a line.

This is the code I used:

dir <- "/my/directory"

list.files(dir, pattern = "map_masked|masked$", recursive = TRUE)

The output, however, looks as follows:

[1] "subdirectory/something_map_masked_something_masked"
[2] "subdirectory/something_map_masked_something_masked.hdr"
etc.

Now, how do I tell R to exclude the files that have an ".hdr" extension? I am aware this could easily be done by applying a filter on the output, but I would rather like to know what is wrong with my code and understand why R behaves the way it does in this case.

CodePudding user response:

You can use

list.files(dir, pattern = "map_masked.*masked$", recursive = TRUE)

It returns filepaths that contain map_masked and end with masked string.

Details:

  • map_masked - a fixed string
  • .* - any zero or more chars as many as possible
  • masked - a masked substring
  • $ - end of string.

See the regex demo.

  • Related