Tuesday, January 20, 2009

Reinstall deleted or damaged files in Debian/Ubuntu

Suppose you delete or damage a system file named idoit.iam. To recover the file, do the following.

1. Locate the package which installed idoit.iam:

dpkg-query -S idiot.iam

Say the package name is idiotic. (It is possible that there is more than one package, especially if you deleted a directory. Replace the package name with all the package names in what follows.)

2. Try to reinstall the package:

apt-get --reinstall install idoitic

3. Check to see whether your file has been replaced. If so, then you are happy and owe me a beer . If not, then it must be a configuration file (the previous command will not restore configuration files). But all is not lost. Proceed.

4. Download the package without installing:

apt-get -d --reinstall install idiotic

(Technically you don't have to do this because the command in step 2 should have downloaded the packages already.)

5. Change to the directory containing the packages:

cd /var/cache/apt/archives/

6. Now install the packages using dpkg, but forcing it to reinstall the missing configuration files:

dpkg --install --force-confmiss idoitic

Now you should be happy , and still owe me a beer .

PS: Technically, I prefer a good glass of red wine over any beer, but there is no Google icon for wine.