Accidentally overwrote a binary file on Linux? Here is how to restore it

A shell script went wild due to some bug, and the script overwrote a binary file /bin/ping. Here is how tor restores it.

/bin/ping erased

/bin/ping erased (Credit: https://dribbble.com/TonyBabel)


There are two ways to solve this problem.

ADVERTISEMENTS

Easy way: copy it from another server

Just scp file from another box running the same version of your Linux distribution:
$ sudo scp vivek@server1.cyberciti.biz:/bin/ping /bin/ping

Proper way: Search and reinstall package

First, query the package which provides FILE /bin/ping as per your Linux distro:

Debian/Ubuntu Linux user type

$ dpkg -S /bin/ping
iputils-ping: /bin/ping

Now just reinstall the iputils-ping package using apt-get command or apt command:
$ sudo apt-get --reinstall install iputils-ping

RHEL/SL/Scientific/Oracle Linux user type

$ yum provides /bin/ping
iputils-20071127-24.el6.x86_64 : Network monitoring tools including ping

Now just reinstall the iputils package using yum command:
$ sudo yum reinstall iputils

Fedora Linux user type

$ dnf provides /bin/ping
iputils-20161105-1.fc25.x86_64 : Network monitoring tools including ping

Now just reinstall the iputils package using dnf command:
$ sudo dnf reinstall iputils

Arch Linux user type

$ pacman -Qo /bin/ping
/usr/bin/ping is owned by iputils 20161105.1f2bb12-2

Now just reinstall the iputils package using pacman command:
$ sudo pacman -S iputils

Suse/OpenSUSE Linux user type

$ zypper search -f /bin/ping
Sample outputs:

Loading repository data...
Reading installed packages...

S | Name    | Summary                            | Type   
--+---------+------------------------------------+--------
  | ctdb    | Clustered TDB                      | package
i | iputils | IPv4 and IPv6 Networking Utilities | package
  | pingus  | Free Lemmings-like puzzle game     | package

Now just reinstall the iputils package using zypper command:
$ sudo zypper -S iputils

If everything else failed, try restoring the file from backup

The reason why you keep backups is here. It is time to restore that file from NAS server or tape device:
scp vivek@backup1.cyberciti.biz:/backups/server1.cyberciti.biz/snap/lastest/bin/ping /bin/ping

What can be done to avoid such problem in future?

Testing in a sandbox is an excellent way to prevent such problem. Care must be taken to make sure that variable has value. The following is dangerous:
echo "foo" > $file
Maybe something like as follows would help (see “If Variable Is Not Defined, Set Default Variable“)
file="${1:-/tmp/file.txt}"
echo "foo" > $file

Another option is to stop if variable is not defined:
${Variable?Error \$Variable is not defined}

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
3 comments… add one
  • Maik May 23, 2017 @ 18:25

    The easiest way: if you place a “set -u” in your script then bash shows an error when it tries to use an undefined variable.

  • Nehal J Wani May 24, 2017 @ 3:50

    It would be nice to also include that if a binary gets deleted, but is still running, one can (in most cases) recover it from /proc/$pid/exe . Although it may seem to be a dangling link, the read/copy operations still work.

  • Pavel Chernikov May 24, 2017 @ 14:47

    Another way is to use Python instead of shell scripting, where Python will detect major issues like undefined variables.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.