Get a Recursive, Relative List of Files from a Directory in Rust

September 2025

This is how I'm getting recursive file lists with relative paths from a directory. Hidden files are ignored.

---
[dependencies]
walkdir = "2"
---

use walkdir::WalkDir;
use std::path::PathBuf;

fn main() {
  let dir = PathBuf::from("/Users/alan/workshop/examples/recursive_test");
  let files = get_files(&dir);
  files.iter().for_each(|f| {
    println!("File: {}", f.display());
  });
}

pub fn get_files(dir: &PathBuf) -> Vec<PathBuf> {
  WalkDir::new(dir)
    .into_iter()
      .filter_map(|e| e.ok())
      .filter(|e| e.path().is_file())
      .filter(|e| {
        !e.file_name()
          .to_str()
          .map(|s| s.starts_with("."))
          .unwrap_or(false)
        })
      .map(|e| e.path().to_path_buf())
      .map(|pb| pb.strip_prefix(dir).unwrap().to_path_buf())
    .collect()
}
Output:
File: a/2.txt
File: a/c/4.txt
File: a/c/delta.html
File: alfa.html
File: 1.txt
File: bravo.html
File: b/3.txt
File: b/charlie.html
end of line