dirgrab 📁⚡
dirgrab is a simple command-line tool to grab the content of files within a directory and concatenate them, suitable for feeding project contexts into language models.
It intelligently uses Git context when available (git ls-files) to only include tracked files (respecting .gitignore), but also works seamlessly on plain directories.
Features:
- Concatenates file contents to stdout, a file (
-o), or the clipboard (-c). - Uses
git ls-filesin Git repositories (respects.gitignoreby default). - Optionally includes untracked files in Git repos (
-u). - Works on non-Git directories by walking the file tree.
- Allows custom exclude patterns (
-e) using.gitignoreglob syntax. - Includes file headers (
--- FILE: path/to/file ---) by default (disable with--no-headers). - Skips binary/non-UTF8 files with a warning.
Installation
Ensure you have Rust and Cargo installed. Then, install dirgrab using Cargo:
Verify the installation:
Usage
Arguments:
[TARGET_PATH]: Optional path to the directory or Git repository to process. Defaults to the current working directory.
Options:
-o, --output <FILE>: Write output to a file instead of stdout.-c, --clipboard: Copy output to the system clipboard.--no-headers: Disable the default '--- FILE: ... ---' headers.-e, --exclude <PATTERN>: Add glob patterns to exclude files/dirs (can be used multiple times).-u, --include-untracked: [Git Mode Only] Include untracked files (still respects.gitignoreand excludes).-v, -vv, -vvv: Increase verbosity level for logging.-h, --help: Print help information.-V, --version: Print version information.
Examples:
-
Grab tracked files from current Git repo to stdout (default headers):
-
Grab files from current directory, disable headers, output to file:
-
Grab tracked files from specific repo path, copy to clipboard:
-
Grab tracked + untracked files, excluding logs and build dirs:
-
Grab all files from a non-Git directory, excluding temp files:\
Behavior Details
- Git Mode: If the target directory is detected as part of a Git repository,
dirgrabusesgit ls-filesto determine which files to include. This automatically respects.gitignorerules. The-uflag adds untracked files, still respecting.gitignore. Exclusions (-e) are passed togit ls-files. - Non-Git Mode: If the target directory is not a Git repository,
dirgrabwalks the directory tree. It includes all files found unless they match an exclusion pattern (-e)..gitignorefiles are ignored in this mode. - File Encoding:
dirgrabattempts to read files as UTF-8. If a file cannot be decoded (likely binary), it is skipped, and a warning is printed to stderr (visible with-v). - Output: By default, file content is concatenated directly. With headers enabled (default), each file's content is preceded by
--- FILE: <relative_path> ---. An extra newline is added between files for readability.
Library (dirgrab-lib)
The core logic is available as a separate library crate (dirgrab-lib) for use in other Rust projects. See its documentation on docs.rs (link will work after publishing).
License
This project is licensed under either of:
at your option.
Contribution
Contributions are welcome! Please feel free to submit issues and pull requests.