directory-contents: Recursively build, navigate, and operate on a tree of directory contents.

[ bsd3, library, program, system ] [ Propose Tags ]

Like the linux tree command, this library recursively constructs a tree of directory contents while detecting and avoiding symlink cycles. It also provides functions for operating on and navigating the contents.

[Skip to Readme]
Versions [faq],
Change log
Dependencies base (>=4.12 && <4.15), containers (==0.6.*), directory (==1.3.*), directory-contents, filepath (==1.4.*), text (==1.2.*), transformers (==0.5.*), witherable (==0.3.*) [details]
License BSD-3-Clause
Copyright 2020 Obsidian Systems LLC
Author Obsidian Systems
Category System
Bug tracker
Source repo head: git clone git://
Uploaded by abrar at 2020-12-11T16:33:58Z
Distributions NixOS:
Executables readme
Downloads 45 total (8 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs uploaded by user
Build status unknown [no reports yet]


[Index] [Quick Jump]


Maintainer's Corner

For package maintainers and hackage trustees

Readme for directory-contents-

[back to package description]


Haskell Hackage Hackage CI Github CI BSD3 License

Recursively list, operate on, and navigate the contents of a directory while avoiding symlink loops.


Modeled after the linux tree command (when invoked with the follow-symlinks option), this module recursively lists the contents of a directory while avoiding symlink loops. In particular, tree -l and buildDirTree should provide the same result. See the documentation of buildDirTree for an example.

In addition to building the directory-contents tree, this module provides facilities for filtering, displaying, and navigating the directory hierarchy.


> import Data.Foldable as F
> import Data.List
> import qualified Data.Text as T
> import System.Directory.Contents
> import System.Directory.Contents.Zipper
> import System.FilePath
> main :: IO ()
> main = do

Building a directory tree is easy. Just call buildDirTree on a path of your choice. It'll recursively enumerate the contents of the directories. If it encounters symlinks, it'll follow those symlinks if it hasn't yet encountered the target of the symlink. If it has, it'll store a reference to that already-seen target.

>   mp <- buildDirTree "."
>   case mp of
>     Nothing -> putStrLn "Couldn't find that path."
>     Just p -> do

Once you've got a DirTree you can fmap, traverse, filter, or wither it to transform it however you like.

Note that the filtering operations generally do not remove empty directories. You have to call pruneDirTree to do that.

>       let f = pruneDirTree =<< filterDirTree ((`elem` [".hs", ".lhs"]) . takeExtension) p
>       putStrLn $ case f of
>         Nothing -> "No haskell source files found."
>         Just hs -> unlines
>           [ "Paths that contain haskell source files:"
>           , T.unpack $ drawDirTree hs
>           , ""
>           , "Haskell source files:"
>           , intercalate ", " $ F.toList hs
>           ]

You can also use the provided DirZipper to browse your directory hierarchy and make changes wherever you like.

>       let printFocused =  maybe
>             (putStrLn "Couldn't find navigation target")
>             (printDirTree . focused)
>       putStrLn "Navigating down to src/System/Directory:"
>       printFocused $
>         downTo "Directory" =<< downTo "System" =<< downTo "src" (zipped p)
>       putStrLn "Navigating using a path containing \"..\":"
>       printFocused $
>           followRelative "./src/../src/System/Directory" (zipped p)
>       putStrLn "Removing the src/System directory. The src folder is now empty"
>       putStrLn "(note that this doesn't change the actual files):"
>       printFocused $
>         remove =<< followRelative "./src/System" (zipped p)