How to recover deleted “dpkg” directory in Debian or Ubuntu

Question:

Unfortunately I’ve deleted dpkg directory while removing the lock. By mistake I typed

root@akupedia:~$ rm -r /var/lib/dpkg

Now when I am trying to install/uninstall packages it shows me following error.

E: Could not open lock file /var/lib/dpkg/lock - open (2: No such file or directory)

What should I do now?

Solution:

root + rm + -r = disaster

So let us begin step by step?

ls -l /var/lib/dpkg/
total 9964
drwxr-xr-x 2 root root    4096 nov 28 11:18 alternatives
-rw-r--r-- 1 root root      11 sep 18 14:08 arch
-rw-r--r-- 1 root root 2573807 nov 28 11:18 available
-rw-r--r-- 1 root root 2561322 nov 28 10:25 available-old
-rw-r--r-- 1 root root       8 abr 24  2013 cmethopt
-rw-r--r-- 1 root root     538 sep 25 17:24 diversions
-rw-r--r-- 1 root root     457 sep 25 17:24 diversions-old
drwxr-xr-x 2 root root  483328 nov 28 11:17 info
-rw-r----- 1 root root       0 nov 28 11:18 lock
drwxr-xr-x 2 root root    4096 mar 22  2013 parts
-rw-r--r-- 1 root root     135 abr 24  2013 statoverride
-rw-r--r-- 1 root root 2269113 nov 28 11:18 status
-rw-r--r-- 1 root root 2268870 nov 28 11:18 status-old
drwxr-xr-x 2 root root    4096 nov 28 11:18 triggers
drwxr-xr-x 2 root root    4096 nov 28 11:18 updates

You removed 5 directories, the status file, etc. So, lets try to fix the stuff. First, create the directory:

sudo mkdir -p /var/lib/dpkg/{alternatives,info,parts,triggers,updates}

Recover some backups:

sudo cp /var/backups/dpkg.status.0 /var/lib/dpkg/status

Now, let’s see if your dpkg is working (start praying):

apt-get download dpkg
sudo dpkg -i dpkg*.deb

If everything is “ok” then repair your base files too:

apt-get download base-files
sudo dpkg -i base-files*.deb

Now try to update your package list, etc.:

dpkg --audit
sudo apt-get update
sudo apt-get check

Now, let’s take a deep breath. Phew!!! Do ls -l /var/lib/dpkg and compare with the above list. If some -old file is not present don’t worry it will be there after few days.

By Akash Angle

I am a Full time Linux user who has quit using Windows for unknown reasons, making my life truly open source.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x