How to Safely Delete and Restore Files in Linux Terminal Using trash-cli

One of the most intimidating aspects of using the Linux command line is the finality of file deletion. When you run rm or rm -rf, the operating system unlinks the file from the filesystem immediately—bypassing the desktop graphical trash can completely. There is no confirmation dialogue by default, no native undo shortcut, and no simple way to recover accidentally erased documents, project source code, or configuration files without complex forensic carving.

To eliminate the risk of accidental deletion while retaining command-line speed, Linux administrators and desktop users rely on trash-cli. Implementing the FreeDesktop.org Trash specification, trash-cli provides native terminal commands that move files and directories directly to your desktop desktop trash bin. In this guide, we explore how to install trash-cli, use its command suite to safely remove and restore files, alias dangerous commands, and automate trash maintenance.

The Risk of rm vs the Protection of trash-cli

Understanding how trash-cli integrates with the underlying Linux desktop environment highlights why it is a critical utility for everyday productivity:

Feature Standard rm Command trash-cli Utility User Benefit
Deletion Target Unlinks inodes directly from disk Moves files to ~/.local/share/Trash Complete recovery capability
GUI Integration Bypasses graphical recycle bin Syncs with GNOME / KDE Trash Can View and restore items via graphical desktop
Restore Mechanism Requires complex data recovery tools Native interactive terminal restoration Instant restoration with original paths
Accidental Directory Purge Instant, unrecoverable data loss Safe, reversible archiving Eliminates catastrophic typographical errors

When performing broader maintenance alongside file organisation, you can also safely free up disk space in Ubuntu using the terminal to remove unused cached packages and system logs.

Step-by-Step Tutorial: Installing trash-cli Across Linux Distributions

The trash-cli package is maintained in the official software repositories of all major Linux distributions:

  1. Open your terminal application (e.g. GNOME Terminal, Konsole, or Kitty).
  2. Update your package index and install trash-cli using your distribution’s native package manager:
    • Ubuntu / Debian / Linux Mint:
      sudo apt update && sudo apt install trash-cli
    • Fedora / RHEL:
      sudo dnf install trash-cli
    • Arch Linux / Manjaro:
      sudo pacman -S trash-cli
  3. Verify the installation by running:
    trash --version

The Essential trash-cli Command Suite

The package installs five core utilities that correspond to standard graphical trash interactions:

  • trash-put <file>: Moves a specified file or directory to the trash bin (or simply trash <file>).
  • trash-list: Lists all items currently held in the trash, displaying original paths and exact deletion timestamps.
  • trash-restore: Launches an interactive prompt to select and recover previously trashed files to their original locations.
  • trash-rm <pattern>: Permanently deletes individual matching files from the trash.
  • trash-empty: Permanently clears all contents from the trash can.

Interactive File Restoration

Recovering a deleted document using trash-restore requires only a single command:

  1. In your terminal, navigate to any directory and execute:
    trash-restore
  2. The utility scans your trash bin and outputs a numbered list of deleted items showing the deletion date, time, and original file path:
    0 2026-10-12 11:00:15 /home/user/Documents/quarterly_budget.xlsx
    1 2026-10-12 10:45:20 /home/user/Projects/client_brief.docx
    2 2026-10-12 09:12:04 /home/user/Downloads/setup_script.sh
  3. Type the index number corresponding to the file you want to recover (for example, 0) and press Enter.
  4. trash-restore immediately extracts the file from the trash storage directory and returns it to its original folder with permissions intact.

Aliasing rm for Automated Protection

To prevent muscle memory from accidentally invoking rm, you can create a shell alias that redirects file removals to trash-put:

  1. Open your shell configuration file in a text editor (e.g. nano ~/.bashrc or nano ~/.zshrc).
  2. Scroll to the end of the file and append the following lines:
    # Safe file deletion alias
    alias rm='trash-put'
    alias tp='trash-put'
    alias tl='trash-list'
    alias tr='trash-restore'
  3. Save and close the file (in nano, press Ctrl + O then Ctrl + X).
  4. Reload your shell configuration by executing:
    source ~/.bashrc
  5. Now, whenever you type rm filename.txt, the file will be safely sent to your recycle bin rather than permanently expunged. If you ever genuinely require raw unbuffered deletion, you can bypass the alias by prepending a backslash: \rm filename.txt.
  6. For maintaining storage integrity over time, you can also clean and vacuum systemd journal logs using journalctl to prevent logs from accumulating unchecked.

Frequently Asked Questions

Where does trash-cli store deleted files on Linux?

For files located on your home partition, trash-cli stores them in ~/.local/share/Trash/files with metadata recorded in ~/.local/share/Trash/info. For external USB drives or secondary partitions, it uses a hidden .Trash-$UID directory at the root of that drive.

Can I empty items older than a specific number of days?

Yes. The trash-empty command accepts a days parameter. Running trash-empty 30 permanently purges only items that have been in the trash for more than 30 days, leaving recently trashed items intact.

Does trash-cli work over SSH terminal sessions?

Yes. Because trash-cli is a pure CLI tool written in Python that complies with standard FreeDesktop specifications, it functions seamlessly across remote headless servers, SSH sessions, and Docker development containers.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.