Skip to content
AjaiKNPublic

About

A bash CLI for putting files in the trash (WIP)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

zap.sh: a bash CLI for putting files in the trash (WIP)

Installation

With homebrew

brew install ajaikn/tap/zap.sh

Manual

  • Copy or symlink bin/zap into a directory on your PATH.
  • To install zsh completions (optional): copy completions/zsh/_zap into your zsh completions directory, or source zap.plugin.zsh.

Usage

Usage: zap [OPTION]... [--] FILE...
  or:  zap --list
  or:  zap --restore [OPTION]... [--] FILE...
Put each FILE in the trash. Currently supports macOS and Linux.

  -h, --help               Show this usage message.
  -f, --force              Override non-writable files, ignore nonexistent files, and skip untrashable ones.
                           Like rm -f: files that don't have write permissions are trashed anyway, nonexistent
                           files are silently ignored, and files that can't be trashed (e.g. because their
                           directories aren't writable) are reported and skipped while everything else is
                           still trashed, in which case the exit status is 1. Without --force, this command
                           refuses to do anything if any of the arguments would fail to be trashed.
  -i                       Prompt before trashing each file.
  -I                       Prompt once before trashing more than three files, or when trashing recursively
                           (with -r).
      --interactive[=WHEN] Prompt according to WHEN: never, once (-I), or always (-i). Without WHEN,
                           always prompt. By default, prompt before trashing a write-protected file if
                           standard input is a terminal, and otherwise refuse (unless --force is given).
                           -i and -I cancel an earlier --force, and --force cancels an earlier -i or -I.
  -n, --dry-run            Don't actually trash anything; just print what this command *would* do.
                           This option implies --verbose.
  -v, --verbose            Be more verbose.
  -s, --strategy=STRATEGY  Specify which trashing strategy to use.
                           Can also be specified with the ZAP_STRATEGY environment variable, which this
                           option supersedes.
  -l, --list               List trashed files (oldest first) as DELETIONDATE<TAB>ORIGINALPATH.
      --restore            Restore each FILE (given by its original path) from the trash. If a path was
                           trashed more than once, restore the most recent one. Refuses to overwrite.
                           --list and --restore work regardless of strategy. They see freedesktop.org
                           trash directories, plus items zap trashed with the macOS strategies (recorded
                           in $XDG_DATA_HOME/zap/info). Items trashed with other tools on macOS aren't
                           seen.
      --one-file-system    Refuse to trash a directory that has another filesystem mounted somewhere
                           inside it (default). Otherwise the mount would go into the trash with it, and
                           emptying the trash would delete what's on it. (rm skips just those parts, but
                           zap trashes each FILE whole.)
      --no-one-file-system Trash such directories anyway.
      --no-preserve-root   Don't treat '/' specially.
      --preserve-root      Refuse to trash '/' (default). With --force, '/' is skipped but the other
                           arguments are still trashed, and the exit status is 1.
      --allow-root         Trash files even when running as root (e.g. under sudo). Without this, zap
                           refuses, since they'd go to root's trash rather than yours. Can also be
                           enabled with ZAP_ALLOW_ROOT=1. Not needed for the dangerous_* strategies.
  -d, --dir                Does nothing. For compatibility with rm.
  -r, -R, --recursive      Does nothing (except with -I). For compatibility with rm.

*Available strategies*
  freedesktop              (default on Linux) Use the freedesktop.org Trash Specification:
                           https://specifications.freedesktop.org/trash-spec/1.0/
  macos_trash_command      (default on macOS >=15.0) Use the built-in macOS /usr/bin/trash command
  macos_applescript        (default on macOS <15.0) Use AppleScript
  macos_mv                 (default on macOS as root) Use mv to move to ~/.Trash/. Under sudo, that's the
                           invoking user's trash, like Finder.
  gio                      Use the 'gio trash' command
  kioclient                Use the 'kioclient move FILE... trash:/' command
  trash_cli                Use trash-put from trash-cli: https://github.com/andreafrancia/trash-cli
  dangerous_rm             Use rm
  dangerous_shred          Use GNU shred

Limitations

  • File names ending in a newline aren’t supported; zap refuses to trash (or restore) them.

Restoring on macOS

macOS doesn’t record where trashed files came from in a form other tools can read, so when zap trashes something with one of the macos_* strategies, it writes its own record (a .trashinfo file with extra X-Zap-* keys) to $XDG_DATA_HOME/zap/info (default ~/.local/share/zap/info). zap --list and zap --restore use these records, so they only know about files that zap itself trashed.

Comparison table

Based on gtrash’s comparison table.

AjaiKN/zap.shgtrashtrash-clitrashytrash-dsindresorhus/trash-cliali-rantakari/trashnateshmbhat/rm-trashbashtrashgomi
Supports Linux?✅✅✅✅✅✅ (“not very good and not maintained”)❌✅ (but doesn’t use $XDG_DATA_HOME env var)✅✅
Supports macOS system trash can?✅❌❌❌❌✅✅❌❌❌ (previously supported?)
Supports Windows?❌❌❌✅❌✅❌❌❌✅
LanguageBashGoPythonRustDJavaScript (+ Swift binary on Mac)Objective-CBashBashGo
ArchitectureSingle scriptSingle binary & Multi subcommandsMulti commandsSingle binary & Multi subcommandsSingle binarySingle commandSingle binarySingle scriptSingle scriptSingle binary
Has rm-like interface✅✅✅❌✅✅ (ignores all flags)✅ (ignores all flags)✅✅✅
Command to put FILE in trashzap FILEgtrash put FILEtrash-put FILEtrashy FILE or trashy put FILEtrash FILEtrash FILEtrash FILErm-trash FILEtrash FILEgomi FILE
Command to list trash contentszap --listgtrash findtrash-listtrashy listtrash --list❌trash -l❌❌❌
Command to restore FILE from trashzap --restore FILEgtrash find REGEX --restoretrash-restoretrashy restore REGEXtrash --restore FILE ️❌❌❌❌gomi --restore
Command to permanently delete/empty FILE from trash❌gtrash find REGEX --rmtrash-rm FILEtrashy empty FILEtrash --delete FILE❌❌❌❌❌
Command to fully empty trash❌gtrash find --rmtrash-emptytrashy empty --alltrash --emptyavailable in separate projecttrash -e (or securely with trash -s)❌❌gomi --prune=0m
Restore with TUI❌gtrash restore❌❌❌❌❌❌❌gomi --restore
Restore as a group❌✅❌❌❌❌❌❌❌❌
Can show file and directory size❌✅❌❌❌N/A❌N/AN/A❌
Can show summary of trash cans (total items, size)❌✅❌❌❌N/A❌N/AN/A❌
Support FreeDesktop.org directorysize cache❌✅❌❌✅ (only put, cannot list)❌N/A❌❌❌
Support FreeDesktop.org external filesystem trash can✅✅✅✅❌✅N/A❌✅✅
Support FreeDesktop.org fallback to home trash✅✅✅❌N/AN/AN/A
Multiple strategies✅ (many)❌❌❌❌❌✅ (Finder/AppleScript or system API)❌❌✅ (XDG or “legacy”)
Size-based pruning❌✅❌❌❌❌❌❌❌❌
Date-based pruning❌✅trash-empty DAYS✅❌❌❌❌❌✅
Safe (show confirmation prompt before deleting by default)N/A✅❌✅❌❌✅N/AN/A✅
Sort trashed items by deletion date by default?✅✅❌✅❌N/A❌N/AN/A✅ (when pruning)

About

A bash CLI for putting files in the trash (WIP)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages