Wildcard Expansion on File Paths


Function to do wildcard expansion (also known as ‘globbing’) on file paths.


Sys.glob(paths, dirmark = FALSE)



character vector of patterns for relative or absolute filepaths. Missing values will be ignored.


logical: should matches to directories from patterns that do not already end in / have a slash appended? May not be supported on all platforms.


This expands wildcards in file paths. For precise details, see your system's documentation on the glob system call. There is a POSIX 1003.2 standard (see http://pubs.opengroup.org/onlinepubs/9699919799/functions/glob.html) but some OSes will go beyond this. The R implementation will always do tilde expansion.

All systems should interpret * (match zero or more characters), ? (match a single character) and (probably) [ (begin a character class or range). The handling of paths ending with a separator is system-dependent. On a POSIX-2008 compliant OS they will match directories (only), but as they are not valid filepaths on Windows, they match nothing there. (Earlier POSIX standards allowed them to match files.)

The rest of these details are indicative (and based on the POSIX standard).

If a filename starts with . this may need to be matched explicitly: for example Sys.glob("*.RData") may or may not match ‘.RData’ but will not usually match ‘.aa.RData’. Note that this is platform-dependent: e.g.\sspaceon Solaris Sys.glob("*.*") matches ‘.’ and ‘..’.

[ begins a character class. If the first character in [...] is not !, this is a character class which matches a single character against any of the characters specified. The class cannot be empty, so ] can be included provided it is first. If the first character is !, the character class matches a single character which is none of the specified characters. Whether . in a character class matches a leading . in the filename is OS-dependent.

Character classes can include ranges such as [A-Z]: include - as a character by having it first or last in a class. (The interpretation of ranges should be locale-specific, so the example is not a good idea in an Estonian locale.)

One can remove the special meaning of ?, * and [ by preceding them by a backslash (except within a character class).


A character vector of matched file paths. The order is system-specific (but in the order of the elements of paths): it is normally collated in either the current locale or in byte (ASCII) order; however, on Windows collation is in the order of Unicode points.

Directory errors are normally ignored, so the matches are to accessible file paths (but not necessarily accessible files).

See Also


Quotes for handling backslashes in character strings.


Sys.glob(file.path(R.home(), "library", "*", "R", "*.rdx"))

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.

comments powered by Disqus