I know this is something I shouldn't do, but it happened(Ubuntu 12.04.1 LTS) as a root user I deleted /usr/lib folder. Any ideas how to restore it?
4 Answers
There are tools that might help you undelete the files, but usually it's a slow and mostly manual process. Search engines are your friends.
It may be easier to boot up a live CD or USB, mount your system's root partition, then copy /usr/lib to /mnt/{root.drive}usr/lib, but you will only get the default lib files and not what you may have added.
I think the best option is to reinstall the OS. Of course, you will want your data on a separate partition that is not formatted during install, good practice IMHO.
Supposing apt-get
still works you can try to use dpkg
to get a listing of the packages that have files there and then install them with apt-get
. You can use this Ruby script, but the same idea can be implemented in python or bash:
raw_pkgs = `dpkg --get-selections`.split("\n")
need_reinstall = []
path="/usr/lib"
raw_pkgs.each do |x|
pkg = x.split(" ")[0]
if `dpkg -L #{pkg}`.include? path
puts "-> #{pkg} has files in #{path}"
need_reinstall << pkg
end
end
puts "\nYou need to reinstall #{need_reinstall.size} packages:"
puts "\tsudo apt-get install --reinstall " + need_reinstall.join(" ")
It is a little of a brute-force solution and will take some time (in my system the listing was ~65% of the total packages installed...), but should work.
Then create an ubuntu USB boot. Then boot to "try ubuntu" mode. Then mounting your disk to access /usr/lib folder Access files in Home directory from live mode . Copy /usr/lib from other computer to your. Restart. Then your computer can work almost normally with almost all basic function. You can install missing libs later
-
I just did this. It is August of 2022. I created a USB boot/install thumb drive. I used that USB thumb drive to make a regular bootable install on a USB 2.5" hard drive. That bootable USB 2.5" hard drive was used to boot the FUBARed laptop. I mounted the laptop hard drive. I copied the /usr directory from the bootable USB 2.5" hard disk to the laptop drive. vi does not work, ls works, chrome loads, sublime loads, zoom does not, in fact flatpak doesn't seem to work for any applications I have. Needed to set sudo permissions to "#chmod 4755 /usr/bin/sudo". Aug 25, 2022 at 5:18
import subprocess
from functools import cache
def main():
run_cmd = subprocess.run(["dpkg", "--get-selections"], capture_output=True)
path = "/usr/lib/python3"
output = {x for x in run_cmd.stdout.decode().split("\n")}
app_box = []
for x in output:
pkg = x.split("\t")[0]
the_process = {x for x in subprocess.run(["dpkg", "-L", pkg], capture_output=True).stdout.decode().split("\n")}
if path in the_process:
print(f"-> {pkg} has file in {path}")
app_box.append(pkg)
else:
print(f"-> {pkg} has no file in {path}")
print(f"you need to reinstall {len(app_box)} package")
print(f"\tsudo apt install --reinstall {' '.join(app_box)}")
if __name__ == "__main__":
main()
If you are wondering about the python version of the answer @Salem provided............ Here is it. well, not that efficient but it works LOL.
/usr/lib/*
and not/lib/*
, you can probably recover.")