I deleted /bin/rm. How do I recover it?

sudo touch /bin/rm && sudo chmod +x /bin/rm
apt-get download coreutils
sudo dpkg --unpack coreutils*

And never again.


Why didn't you use sudo with apt-get?

Because the download command doesn't require it:

download
download will download the given binary package into the current directory.

So, unless you are in some directory you can't write, you don't need to use sudo, and it could get problematic later on since you will need root permissions to remove/move the package.


debian and its derivatives (and probably most other distributions) come with busybox which is used in the initramfs.

busybox bundles most core command line utilities in a single executable.

You can temporarily symlink /bin/rm to /bin/busybox:

ln -s busybox /bin/rm

To get a working rm (after which you can do your apt-get install --reinstall coreutils).

That same method can be used for all the other utilities that busybox includes. That list varies from one deployment to another. You can get the list with busybox --list.

Note however that they are limited versions of the corresponding utilities. They sometimes support GNU extensions, but generally not and some of them will not even support all the standard/POSIX features (some features can be enabled/disabled at compile time).

Alternatively, you could use zsh's builtin rm:

#! /bin/zsh
zmodload zsh/files
rm "$@"

The zsh/files module provides with a few additional builtin commands (rm, mv, ln, mkdir, rmdir, chown, chmod, sync). It's useful in this kind of situation or when you cannot fork more processes but do have an interactive zsh running.

ksh93 also has a number of extra/optional commands buitin, but not rm among them (basename, chmod, dirname, getconf, head, mkdir, logname, cat, cmp, cut, uname, wc, sync). You can invoke them with:

command /opt/ast/bin/the-command

in a ksh93 script.


In case apt-get or dpkg needs rm and without rm a reinstallation is not posssible, then you can emulate rm with perl:

cat > /bin/rm << "EOF"
#!/usr/bin/perl
foreach (@ARGV) { unlink $_ or warn "$@:$!"; }
EOF
chmod +x /bin/rm