Just for fun, I thought I would use this command on my Raspberry Pi running Raspbian:

sudo rm -f /bin/rm

I thought I could just reinstall coreutils: I was wrong!

apt-get install --reinstall coreutils gives an error from dpkg, saying it couldn't remove the package. Compiling from source doesn't work because the Makefile uses rm.

How can I get a working rm back?

  • 9
    Every .deb is basically an archive you can unpack and simply copy the rm executable to /bin.
    – schaiba
    Feb 18, 2014 at 16:16
  • 1
    What OS is this? Linux? Unix? Something else? If Linux, which distro? 64bit? 32?
    – terdon
    Feb 18, 2014 at 16:16
  • 12
    ln -s /usr/lib/initramfs-tools/bin/busybox /bin/rm (or /bin/busybox, or extract it from an initrd) Feb 18, 2014 at 16:34
  • 11
    The Raspberry Pi has its OS on an SD memory card so you can just extract the rm binary from the package on another machine and copy it back onto the card. In any case, hilarious stunt you pulled there and very bold of you to admit it publicly :)
    – Christian
    Feb 18, 2014 at 19:50
  • 18
    @user645715 37 people found this question amusing.
    – Brilliand
    Feb 19, 2014 at 17:10

5 Answers 5

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 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.

  • 3
    apt-get needs to be run with sudo :)
    – AWippler
    Feb 18, 2014 at 17:36
  • 35
    @AWippler no, in this case no.
    – Braiam
    Feb 18, 2014 at 22:14
  • 7
    Note that an empty executable file is executed as a shell script that does nothing. So that means invoking /bin/sh. It would have been simpler to do a sudo ln -s true /bin/rm or even better sudo ln -s busybox /bin/rm to have a working rm. Feb 20, 2014 at 9:59
  • 1
    apt-get does need sudo if you haven't downloaded coreutils because otherwise it won't be able to write to /var where debian saves the downloaded files. Feb 21, 2014 at 23:08
  • 3
    @KristopherIves wrong... apt-get downloads the package to the current directory always (yeah, even if you used sudo), so unless you are in a directory you can't write as your user you don't need sudo. Please check the man pages.
    – Braiam
    Feb 21, 2014 at 23:12

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 or invoke builtin the-command for the the-command builtin to be enabled and replace the external one.

  • 5
    Great answer. Makes me wonder if the OP really needs to reinstall the real rm at all. ;-) Feb 19, 2014 at 17:19
  • 1
    Indeed. Actually, this makes me think about deleting coreutils too... :-)
    – Damon
    Feb 20, 2014 at 16:05
  • 4
    @Damon: coreutils provide a extended version of the basic commands compared to busybox, so while busybox might be enough to match SUSv3 requirements, coreutils are more featureful; for example busybox has limited i18n support.
    – liori
    Feb 20, 2014 at 22:50
  • 2
    @liori: I was half-joking, though 10-15 years ago, I would probably indeed have done it in that situation (the added "features", in particular i18n, are not an advantage in my opinion -- unintellegible translations, and learning to use switches that unexpectedly break scripts on another computer, no thanks). However, nowadays, I'm happy if only a Linux system runs smoothly as-installed without me touching anything, and without having to move/delete/edit system/config files or binaries. Which sadly, still isn't the case often enough, so I'm surely not touching something that works :-)
    – Damon
    Feb 21, 2014 at 13:21

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"
foreach (@ARGV) { unlink $_ or warn "$@:$!"; }
chmod +x /bin/rm
  • 3
    Note that it doesn't handle -r, and the fact that it doesn't return a correct exit status may cause problems. Feb 18, 2014 at 16:58
  • 7
    @StephaneChazelas it is sufficient to allow apt-get install --reinstall coreutils and get the OP back to normal.
    – terdon
    Feb 18, 2014 at 17:02
  • that's why I wrote "in case". the package coreutils has no scripts. typically the pre- and post-install scripts fail on errors if files are exists or don't exist, for example if they need to be removed. And yes, it is true I was too lazy to check the package in advance.
    – user55518
    Feb 18, 2014 at 22:06
  • you could also emulate rm with mv (to 'trash bin') Sep 5, 2014 at 20:15

I would try obtaining the correct rm binary from another machine, and then using scp or something to copy it to the Pi. This of course only works if scp is already installed...

If scp is not available, then nc (a.k.a. netcat) on the sending side and bash with a /dev/tcp/HOST/PORT redirection on the receiving side might work as well.

If you don't have another Raspbian machine, you can retrieve the coreutils package (get the .deb for the right version), and unpack it with dpkg-deb (on Debian/Ubuntu/Mint/…, even if it isn't on a Pi):

dpkg-deb --fsys-tarfile coreutils*.deb | tar xf - ./bin/rm

If you don't have another machine with dpkg, you can extract the file with ar (from the binutils development tools) and tar:

ar p coreutils*.deb data.tar.gz  | tar xzf - ./bin/rm
  • 8
    If scp is not available, then nc (a.k.a. netcat) on the sending side and bash with a /dev/tcp/HOST/PORT redirection on the receiving side might work as well.
    – MvG
    Feb 19, 2014 at 19:06

Since it's debian (or ubuntu), there's an easy way to get the files:

mkdir /tmp/coreutils
sudo dpkg-deb --extract /var/cache/apt/archives/coreutils_ [tab complete for correct version].deb /tmp/coreutils
sudo cp /tmp/coreutils/bin/rm /bin

This works because apt-get downloaded the coreutils.deb before trying to install it, and dpkg-deb guaranteed to exist on a debian-based system.

Don't extract directly to /tmp, it changes permissions on the parent directory.

If you're going to play around, you may want to install the package busybox-static, which works even if you break everything else.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .