Ubuntu – Accidentally deleted the “/usr/share” folder

18.04data-recovery

The system does not start. Did it by using rm - R accidentally. How can I recover the system?

Best Answer

You need to reinstall all applications which have files in /usr/share.

  1. Boot system from Recovery Mode with root prompt.

  2. Remount rootfs to read-write mode with mount -o rw,remount /.

  3. Raise up ethernet interface and get IP address from DHCP server: dhclient enp0s3 (check correct interface name in ip a or ifconfig -a).

  4. Specify DNS server by

    rm /etc/resolv.conf
    echo "nameserver 192.168.1.1" > /etc/resolv.conf
    

    (change 192.168.1.1 to yours gateway/router)

  5. You need to fix the dpkg package as it misses the /usr/share/dpkg/cputable file (otherwise you will face the "E: error reading the cpu table" on any apt/apt-get command)

    wget http://mirrors.kernel.org/ubuntu/pool/main/d/dpkg/dpkg_1.19.0.5ubuntu2.1_amd64.deb
    dpkg -i dpkg_1.19.0.5ubuntu2.1_amd64.deb
    

    or installing it from cache:

    dpkg -i /var/cache/apt/archives/dpkg_*.deb
    
  6. update package cache:

    apt-get update
    
  7. reinstall essential componenets

    apt-get install --reinstall debconf linux-base
    mkdir /usr/share/python
    apt-get install --reinstall python2.7-minimal python3-minimal
    
  8. and then use one-liner below:

    apt-get install --reinstall $(dpkg -S /usr/share/ | sed 's/,//g' | sed 's/: \/usr\/share//g')
    

    Above:

    • dpkg -S /usr/share/ shows the list of comma-separated packages
    • sed 's/,//g' - removes commas
    • sed 's/: \/usr\/share//g' - removes : /usr/share in the end

    This part may fail with messages about some packages. For example on my VM I have had a problem with bsdmainutils, so I reinstalled them with:

    dpkg -i /var/cache/apt/archives/bsdmainutils_*.deb
    

    and then reran one-liner above.

  9. fix broken packages

    dpkg --configure -a
    apt-get install -f
    
  10. finally fix /etc/resolv.conf link by

    rm /etc/resolv.conf
    ln -s /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
    
  11. reboot.

Note: the resulting fixed system do not show any problems while running sudo apt-get check or sudo debsums --all --changed.

Related Question