Erlang logo
User's Guide
Reference Manual
Release Notes

Reference Manual
Version 3.1

Expand All
Contract All

Table of Contents





File utilities, such as wildcard matching of filenames.


This module contains utilities on a higher level than the file module.

This module does not support "raw" filenames (that is, files whose names do not comply with the expected encoding). Such files are ignored by the functions in this module.

For more information about raw filenames, see the file module.


filename() = file:name()

dirname() = filename()

dirname_all() = filename_all()

filename_all() = file:name_all()


ensure_dir(Name) -> ok | {error, Reason}


Reason = file:posix()

Ensures that all parent directories for the specified file or directory name Name exist, trying to create them if necessary.

Returns ok if all parent directories already exist or can be created. Returns {error, Reason} if some parent directory does not exist and cannot be created.

file_size(Filename) -> integer() >= 0


Filename = filename_all()

Returns the size of the specified file.

fold_files(Dir, RegExp, Recursive, Fun, AccIn) -> AccOut


Dir = dirname()
RegExp = string()
Recursive = boolean()
Fun = fun((F :: file:filename(), AccIn) -> AccOut)
AccIn = AccOut = term()

Folds function Fun over all (regular) files F in directory Dir that match the regular expression RegExp (for a description of the allowed regular expressions, see the re module). If Recursive is true, all subdirectories to Dir are processed. The regular expression matching is only done on the filename without the directory part.

If Unicode filename translation is in effect and the file system is transparent, filenames that cannot be interpreted as Unicode can be encountered, in which case the fun() must be prepared to handle raw filenames (that is, binaries). If the regular expression contains codepoints > 255, it does not match filenames that do not conform to the expected character encoding (that is, are not encoded in valid UTF-8).

For more information about raw filenames, see the file module.

is_dir(Name) -> boolean()


Returns true if Name refers to a directory, otherwise false.

is_file(Name) -> boolean()


Returns true if Name refers to a file or a directory, otherwise false.

is_regular(Name) -> boolean()


Returns true if Name refers to a (regular) file, otherwise false.

last_modified(Name) -> file:date_time() | 0


Returns the date and time the specified file or directory was last modified, or 0 if the file does not exist.

wildcard(Wildcard) -> [file:filename()]


Wildcard = filename() | dirname()

Returns a list of all files that match Unix-style wildcard string Wildcard.

The wildcard string looks like an ordinary filename, except that the following "wildcard characters" are interpreted in a special way:


Matches one character.


Matches any number of characters up to the end of the filename, the next dot, or the next slash.


Two adjacent * used as a single pattern match all files and zero or more directories and subdirectories.


Matches any of the characters listed. Two characters separated by a hyphen match a range of characters. Example: [A-Z] matches any uppercase letter.


Alternation. Matches one of the alternatives.

Other characters represent themselves. Only filenames that have exactly the same character in the same position match. Matching is case-sensitive, for example, "a" does not match "A".

Notice that multiple "*" characters are allowed (as in Unix wildcards, but opposed to Windows/DOS wildcards).


The following examples assume that the current directory is the top of an Erlang/OTP installation.

To find all .beam files in all applications, use the following line:


To find .erl or .hrl in all applications src directories, use either of the following lines:


To find all .hrl files in src or include directories:


To find all .erl or .hrl files in either src or include directories:


To find all .erl or .hrl files in any subdirectory:


wildcard(Wildcard, Cwd) -> [file:filename()]


Wildcard = filename() | dirname()
Cwd = dirname()

Same as wildcard/1, except that Cwd is used instead of the working directory.