Crate walkdir [] [src]

Crate walkdir provides an efficient and cross platform implementation of recursive directory traversal. Several options are exposed to control iteration, such as whether to follow symbolic links (default off), limit the maximum number of simultaneous open file descriptors and the ability to efficiently skip descending into directories.

To use this crate, add walkdir as a dependency to your project's Cargo.toml:

[dependencies]
walkdir = "1"

From the top

The WalkDir type builds iterators. The WalkDirIterator trait provides methods for directory iterator adapters, such as efficiently pruning entries during traversal. The DirEntry type describes values yielded by the iterator. Finally, the Error type is a small wrapper around std::io::Error with additional information, such as if a loop was detected while following symbolic links (not enabled by default).

Example

The following code recursively iterates over the directory given and prints the path for each entry:

use walkdir::WalkDir;

for entry in WalkDir::new("foo") {
    let entry = entry.unwrap();
    println!("{}", entry.path().display());
}

Or, if you'd like to iterate over all entries and ignore any errors that may arise, use filter_map. (e.g., This code below will silently skip directories that the owner of the running process does not have permission to access.)

use walkdir::WalkDir;

for entry in WalkDir::new("foo").into_iter().filter_map(|e| e.ok()) {
    println!("{}", entry.path().display());
}

Example: follow symbolic links

The same code as above, except follow_links is enabled:

use walkdir::WalkDir;

for entry in WalkDir::new("foo").follow_links(true) {
    let entry = entry.unwrap();
    println!("{}", entry.path().display());
}

Example: skip hidden files and directories efficiently on unix

This uses the filter_entry iterator adapter to avoid yielding hidden files and directories efficiently:

use walkdir::{DirEntry, WalkDir, WalkDirIterator};

fn is_hidden(entry: &DirEntry) -> bool {
    entry.file_name()
         .to_str()
         .map(|s| s.starts_with("."))
         .unwrap_or(false)
}

let walker = WalkDir::new("foo").into_iter();
for entry in walker.filter_entry(|e| !is_hidden(e)) {
    let entry = entry.unwrap();
    println!("{}", entry.path().display());
}

Structs

DirEntry

A directory entry.

Error

An error produced by recursively walking a directory.

Iter

An iterator for recursively descending into a directory.

IterFilterEntry

A recursive directory iterator that skips entries.

WalkDir

A builder to create an iterator for recursively walking a directory.

Traits

WalkDirIterator

A trait for recursive directory iterators.

Functions

is_same_file

Returns true if the two file paths may correspond to the same file.

Type Definitions

Result

A result type for walkdir operations.