Chapter 9. I/O Case Study: A Library for Searching the Filesystem

The problem of I know I have this file, but I don’t know where it is has been around for as long as computers have had hierarchical filesystems. The fifth edition of Unix introduced the find command in 1974; it remains indispensable today. The state of the art has come a long way: modern operating systems ship with advanced document indexing and search capabilities.

There’s still a valuable place for find-like capability in the programmer’s toolbox. In this chapter, we’ll develop a library that gives us many of find’s capabilities, without leaving Haskell. We’ll explore several different approaches to writing this library, each with different strengths.

The find Command

If you don’t use a Unix-like operating system, or you’re not a heavy shell user, it’s quite possible you may not have heard of find. Given a list of directories, it searches each one recursively and prints the name of every entry that matches an expression.

Individual expressions can take such forms as “name matches this glob pattern,” “entry is a plain file,” “last modified before this date,” and many more. They can be stitched together into more complex expressions using and and or operators.

Starting Simple: Recursively Listing a Directory

Before we plunge into designing our library, let’s solve a few smaller issues. Our first problem is to recursively list the contents of a directory and its subdirectories:

-- file: ch09/RecursiveContents.hs module ...

Get Real World Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.