撤销上一个 shell 命令对文件系统的操作
Revert what the last shell command did to the filesystem. Website: undo.edaywalid.com
Works for the classic mistakes: rm / rm -rf, mv over a file you
needed, truncating with >, an accidental chmod -R, files and
directories created by a script that ran amok. Changed your mind again?
undo redo re-applies. Undo never deletes anything permanently, only
parks it in the session store, so a session toggles between undone and
applied as many times as you like.
Contents
shredThe shell has never had an undo, and the usual advice is that once data
is overwritten it is gone. The tools that do exist ask you to change
your habits first: alias rm to a trash command, or set up btrfs/zfs
snapshots before the accident. undo asks for nothing. You keep typing
rm, and the safety net is already under you, even when the deletion
happens three processes deep inside a build tool where no alias could
reach.
It is command-granular, not time-granular: it reverts exactly what that one command touched, on any filesystem, without root. That is the difference from a snapshot, which rolls the whole tree back to a point in time and takes your good work with it.
There is no snapshotting and no daemon. Nothing runs between your commands.
LD_PRELOAD
library, libundo.so, around every command you run.unlink, rename, open with write flags, rmdir, mkdir,
chmod, ...). Before each one goes through, it saves the affected
file into a per-command session and appends a line to a journal.
Deletions are saved by hardlink, so rm -rf on gigabytes copies
no data and costs almost nothing.undo reads the last session's journal and replays it in
reverse: relink deleted files, move renames back, swap truncated
files with their backups, remove accidental creations, recreate
directories with their original modes.Each command that changed something gets one session directory. The whole storage format is plain files you can inspect:
~/.local/share/undo/sessions/ (mode 0700)
└── 1784718280691946/
├── cmd rm -rf thesis/ the command line, for `undo list`
├── journal one line per change replayed in reverse
├── pid, done liveness markers so undo won't touch a running command
├── budget bytes stored so far shared by every process in the session
└── data/
├── 48211-1 = thesis/draft.md (hardlink, no data copied)
└── 48211-2 = thesis/refs.bibThe last 30 sessions are kept, within a 1 GiB budget; both are
configurable, and undo purge wipes the store. The most recent session
is always kept, even if it alone exceeds the budget, since that is the
one undo reverts.
Any distro, no root, nothing to configure:
curl -fsSL https://undo.edaywalid.com/install.sh | shThat installs into ~/.local, then asks whether to add the hook line
to your shell rc, showing you the exact line first. Say no and it prints
the instructions instead; it never edits anything when there is no
terminal to ask at (a CI run, a piped install). UNDO_MODIFY_RC=1
answers yes for scripted installs, UNDO_NO_MODIFY_RC=1 answers no.
Prefer a package manager?
| Channel | Command |
|---|---|
| Homebrew (Linux) | brew install edaywalid/tap/undo |
| Debian / Ubuntu | .deb from releases, then sudo dpkg -i undo_*.deb |
| Fedora / openSUSE | .rpm from releases, then sudo rpm -i undo_*.rpm |
| Arch | yay -S undo-cli-bin (AUR), or .pkg.tar.zst from releases |
| Nix | nix run github:edaywalid/undo (flake, experimental) |
| From source | make install (needs gcc + go, installs to ~/.local) |
If you let the installer do it, open a new terminal and skip to step 3. After a package install, or if you declined, add the line for your shell:
zsh
echo 'source /usr/share/undo/undo.zsh' >> ~/.zshrc && exec zshbash (5 or newer)
echo 'source /usr/share/undo/undo.bash' >> ~/.bashrc && exec bashfish (3.4 or newer)
echo 'source /usr/share/undo/undo.fish' >> ~/.config/fish/config.fish && exec fishThose paths are for a distro package. The hooks land somewhere else for the other channels:
| Installed with | Hook directory |
|---|---|
.deb, .rpm, AUR |
/usr/share/undo/ |
the one-liner, make install |
~/.local/share/undo/ |
| Homebrew | $(brew --prefix)/share/undo/ |
For Homebrew, let the shell resolve the prefix as you write the line, so your rc ends up with a plain path:
echo "source $(brew --prefix)/share/undo/undo.bash" >> ~/.bashrc && exec bashUse double quotes there. Left unexpanded, that runs brew on every
shell startup, and it is a Ruby program.
$ undo doctordoctor verifies the install and then actually deletes and restores a
canary file, so you get a real answer rather than a guess. Then try it
for yourself:
$ touch x
$ rm x
$ undoRun undo on its own line, not chained with &&. A chained line is a
single command to the shell, so undo would be running inside the very
session it is being asked to revert, before that command has finished.
No hook, or a script / CI context? Arm a single command instead, no setup required:
undo run -- ./sketchy-cleanup.shRequires Linux (amd64 or arm64) with glibc 2.6 or newer, which is every distribution still in use. See platform support for musl, macOS and WSL.
…The honest version, because it is the question everyone asks second.
Deleting costs no new disk, but it does defer freeing the old. When
you rm a file, undo makes a hardlink to it before the delete runs.
A hardlink is a second name for the same data, not a copy, which is why
rm -rf on gigabytes is instant and adds nothing. The catch: Linux only
frees a file's blocks when its last name disappears, and undo is still
holding one. So the space is not reclaimed until that backup is pruned.
If you deleted something specifically to free disk, run undo purge.
Overwriting a file in place does cost disk. A hardlink cannot
preserve content that is about to be rewritten, so for > truncation,
editors, and shred, undo copies the previous version. That copy is real
new space, capped per file by UNDO_MAX_BYTES (256 MiB default). The
same cap applies to deletions: the hardlink costs no new blocks, but it
is exactly what keeps the original ones from being freed.
Two ceilings apply while a command runs, enforced by the shim itself:
| Limit | Default | Meaning |
|---|---|---|
UNDO_MIN_FREE |
2 GiB | free space undo refuses to eat into. Checked against the store's filesystem as backups are written. |
UNDO_MAX_SESSION |
half of UNDO_MAX_STORE (512 MiB) |
how much one command may record. Derived so the store has room for more than one session. |
Hit either and the session stops recording, writes down why, and says so
at your next prompt. undo list marks it ! and undo show explains
the gap. The command itself is never blocked or slowed down; undo only
ever stops recording it.
This matters most for commands that do not return for hours: an editor, a dev server, an agent. The store-wide limits below only get a turn between commands, so before this they simply never ran for the session that was actually growing.
Two more apply between commands, when the prompt comes back:
| Limit | Default | Meaning |
|---|---|---|
UNDO_KEEP |
30 | how many commands are kept, not files. One rm -rf of 10,000 files is a single session and one undo restores all of them. |
UNDO_MAX_STORE |
1 GiB | total size budget for the whole store. Oldest sessions are dropped first. |
The most recent session is never pruned, even if it alone exceeds the
budget, since that is the one plain undo reverts. Older sessions that
bust the budget are dropped almost immediately, so a very large delete
stays undoable only until the next command unless you raise the limits.
Commands that changed nothing leave no session at all. undo gc prunes
on demand, undo purge empties the store completely.
If you are deleting something because it must be gone, know that undo
works against you by default: for an in-place overwrite it copies the
original bytes into its store before shred rewrites them, so a
readable copy survives. undo purge unlinks that copy but does not
overwrite it either.
Exclude the path first, then shred:
echo '/home/you/secrets' >> ~/.config/undo/ignore
shred -u ~/secrets/tax-return.pdfsudo shred also works, since sudo strips LD_PRELOAD and undo never
loads. Note that shred is itself unreliable on journaling filesystems
and SSDs, which its own man page explains.
A command that rewrites thousands of files (a package install, a build)
would otherwise flood undo list and the store with churn you will never
revert. The shim always skips the caches that build tools own and
recreate:
node_modules __pycache__ test-results playwright-report
.git .cache .turbo .next .nuxt .vite .svelte-kit .parcel-cache
.angular .nx .tox .pytest_cache .mypy_cache .ruff_cache
.gradle .terraform .dart_toolIt also collapses repeated writes to the same file within one command
down to a single backup. Names you might have picked yourself, like
dist, build, target or vendor, are deliberately not built in:
they hold generated output most of the time, but an accidental
rm -rf dist is a thing people want back. Add your own patterns in
~/.config/undo/ignore (see examples/ignore):
target # any path component named target, at any depth
dist
/home/you/scratch # an absolute path and everything under itSet UNDO_DEFAULT_IGNORE=0 to turn the built-ins off, or UNDO_IGNORE
to a colon-separated list to override the config file.
Environment variables, set before sourcing the hook:
| Variable | Default | Meaning |
|---|---|---|
UNDO_KEEP |
30 |
how many commands are kept (see storage) |
UNDO_MAX_STORE |
1 GiB | total store size budget in bytes; oldest pruned first |
UNDO_MAX_BYTES |
256 MiB | largest file the shim will back up, for deletions as well as overwrites |
UNDO_MIN_FREE |
2 GiB | stop recording when the store's filesystem has less than this free; 0 disables |
UNDO_MAX_SESSION |
half of UNDO_MAX_STORE |
stop recording when one command has stored this much; 0 disables |
UNDO_DATA_DIR |
~/.local/share/undo |
where sessions live |
UNDO_IGNORE |
from config file | colon-separated ignore patterns, overrides ~/.config/undo/ignore |
UNDO_IGNORE_FILE |
~/.config/undo/ignore |
where the ignore list is read from |
UNDO_DEFAULT_IGNORE |
on | set to 0 to stop skipping the built-in cache directories |
UNDO_CAPTURE_SHELL |
off | zsh only: set to 1 to re-exec |
暂无开放 Issues,或尚未同步最近议题。