How to Make rm Move Files to the Trash (Aliases Fail)

2026-11-28
Brian

Subscribe
Get New Posts by Email
New RepoSquare posts in your inbox: comparisons, setup guides and explainers on open-source tools and AI coding agents. No spam, and you can unsubscribe anytime.

To make rm move files to the Trash, install trash-cli and type trash instead of rm. Files go to the same Trash your desktop uses, and trash-restore puts them back. Do not alias rm to it. In my test the alias only worked in an interactive terminal: scripts, bash -c and my AI coding agent still deleted for real.

You type rm on the wrong file, press Enter, and it is gone. No Trash, no undo, no “are you sure”. On a desktop you would drag it back out of the bin. In a terminal there is no bin.

Below: why rm cannot be undone, how to give your terminal a real Trash in two minutes, and which popular fix only looks safe. One idea runs through all of it: a safety net only counts if it catches you in every shell, not just the one you are looking at.

Make rm move files to trash: rm deletes for good, trash sends the file to a Trash you can restore from

Why can’t you undo rm?

Mind map of making rm move files to trash: install trash-cli, trash-put, trash-restore, alias only works in interactive shells, PATH wrapper, Claude Code skipped the alias below the guard, trash-empty 30

Because rm does not move the file anywhere. It removes the name, and the space gets reused.

Start with how a file is stored. The data sits on the disk, and a folder only holds a name that points to it. Think of a library card that says which shelf a book is on.

rm tears up the card. Under the hood it calls unlink (the system call that deletes a file’s name). Once no name points to the data, the system marks that space as free and writes over it whenever it needs room.

That is why nothing asks you first. There is no “deleted items” stop on the way. Recovery tools can sometimes find the old data before it is overwritten, but that is luck, not a feature.

So the fix is not a better undo. The fix is to stop tearing up the card, and to move the book to a holding shelf instead.

rm removes a file's name and marks its disk space free, so the data gets overwritten

Make rm move files to the Trash with trash-cli

Install trash-cli, then use trash-put to delete, trash-restore to undo and trash-empty to clean up.

trash-cli is a small free tool, open source under the GPL v2 license. It moves files into the same Trash that GNOME, KDE and XFCE use, so your file manager sees them too. Install it one of these ways:

  1. Ubuntu or Debian: sudo apt install trash-cli
  2. Fedora: sudo dnf install trash-cli
  3. Arch: sudo pacman -S trash-cli
  4. Any system with Python: pipx install trash-cli

That gives you five commands, and you can learn them in a minute:

  • trash-put (or just trash) moves files to the Trash.
  • trash-list shows what is in it.
  • trash-restore brings one back.
  • trash-rm deletes one item for good.
  • trash-empty clears the lot.

Here is a real run. I trashed two files, listed them, and restored one by picking its number:

Terminal run of trash-cli: trash-put, trash-list, the trashinfo file and trash-restore

One habit to know: trash-restore with no argument only lists files that came from the folder you are standing in. Run it from the original folder, or pass the full path, like trash-restore /home/you/myproject/notes.txt.

On a Mac you may not need to install anything. macOS 15 Sequoia and later ship a built-in trash command at /usr/bin/trash that moves files into your user Trash.

Where trashed files actually go

Into a hidden folder in your home directory, with a small note that remembers where each file came from.

Knowing the spot matters, because it is how you rescue a file when the tools are not around. On Linux, anything you trash from your home drive lands in ~/.local/share/Trash/. Inside are two folders.

files/ holds the files themselves. info/ holds one .trashinfo file per item (a tiny text note with the original path and the time you deleted it). That note is how trash-restore knows where to put things back.

Trash two files with the same name and the second becomes dup.txt_1, so nothing gets overwritten.

Linux Trash folder layout: files and info folders, other drives and the RAM disk caveat

Files on other drives work differently. Trash something on a USB stick or a second disk and it goes into a hidden .Trash-1000 folder on that drive (the number is your user ID). That keeps the move instant, because nothing is copied between disks.

Here is the catch I hit. I trashed a file from a RAM disk (a drive that lives in memory and is wiped on reboot), and trash-list did not show it until I pointed it at that folder with --trash-dir. A Trash on a RAM disk also dies with the next restart.

One more limit: the trash-cli docs say it does not support Btrfs volumes. That matters on Fedora Workstation, which uses Btrfs by default, so test a trash and restore there before you rely on it.

Should you alias rm to trash?

No. The alias only works in the terminal you are typing in, so it fails exactly where you need it.

An alias is a shortcut your shell swaps in before running a command. Putting alias rm='trash-put' in ~/.bashrc (the settings file Bash reads when it starts) feels like the obvious fix. The trash-cli docs say you can, but you shouldn’t, because it behaves differently from rm.

Why? Aliases only load in an interactive shell (a terminal where a person is typing). Scripts, cron jobs, find -exec and anything started with bash -c run a non-interactive shell, which skips aliases. Those shells run the real rm.

So I tested it. I added the alias to ~/.bashrc, made a file, and deleted it in different ways. The chart also shows the wrapper from the next section:

Test results: the rm alias used the Trash only in an interactive terminal, a PATH wrapper caught scripts too
  • An interactive terminal (bash -i): went to the Trash.
  • bash -c 'rm file': deleted for good.
  • A login shell (bash -l -c): deleted for good.
  • sh -c: deleted for good.
  • A shell script containing rm: deleted for good.
  • find . -exec rm {} \;: deleted for good.

One out of six. Worse, the alias trains your fingers to trust rm. Then one day you are on a server without your .bashrc, and rm does exactly what it always did.

There is a quieter problem too. trash-put looks like rm, but it is not the same program. It accepts -r and -f, yet ignores -r because it never needs it. A missing file returned exit code 74 in my test, where rm returns 1. A script that checks for exactly 1 would misread that.

The safer habit: type trash on purpose

Type trash on purpose, and keep rm for the times you really mean it.

The safest setup is the boring one. Use trash for everyday deleting, so the safety net is something you choose, not something you assume. When you truly want a file gone, rm is still there and still honest about what it does.

There is a stronger option, with a cost. A wrapper is a tiny script named rm that calls trash-put, placed in a folder that comes first in your PATH (the list of folders your shell searches for commands). Unlike an alias, every program that inherits that PATH finds the wrapper first.

#!/bin/sh
exec trash-put "$@"

I tested this too. With the wrapper folder first in PATH, bash -c, sh -c, a script and find -exec all sent the file to the Trash. Two things still got past it: calling /bin/rm by its full path, and find -delete, which deletes without calling rm at all.

Which safety net catches what: trash habit, PATH wrapper, alias, agent rules, backups

So is the wrapper worth it? It catches far more than the alias. But it also changes rm for every build script and installer that runs under your PATH, and some of them delete thousands of temporary files on purpose. That fills your Trash quietly. Use the wrapper inside one risky session, like a coding agent’s terminal, not system-wide.

Does a Trash protect you from AI coding agents?

Partly. Whether an agent sees your alias depends on where the alias sits in your settings file, so test it.

AI coding agents run shell commands for you, and most of those shells are non-interactive. I ran Claude Code’s Bash tool on Linux and asked it to rm a test file, twice.

First run: the alias sat at the bottom of ~/.bashrc. Ubuntu’s default ~/.bashrc, like many others, opens with a check that says “if not running interactively, don’t do anything” and stops right there. Claude Code’s shell never reached the alias, and the file was deleted for good.

Second run: I moved the alias to the very first line, above that guard. This time Claude Code reported rm is aliased to trash-put, and the file landed in the Trash. Plain bash -c in the same setup still deleted for real.

An alias placed above the bashrc non-interactive guard is seen by Claude Code, one below it is skipped

So a Trash helps with agents, but it is not a wall. An agent can still pick find -delete, Python or a full path. For real limits, use the agent’s own controls, covered step by step in how to stop Claude Code from deleting your files.

Empty the Trash on a schedule without losing the net

Clear only old items, so recent mistakes stay recoverable.

A Trash that never empties fills your disk. In my test, trashing a folder of 5,000 small files took about as long as rm did, a twentieth of a second, because on the same drive it is just a rename. That speed is great, until a build folder you trash daily has eaten gigabytes.

trash-empty takes a number of days. trash-empty 30 deletes only items trashed more than 30 days ago and keeps the rest. Add -f to skip the “are you sure” question, which you will need if it runs on a schedule.

# crontab -e, then add: every day at 9am, purge items older than 30 days
0 9 * * * trash-empty -f 30
trash-empty 30 purges items older than 30 days and keeps the rest restorable

That gives you a rolling month of undo. If disk space is already tight, the same “delete old things on a schedule” idea is in how to clean up Claude Code disk space. A Trash is still not a backup: a dead disk takes the Trash with it.

Install trash-cli, type trash tomorrow morning, and leave rm alone. The best safety net is the one you can see yourself using.

Get New Posts by Email
New RepoSquare posts in your inbox: comparisons, setup guides and explainers on open-source tools and AI coding agents. No spam, and you can unsubscribe anytime.

Leave a comment