dua-cli

repository·main·Indexed 27 days ago

https://github.com/byron/dua-cli

A fast, parallel disk usage analyzer (version 2.39.0) featuring a high-performance CLI and an interactive TUI to identify and delete large files and directories. It provides tools for filesystem traversal, data aggregation, and customizable sorting modes (size, modification time, and entry count). The tool can be used as a standalone CLI or as a Rust library for high-performance disk usage analysis.

Tokens
3.7K
Snippets
12
Records
24
Agent score
91%

What's inside dua-cli

  1. Install dua-cli

    main

    You can install dua-cli using various package managers depending on your operating system.

    MacOS

    • Homebrew: brew install dua-cli
    • MacPorts: sudo port install dua-cli
    • Binary Script:
      curl -LSfs https://raw.githubusercontent.com/Byron/dua-cli/master/ci/install.sh | \
          sh -s -- --git Byron/dua-cli --crate dua --tag v2.29.0

    Linux

    • Fedora: sudo dnf install dua-cli
    • Arch Linux: sudo pacman -S dua-cli
    • VoidLinux: xbps-install dua-cli
    • NixOS: Add pkgs.dua to your environment.systemPackages.
    • Binary Script (MUSL):
      curl -LSfs https://raw.githubusercontent.com/Byron/dua-cli/master/ci/install.sh | \
          sh -s -- --git Byron/dua-cli --target x86_64-unknown-linux-musl --crate dua --tag v2.29.0

    Windows

    • Scoop: scoop install dua
    • WinGet: winget install Byron.dua-cli
    • Cargo: cargo +nightly install dua-cli

    Cargo (Rust)

    • Unix: cargo install dua-cli
    • No TUI (Most compatible): cargo install dua-cli --no-default-features
    • With TUI (Cross-platform): cargo install dua-cli --no-default-features --features tui-crossplatform
    brew install dua-cli
  2. Launch Interactive Mode

    main

    Launch an interactive terminal user interface (TUI) to explore and delete files/directories to release space. Use ? to see keyboard shortcuts.

    To localize the help screen, set the standard POSIX locale environment variables (e.g., LANG or LC_ALL). Japanese (ja) is supported.

    # Launch interactive mode
    dua i
    
    # Launch with Japanese help screen
    LANG=ja_JP.UTF-8 dua i
    dua i
  3. Use the `dua` core library for traversal and aggregation

    main
    The dua crate provides a public API for filesystem traversal, in-memory tree representation, and data aggregation. It can be used as a library to perform high-performance disk usage analysis within your own Rust applications.
  4. Configure dua-cli

    main

    Configuration is handled via a config.toml file located in your OS-specific config directory:

    • Linux/Unix: $XDG_CONFIG_HOME/dua-cli/config.toml (or platform default)
    • macOS: ~/Library/Application Support/dua-cli/config.toml
    • Windows: %APPDATA%\dua-cli\config.toml

    Supported Configuration Keys

    [keys]

    • esc_navigates_back (boolean):
      • If true (default), pressing <Esc> in the main pane ascends to the parent directory.
      • If false, pressing <Esc> follows the default quit behavior.
    [keys]
    # If true (default), pressing <Esc> in the main pane ascends to the parent directory.
    esc_navigates_back = true
  5. Configure filesystem walking with WalkOptions

    main

    The WalkOptions struct allows you to customize how dua-cli traverses the filesystem. Key configuration fields include:

    • threads: Number of threads to use for traversal (0 for default, 1 for serial).
    • count_hard_links: If true, counts every hard-link occurrence independently.
    • apparent_size: If true, uses metadata.len() instead of actual disk allocation.
    • sorting: Determines the order of entries (e.g., TraversalSorting::AlphabeticalByFileName).
    • cross_filesystems: If false, traversal is restricted to the root filesystem/device.
    • ignore_dirs: A set of canonicalized paths to skip during traversal.
    #[derive(Clone)]
    pub struct WalkOptions {
        pub threads: usize,
        pub count_hard_links: bool,
        pub apparent_size: bool,
        pub sorting: TraversalSorting,
        pub cross_filesystems: bool,
        pub ignore_dirs: BTreeSet<PathBuf>,
    }
  6. Use dua-cli for disk usage analysis

    main

    Use the dua command to analyze disk space in your current directory or specific paths.

    • Analyze current directory: Run dua to see space usage of the current working directory.
    • Analyze non-hidden directories: Run dua * to count space used in all directories that are not hidden.
    • Help: Use dua aggregate --help to learn about additional functionality.
    # count the space used in the current working directory
    dua
    
    # count the space used in all directories that are not hidden
    dua *
    
    # learn about additional functionality
    dua aggregate --help
  7. Render the MainWindow component

    main

    The MainWindow struct is the primary container for the dua-cli interactive user interface. It manages the layout and rendering of several sub-panes, including the Entries pane, HelpPane, MarkPane, and GlobPane.

    To render the main window, call the render method, providing MainWindowProps, the target Rect area, the TUI Buffer, and a mutable reference to the Cursor.

    impl MainWindow {
        pub fn render<'a>(
            &mut self,
            props: impl Borrow<MainWindowProps<'a>>,
            area: Rect,
            buffer: &mut Buffer,
            cursor: &mut Cursor,
        ) {
            // ... implementation
        }
    }
  8. Determine exit code from WalkResult

    main

    The WalkResult struct tracks errors encountered during a filesystem walk. You can use the to_exit_code() method to convert the result into a standard process exit code:

    • Returns 0 if no I/O errors occurred.
    • Returns 1 if one or more I/O errors were encountered.
    impl WalkResult {
        /// Convert traversal result into a process exit code.
        ///
        /// Returns `0` if no I/O errors occurred, otherwise `1`.
        pub fn to_exit_code(&self) -> i32 {
            i32::from(self.num_errors > 0)
        }
    }
  9. Canonicalize ignore directory paths

    main

    When providing a list of directories to ignore, use canonicalize_ignore_dirs to ensure paths are resolved correctly before traversal begins. This function uses gix::path::realpath to resolve paths; non-canonicalizable paths are silently ignored.

    pub fn canonicalize_ignore_dirs(ignore_dirs: &[PathBuf]) -> BTreeSet<PathBuf>
  10. Configure byte formatting with ByteFormat

    main

    The ByteFormat enum defines how disk usage sizes are displayed. You can choose between metric (base 1000), binary (base 1024), raw bytes, or specific units like GB, GiB, MB, or MiB.

    Use the .display(bytes) method to create a formatter that implements fmt::Display for a given number of bytes.

    #[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
    pub enum ByteFormat {
        /// metric format, based on 1000.
        #[serde(rename = "metric")]
        Metric,
        /// binary format, based on 1024
        #[serde(rename = "binary")]
        Binary,
        /// raw bytes, without additional formatting
        #[serde(rename = "bytes")]
        Bytes,
        /// only gigabytes without smart-unit
        #[serde(rename = "gb")]
        GB,
        /// only gibibytes without smart-unit
        #[serde(rename = "gib")]
        GiB,
        /// only megabytes without smart-unit
        #[serde(rename = "mb")]
        MB,
        /// only mebibytes without smart-unit
        #[serde(rename = "mib")]
        MiB,
    }
  11. Sort modes available in interactive mode

    main

    When using dua in interactive mode, you can sort entries using several modes. These modes determine the order of files and directories in the view.

    Available SortMode variants:

    • SizeDescending: Largest to smallest.
    • SizeAscending: Smallest to largest.
    • MTimeDescending(MTimeSort): Newest to oldest.
    • MTimeAscending(MTimeSort): Oldest to newest.
    • CountDescending: Most entries to fewest.
    • CountAscending: Fewest entries to most.
    • NameDescending: Z to A.
    • NameAscending: A to Z.

    Note that MTimeSort (the sub-mode for modification time) can be configured as:

    • Entry: Uses the entry's own modification time.
    • RecursiveChildrenNewest: Uses the newest modification time among all descendants.
    • RecursiveChildrenOldest: Uses the oldest modification time among all descendants.
  12. Set filesystem traversal sorting

    main

    The TraversalSorting enum specifies how entries are ordered during the filesystem walk:

    • None: Maintains the default filesystem iteration order.
    • AlphabeticalByFileName: Sorts entries alphabetically by their filename.
    #[derive(Clone)]
    pub enum TraversalSorting {
        /// Keep filesystem iteration order as provided by the walker.
        None,
        /// Sort entries alphabetically by file name during iteration.
        AlphabeticalByFileName,
    }