2025-10-23    2025-10-23    526 字  2 分钟

📘 Overview

On Linux, the command-line tool trash-cli provides a safe alternative to rm, allowing you to move files to the Trash instead of permanently deleting them.
It fully complies with the FreeDesktop Trash Specification, which is used by desktop environments such as GNOME and KDE.


🧰 Installation

You can install trash-cli using your package manager or Python’s pip:

1
2
3
sudo apt install trash-cli
# or
pip install trash-cli

🚫 Disable rm (for safety)

To avoid accidental permanent deletions, you can override the rm command and display a friendly warning instead.

Add this alias to your ~/.bashrc or ~/.zshrc:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
alias rm='echo "
⚠️  This is not the command you are looking for.

👉  Please use trash-cli instead. It is safer and provides these commands:

   trash-put / trash      →  Move files/directories to trash
   trash-empty            →  Empty the trashcan(s)
   trash-list             →  List trashed files
   trash-restore          →  Restore a trashed file
   trash-rm               →  Remove individual files from trash
"'

Then reload your shell:

1
source ~/.bashrc

Now, if you accidentally type rm, you’ll get a helpful message instead of deleting files permanently.


🧩 How trash-cli Manages Trash Directories

1. Per-Partition Trash Design

Linux doesn’t use a single, global trash folder.
Instead, trash-cli follows the FreeDesktop specification, which defines a separate trash directory for each filesystem (partition).

This prevents large files from being copied across devices when deleted.

Location TypeTrash Directory
Your Home Directory~/.local/share/Trash/
Other Filesystems/mountpoint/.Trash-$UID/ (e.g. /data0/.Trash-1000/)

(where $UID is your numeric user ID.)

Each trash directory contains two subfolders:

files/   →  stores deleted files
info/    →  stores metadata (.trashinfo files)

Example:

~/.local/share/Trash/
├── files/
│   └── test.png
└── info/
    └── test.png.trashinfo

Metadata example:

1
2
3
[Trash Info]
Path=/home/user/Desktop/test.png
DeletionDate=2025-10-23T09:15:00

2. How to Check Where Your Trash Is

Method 1: View home trash

1
ls -la ~/.local/share/Trash

Method 2: Search all trash directories

1
sudo find / -type d -name ".Trash*" 2>/dev/null

Method 3: List all trashed files

1
trash-list

Method 4: Check where a deleted file went

1
find / -type f -name "filename" 2>/dev/null | grep Trash

3. Why It’s Per-Partition

  • Prevents cross-device copying when deleting large files

  • Keeps trash on the same filesystem for safety and performance

  • Supports multiple users with separate .Trash-$UID directories


⚙️ Optional: Use a Global Trash Folder

If you really prefer a single global trash instead of per-filesystem behavior, you can override it (though it’s not standard):

1
2
3
4
5
trash-put() {
  mkdir -p "$HOME/.local/share/Trash"/{files,info}
  command trash --trash-dir="$HOME/.local/share/Trash" "$@"
}
alias trash='trash-put'

🧹 Common Commands

CommandDescription
trash-put / trashMove file to trash
trash-listList trashed files
trash-restoreRestore deleted file
trash-emptyEmpty all trash bins
trash-rm patternRemove specific trashed files

✅ Summary

ConceptExplanation
SystemFollows FreeDesktop Trash spec
Trash structurePer-filesystem (.Trash-$UID)
Home trash location~/.local/share/Trash
Metadata folderinfo/ stores .trashinfo files
Safe deleteUse trash-put instead of rm
RestoreUse trash-restore
EmptyUse trash-empty