Previous: Extended Attributes, Up: Information about Files
This section explains how to search for a file in a list of directories (a path), or for an executable file in the standard list of executable file directories.
To search for a user-specific configuration file, See Standard File Names, for the locate-user-emacs-file
function.
This function searches for a file whose name is filename in a list of directories given by path, trying the suffixes in suffixes. If it finds such a file, it returns the file's absolute file name (see Relative File Names); otherwise it returns
nil
.The optional argument suffixes gives the list of file-name suffixes to append to filename when searching.
locate-file
tries each possible directory with each of these suffixes. If suffixes isnil
, or("")
, then there are no suffixes, and filename is used only as-is. Typical values of suffixes areexec-suffixes
(see Subprocess Creation),load-suffixes
,load-file-rep-suffixes
and the return value of the functionget-load-suffixes
(see Load Suffixes).Typical values for path are
exec-path
(see Subprocess Creation) when looking for executable programs, orload-path
(see Library Search) when looking for Lisp files. If filename is absolute, path has no effect, but the suffixes in suffixes are still tried.The optional argument predicate, if non-
nil
, specifies a predicate function for testing whether a candidate file is suitable. The predicate is passed the candidate file name as its single argument. If predicate isnil
or omitted,locate-file
usesfile-readable-p
as the predicate. See Kinds of Files, for other useful predicates, e.g.,file-executable-p
andfile-directory-p
.This function will normally skip directories, so if you want it to find directories, make sure the predicate function returns
dir-ok
for them. For example:(locate-file "html" '("/var/www" "/srv") nil (lambda (f) (if (file-directory-p f) 'dir-ok)))For compatibility, predicate can also be one of the symbols
executable
,readable
,writable
,exists
, or a list of one or more of these symbols.
This function searches for the executable file of the named program and returns the absolute file name of the executable, including its file-name extensions, if any. It returns
nil
if the file is not found. The functions searches in all the directories inexec-path
, and tries all the file-name extensions inexec-suffixes
(see Subprocess Creation).