MyNixOS website logo
Description

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

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.

directory-contents

Haskell Hackage Hackage CI Github CI BSD3 License

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

Description

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.

Example


>
> 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)
>

Metadata

Version

0.2.0.2

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows