I am not satisfied to find files matching a string like this:
(remove-if-not (lambda (it)
(search "wildcard" (namestring it)))
(uiop:directory-files "./"))
;; I'll ignore case with str:contains?
;; https://github.com/vindarel/cl-str
How would one search for files with unix-style wildcards ?
If it is not built-in, I'd enjoy a solution with uiop. Maybe there is with Osicat or cl-fad (with which it doesn't seem so, the documentation oftentimes says "non-wild pathname").
Bonus if it is possible to use the double wildcard to traverse directories recursively (./**/*.jpg
).
edit: I have tried variants of (directory #p"./**/*.jpg")
and it returns nil :( Also tried #p".*jpg"
, #p"./.*jpg"
,…
(wild-pathname-p (pathname "*.jpg"))
(:WILD :WILD-INFERIORS)
(make-pathname :name :wild :type "jpg")
#P"*.jpg"
The following gets me files by jpg extension, but it isn't a proper wildcard yet:
(directory *)
(#P"/home/vince/cl-cookbook/AppendixA.jpg"
#P"/home/vince/cl-cookbook/AppendixB.jpg"
#P"/home/vince/cl-cookbook/AppendixC.jpg")
Documentation on pathnames and make-pathname
: http://gigamonkeys.com/book/files-and-file-io.html (no mentions of wildcards)
SBCL
SBCL supports wildcards in names. First, create some files:
Then, list all files that contains an "a":
The pathname contains an implementation-specific (valid) name component:
SBCL also defines
sb-ext:map-directory
, which process files one by one, instead of first collecting all files in a list.Portable solutions
If you need to stick to standard pathname components, you can first call
directory
with normal wildcards, and filter the resulting list:... where
wildcard
might be based on regex (PPCRE):(note: the above negative lookbehind does not eliminate escaped backslashes)
Intermediate functions:
no current directory and no home directory characters
The concept of
.
denoting the current directory does not exist in portable Common Lisp. This may exist in specific filesystems and specific implementations.Also
~
to denote the home directory does not exist. They may be recognized by some implementations as non-portable extensions.In pathname strings you have
*
and**
as wildcards. This works in absolute and relative pathnames.defaults for the default pathname
Common Lisp has
*default-pathname-defaults*
which provides a default for some pathname operations.Examples
Now in above it is already slightly undefined or diverging what implementations do on Unix:
Next:
Using a relative pathname:
Files in your home directory:
The same:
Finding all files ending with sh in
/usr/local/
and below:Constructing pathnames with MAKE-PATHNAME
Three ways to find all
.h
files under/usr/local/
:Problems
There are a lot of different interpretations of implementations across platforms ('windows', 'unix', 'mac', ...) and even on the same platform (especially 'windows' or 'unix'). Stuff like unicode in pathnames creates additional complexity - not describe in the CL standard.
We still have a lot of different filesystems ( https://en.wikipedia.org/wiki/List_of_file_systems ), but they are different or different in capabilities from what was typical when Common Lisp was designed. Implementations may have tracked the changes, but not necessarily in portable ways.