4

I'm new to linux and installed Ubuntu 12.04 today. I accidentally deleted everything in /usr/bin typing in the command rm -rf *.

I don't know what is mounting and would like to learn but at this point I think the only way left for me is to re install everything. Please let me know the ways to re install everything now. It would be of great help to me.

6
  • 2
    Think twice next time before typing in some commands, especially when running them as root and especially for commands like rm -rf *...
    – phoibos
    Sep 23, 2012 at 0:40
  • @phoibos, lesson learnt. i will delete carefully if need to from next time. i was just too excited today and anything in too much obviously leads to sorrow and philosophy haha.. Sep 23, 2012 at 0:45
  • How about upgrading the system?
    – nikhil
    Sep 23, 2012 at 5:56
  • @nikhil that is a good idea. but i already deleted windows and installed ubuntu in my system Sep 23, 2012 at 15:25
  • I meant running apt-get upgrade, that might work out not sure that would work though.
    – nikhil
    Oct 1, 2012 at 10:50

5 Answers 5

9

Since you just installed today, do the installation again. While you can recover from this, it'll be a lot more work than redoing the installation.

To recover from the deletion of /usr/bin, you would need to reinstall all the packages that have files in that directory. You can use this command to list the affected packages:

cd /var/lib/dpkg/info
grep -l '^/usr/bin/' *.list | sed 's/\.list$//'

You'll then need to find some way of downloading the packages without relying on any of the deleted programs. Since you've deleted dpkg, the low-level package installation utility, you'll need to obtain it from somewhere first. Grab it from some other machine running the same release Ubuntu, or download the dpkg package on another machine and extract the programs from it. You'll need not just /usr/bin/dpkg but also all the other programs in that package, including update-alternatives and all the programs named /usr/bin/dpkg-*. Again, copying might be tricky with so many programs deleted, so you may need to reboot to a rescue CD/USB to do it.

Once you have the dpkg suite, download at least the dpkg and apt package somehow, perhaps on another machine or in a still-running web browser.

Then install apt manually with the command

dpkg -i /path/to/apt_0.8.16~exp12ubuntu10.2.deb

If you get errors about missing commands, reinstall the corresponding packages first. Then reinstall all the affected packages:

apt-get --reinstall install $(grep -l '^/usr/bin/' *.list | sed 's/\.list$//')

Again, in your situation, just do a complete reinstallation.

6
  • i would definitely go for a re installation. But can i reinstall it on the same 80gb memory i used install the current one? Sep 23, 2012 at 0:43
  • @user1455116 Yes, tell the installer to use the existing partition and to erase its content. Sep 23, 2012 at 0:44
  • 1
    Hm, which dpkg says dpkg is in /usr/bin/dpkg, so that won't be available...
    – phoibos
    Sep 23, 2012 at 0:45
  • @Gilles how do i do that? i already tried restarting with the cd in the drive and splits my current windows memory into two, and does not show the 80gb part :( Sep 23, 2012 at 0:46
  • @phoibos Right, thanks, so recovery is likely to require booting to a rescue CD. Sep 23, 2012 at 0:53
4

/usr/bin/ contains most of the programs that are installed on your system.

Just installing Ubuntu again would be the easiest and fastest way to get a working system again.

6
  • /usr/bin contains some programs' executables. Not all programs are there, and their data files are not there either.
    – nanofarad
    Sep 23, 2012 at 0:38
  • @Florian Diesch Hi i would definitely do that. Now i have windows 7 in one partition and ubuntu in another (80gb). Can i reinstall in the same 80gb space? Sep 23, 2012 at 0:41
  • @user1455116 That's the way to go. Sep 23, 2012 at 0:50
  • @user1455116 Yes. just be careful to select the right partition when installing. Sep 23, 2012 at 0:52
  • @ngel Araya im not having any option saying wipe the current ubuntu and reinstall while i start from the CD, it is creating another partition and a third OS :( Sep 23, 2012 at 0:52
0

Nothing really hard if this is a vm:

  1. save the list of packages you had installed

    cd /var/lib/dpkg/info
    grep -l '^/usr/bin/' *.list | sed 's/\.list$//' > ~/packs.txt
    
  2. Install the same OS version (i386 or amd64) on a new VM

  3. On the fresh machine do

    cd /var/lib/dpkg/info
    grep -l '^/usr/bin/' *.list | sed 's/\.list$//' > ~/packs2.txt
    
  4. copy the content of packs.txt on the new system and you can do:

    install=$(diff -c ~/packs2.txt ~/packs.txt | grep "^- " | cut -d' ' -f2 | xargs echo )
    apt-get install -y --force-yes $install
    remove=$(diff -c ~/packs2.txt ~/packs.txt | grep "^+ " | cut -d' ' -f2 | xargs echo )
    apt-get purge -y --force-yes $remove
    
  5. Now the new system has the same content /usr/bin of your damaged machine

  6. mount the disk of your broken machine into the new one and finally:

    rsync -av /usr/bin /mnt/usr/bin
    

profit

0

You could maybe export this directory from another computer through NFS? Yes, this is very ugly and you should just reinstall.

Anyway: mount should still work, because

ls /usr/bin | grep mount

didn't give me anything besides grub-mount. So just copying relevant files from a mounted directory might work temporarily. Just remember, sudo will not work, use su.

0

Had the same foolish problem... here is what I have done to get everything working again:

1. Like in the answer of @Gilles, make a list of your packages:

cd /var/lib/dpkg/info
grep -l '^/usr/bin/' *.list | sed 's/\.list$//' > ~/my_packages1.txt

2. Insert a Live CD with the same Ubuntu version, boot from that and copy your /usr/bin directory. The partition with Ubuntu on my hard disk was mounted, so that is as easy as

cp -R /usr/bin /media/something/usr/bin

3. Now I couldn't use sudo or pkexec, because I got the error message must be owned by uid 0 and have the setuid bit set. So I had to change that, by booting from GRUB into a root shell. (See here how to do this and on this german site for some pics.) Now:

mount -o remount,rw / 
chown -R root:root /usr/bin
chmod a=rx,u+ws /usr/bin/sudo
chmod a=rx,u+ws /usr/bin/pkexec

Reboot into your normal Ubuntu system.

4. Reinstall your packages:

sudo xargs -a ~/my_packages1.txt apt-get install --reinstall
rm ~/my_packages.txt

5. There were still many packages missing. debsums helped my finding those packages for installing them manually. (Maybe with this step 1 and 4 are obsolete, but I haven't tried...)

sudo apt-get install debsums
sudo debsums_init

# This lets debsums find packages with missing files, filters the package names and writes the unique ones to the file.
sudo debsums -cs 2>&1 >/dev/null | sed -e 's/\(.*\)from \(.*\) package\(.*\)/\2/' | uniq -u > my_packages2.txt

sudo xargs -a ~/my_packages2.txt apt-get install --reinstall

You must log in to answer this question.

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