<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>docs/adr/0002-external-trash-command.md - fork/gdu @ master</title>
<meta name="description" content="docs/adr/0002-external-trash-command.md in fork/gdu at master.">
<meta property="og:title" content="docs/adr/0002-external-trash-command.md - fork/gdu @ master">
<meta property="og:description" content="docs/adr/0002-external-trash-command.md in fork/gdu at master.">
<meta property="og:type" content="website">
<meta property="og:site_name" content="git.julia.blue">
<link rel="stylesheet" href="/css/style.css?v=62">
<script type="module" src="/js/main.js?v=62"></script>
</head>
<body>
<main data-ref="master" data-tip="c636c61a7fb7f1f9a9ac9306790b41a011cbabb9">
<noscript>
  <div class="callout">
    <p>
      hey! this is a static git repo viewer. commit diffs, file history,
      older commits and other revisions won't load without javascript.
    </p>
    <p>
      you are still able to view source files on branch/tag tips,
      but the experience is degraded.
    </p>
  </div>
</noscript>
<header class="repo"><div>
<h1>fork/gdu</h1>
</div>
<pre class="clone">git clone https://git.julia.blue/fork/gdu</pre>
</header>
<p class="snapshot-commit"><span class="commit-author">Daniel Milde</span><span class="commit-message" data-commit="b279b4168f4c93bd9268727714335ad0ec03a907">feat: add --trash-command option (#652)</span><span class="snapshot-sha sha" data-commit="b279b4168f4c93bd9268727714335ad0ec03a907">b279b41</span><time datetime="2026-09-02T16:51:38+02:00" title="2026-09-02 16:51:38 +0200">2026-09-02</time></p>
<header class="topbar"><details class="switcher"><summary>master</summary><div class="menu"><strong>branches</strong><a href="/fork/gdu/ref/ignore/">ignore</a><a class="current" href="/fork/gdu/ref/master/">master</a><a href="/fork/gdu/ref/subvolume/">subvolume</a><a class="all" href="/fork/gdu/refs">all refs →</a></div></details><nav class="crumbs"><a href="/fork/gdu/">gdu</a> / <a href="/fork/gdu/ref/master/tree/docs/">docs</a> / <a href="/fork/gdu/ref/master/tree/docs/adr/">adr</a> / <span class="cur">0002-external-trash-command.md</span></nav>
<span class="view-stats"><span>3.4 KiB</span><span>75 lines</span></span><span class="actions"><a class="raw" href="/fork/gdu/raw/fea59829061985e48d95e57a7d4b705276a1a1db/docs/adr/0002-external-trash-command.md">raw</a></span></header>
<div class="markdown-view"><input type="radio" name="markdown-view" id="markdown-rendered" checked><label for="markdown-rendered">rendered</label><input type="radio" name="markdown-view" id="markdown-code"><label for="markdown-code">code</label><section class="readme rendered"><h1>2. The external trash command receives the path as a shell argument</h1>
<p>Date: 2026-09-02</p>
<h2>Status</h2>
<p>Accepted</p>
<h2>Context</h2>
<p>The <code>D</code> key moves an item into the FreeDesktop trash. That implementation is
fixed: it always targets <code>$XDG_DATA_HOME/Trash</code>, and it exists only on
non-macOS Unix — darwin and Windows get a stub that errors out.</p>
<p>Users asked for the escape hatch ncdu offers as <code>--delete-command</code>
(issue #640): a custom trash directory via <code>trash-put --trash-dir DIR</code>, or a
working <code>D</code> key on macOS via <code>trash</code>/<code>gio trash</code>.</p>
<p>ncdu appends the absolute path to the given string and evaluates the result in
a shell. Appending the path <em>textually</em> means a file named <code>; rm -rf ~</code> becomes
shell syntax. Not using a shell at all avoids that, but then <code>~</code> and <code>$HOME</code> in
a command read from <code>~/.gdu.yaml</code> are never expanded, which is exactly the
"custom trash dir" case the option exists for.</p>
<p>Appending also assumes the command ends where an argument can follow. That
holds for <code>trash-put</code> and <code>gio trash --</code>, but not for <code>mv</code>, which takes its
destination last. Testing showed the natural reaction is to write
<code>mv "$1" ~/Trash</code>, which under an unconditional append receives the path twice.</p>
<p>A second question is when to drop the item from the in-memory tree. gdu has no
per-item refresh; it only knows how to remove a row. The command is arbitrary,
so exit code 0 does not imply the item is gone.</p>
<h2>Decision</h2>
<p>The command is evaluated by <code>/bin/sh -c '&lt;command&gt; "$@"' gdu &lt;abspath&gt;</code>.</p>
<p>The shell is kept, so expansion and other shell syntax work. The path is passed
as a positional parameter rather than concatenated, so item names cannot be
interpreted as shell syntax. <code>/bin/sh</code> is used rather than <code>$SHELL</code>, because
<code>"$@"</code> must behave predictably.</p>
<p>The <code>"$@"</code> suffix is omitted when the command already refers to the path via
<code>$1</code>, <code>$@</code> or <code>$*</code>. The path is then wherever the user put it, which is what
commands taking their destination last need.</p>
<p>The tree is updated from the filesystem, not from the exit code: after a
successful command gdu stats the path and only removes the row if the path is
really gone.</p>
<p>The command runs without a terminal and the UI is not suspended, so interactive
commands are unsupported.</p>
<p>Windows returns "not supported", matching the existing built-in trash stub.</p>
<h2>Consequences</h2>
<ul>
<li>A command without a placeholder must be a prefix that a path can be appended
to. <code>gio trash --</code> works; a command ending in <code>;</code> or a pipeline does not.
Writing <code>"$1"</code> lifts the constraint, so unlike ncdu there is always a way out.</li>
<li>The placeholder rule is implicit: a command containing <code>$@</code> for an unrelated
reason never receives the path. It then trashes nothing, and because the tree
is updated from the filesystem the row simply stays — visible, not silent
corruption.</li>
<li>A command that succeeds without removing the item (an interactive command
answered "no", or a typo like <code>ls</code>) leaves the row in place silently. The
tree never claims something was trashed when it wasn't, at the cost of a
key press that appears to do nothing.</li>
<li>An item <em>moved elsewhere</em> by the command is dropped from the tree rather than
re-parented, since gdu does not rescan. Same limitation as ncdu.</li>
<li>macOS users get a working <code>D</code> key by pointing the option at a trash CLI. This
is a user-configured workaround, not autodetection — probing for <code>trash</code> or
<code>gio</code> on macOS remains open.</li>
<li>Windows gains nothing here. A Recycle Bin implementation needs
<code>SHFileOperation</code>, not a command line.</li>
</ul>
</section><div class="code-panel"><pre class="code src language-markdown"><span class="code-line"><a class="ln" id="L1" href="#L1">1</a><span class="code-text"><a-p>#</a-p> <a-tt>2. The external trash command receives the path as a shell argument</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L2" href="#L2">2</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L3" href="#L3">3</a><span class="code-text">Date: 2026-09-02</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L4" href="#L4">4</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L5" href="#L5">5</a><span class="code-text"><a-p>##</a-p> <a-tt>Status</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L6" href="#L6">6</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L7" href="#L7">7</a><span class="code-text">Accepted</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L8" href="#L8">8</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L9" href="#L9">9</a><span class="code-text"><a-p>##</a-p> <a-tt>Context</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L10" href="#L10">10</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L11" href="#L11">11</a><span class="code-text">The `D` key moves an item into the FreeDesktop trash. That implementation is</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L12" href="#L12">12</a><span class="code-text">fixed: it always targets `$XDG_DATA_HOME/Trash`, and it exists only on</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L13" href="#L13">13</a><span class="code-text">non-macOS Unix — darwin and Windows get a stub that errors out.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L14" href="#L14">14</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L15" href="#L15">15</a><span class="code-text">Users asked for the escape hatch ncdu offers as `--delete-command`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L16" href="#L16">16</a><span class="code-text">(issue #640): a custom trash directory via `trash-put --trash-dir DIR`, or a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L17" href="#L17">17</a><span class="code-text">working `D` key on macOS via `trash`/`gio trash`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L18" href="#L18">18</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L19" href="#L19">19</a><span class="code-text">ncdu appends the absolute path to the given string and evaluates the result in</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L20" href="#L20">20</a><span class="code-text">a shell. Appending the path *textually* means a file named `; rm -rf ~` becomes</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L21" href="#L21">21</a><span class="code-text">shell syntax. Not using a shell at all avoids that, but then `~` and `$HOME` in</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L22" href="#L22">22</a><span class="code-text">a command read from `~/.gdu.yaml` are never expanded, which is exactly the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L23" href="#L23">23</a><span class="code-text">&quot;custom trash dir&quot; case the option exists for.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L24" href="#L24">24</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L25" href="#L25">25</a><span class="code-text">Appending also assumes the command ends where an argument can follow. That</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L26" href="#L26">26</a><span class="code-text">holds for `trash-put` and `gio trash --`, but not for `mv`, which takes its</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L27" href="#L27">27</a><span class="code-text">destination last. Testing showed the natural reaction is to write</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L28" href="#L28">28</a><span class="code-text">`mv &quot;$1&quot; ~/Trash`, which under an unconditional append receives the path twice.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L29" href="#L29">29</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L30" href="#L30">30</a><span class="code-text">A second question is when to drop the item from the in-memory tree. gdu has no</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L31" href="#L31">31</a><span class="code-text">per-item refresh; it only knows how to remove a row. The command is arbitrary,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L32" href="#L32">32</a><span class="code-text">so exit code 0 does not imply the item is gone.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L33" href="#L33">33</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L34" href="#L34">34</a><span class="code-text"><a-p>##</a-p> <a-tt>Decision</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L35" href="#L35">35</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L36" href="#L36">36</a><span class="code-text">The command is evaluated by `/bin/sh -c &#39;&lt;command&gt; &quot;$@&quot;&#39; gdu &lt;abspath&gt;`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L37" href="#L37">37</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L38" href="#L38">38</a><span class="code-text">The shell is kept, so expansion and other shell syntax work. The path is passed</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L39" href="#L39">39</a><span class="code-text">as a positional parameter rather than concatenated, so item names cannot be</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L40" href="#L40">40</a><span class="code-text">interpreted as shell syntax. `/bin/sh` is used rather than `$SHELL`, because</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L41" href="#L41">41</a><span class="code-text">`&quot;$@&quot;` must behave predictably.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L42" href="#L42">42</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L43" href="#L43">43</a><span class="code-text">The `&quot;$@&quot;` suffix is omitted when the command already refers to the path via</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L44" href="#L44">44</a><span class="code-text">`$1`, `$@` or `$*`. The path is then wherever the user put it, which is what</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L45" href="#L45">45</a><span class="code-text">commands taking their destination last need.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L46" href="#L46">46</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L47" href="#L47">47</a><span class="code-text">The tree is updated from the filesystem, not from the exit code: after a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L48" href="#L48">48</a><span class="code-text">successful command gdu stats the path and only removes the row if the path is</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L49" href="#L49">49</a><span class="code-text">really gone.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L50" href="#L50">50</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L51" href="#L51">51</a><span class="code-text">The command runs without a terminal and the UI is not suspended, so interactive</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L52" href="#L52">52</a><span class="code-text">commands are unsupported.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L53" href="#L53">53</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L54" href="#L54">54</a><span class="code-text">Windows returns &quot;not supported&quot;, matching the existing built-in trash stub.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L55" href="#L55">55</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L56" href="#L56">56</a><span class="code-text"><a-p>##</a-p> <a-tt>Consequences</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L57" href="#L57">57</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L58" href="#L58">58</a><span class="code-text"><a-p>- </a-p>A command without a placeholder must be a prefix that a path can be appended</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L59" href="#L59">59</a><span class="code-text"><a-p>  </a-p>to. `gio trash --` works; a command ending in `;` or a pipeline does not.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L60" href="#L60">60</a><span class="code-text"><a-p>  </a-p>Writing `&quot;$1&quot;` lifts the constraint, so unlike ncdu there is always a way out.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L61" href="#L61">61</a><span class="code-text"><a-p>- </a-p>The placeholder rule is implicit: a command containing `$@` for an unrelated</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L62" href="#L62">62</a><span class="code-text"><a-p>  </a-p>reason never receives the path. It then trashes nothing, and because the tree</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L63" href="#L63">63</a><span class="code-text"><a-p>  </a-p>is updated from the filesystem the row simply stays — visible, not silent</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L64" href="#L64">64</a><span class="code-text"><a-p>  </a-p>corruption.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L65" href="#L65">65</a><span class="code-text"><a-p>- </a-p>A command that succeeds without removing the item (an interactive command</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L66" href="#L66">66</a><span class="code-text"><a-p>  </a-p>answered &quot;no&quot;, or a typo like `ls`) leaves the row in place silently. The</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L67" href="#L67">67</a><span class="code-text"><a-p>  </a-p>tree never claims something was trashed when it wasn&#39;t, at the cost of a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L68" href="#L68">68</a><span class="code-text"><a-p>  </a-p>key press that appears to do nothing.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L69" href="#L69">69</a><span class="code-text"><a-p>- </a-p>An item *moved elsewhere* by the command is dropped from the tree rather than</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L70" href="#L70">70</a><span class="code-text"><a-p>  </a-p>re-parented, since gdu does not rescan. Same limitation as ncdu.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L71" href="#L71">71</a><span class="code-text"><a-p>- </a-p>macOS users get a working `D` key by pointing the option at a trash CLI. This</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L72" href="#L72">72</a><span class="code-text"><a-p>  </a-p>is a user-configured workaround, not autodetection — probing for `trash` or</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L73" href="#L73">73</a><span class="code-text"><a-p>  </a-p>`gio` on macOS remains open.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L74" href="#L74">74</a><span class="code-text"><a-p>- </a-p>Windows gains nothing here. A Recycle Bin implementation needs</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L75" href="#L75">75</a><span class="code-text"><a-p>  </a-p>`SHFileOperation`, not a command line.</span></span></pre></div></div>
</main>
</body>
</html>
