22

I don't know what I've done but basic commands like "ls" and "sudo" no longer work and now throw me this error message.

File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg' 

Please help, I'm crying, I have a deadline tomorrow.

Should I just do a fresh install of Ubuntu but keep the files?

Retsek
  • 343
  • 2
    Welcome! After what command the error shows? – schrodingerscatcuriosity Feb 20 '20 at 23:53
  • 4
    Let us start with the basics. does cd /bin work or give an error? If it works does cd /bin ; echo ls* give one or more words of output including ls, or does it only give ls* back? – icarus Feb 20 '20 at 23:53
  • Lots of different commands all bring the same error (at least the same as far as I can tell with a quick skim-read). Commands like "sudo", "ls", "dpkg" but commands like "cd" and "pwd" work fine – Retsek Feb 20 '20 at 23:55
  • @icarus Hi Icarus, both of those commands work and the second one prints out more than just ls it prints out lsblk and lsmod too – Retsek Feb 20 '20 at 23:56
  • 2
    Great. Does /bin/ls -l /bin/ls give you a long listing of the 'ls program or an error? If it works then run PATH=/bin:/usr/bin:$PATH and see if ls and sudo are now working. – icarus Feb 21 '20 at 00:04
  • It doesn't exactly give me a long listing but it doesn't give me an error, it says -rwxr-xr-x 1 root root 133792 Jan 18 2018 /bin/ls, ls and sudo now work!! – Retsek Feb 21 '20 at 00:06
  • @icarus you are a god-send, I removed some very silly edits to my .bashrc and everything seems to be okay now? (fingers crossed) thank-you so much – Retsek Feb 21 '20 at 00:15
  • 2
    @Retsek Good luck with the thing you had a deadline for! – MechMK1 Feb 21 '20 at 09:13

1 Answers1

38

We did some debugging with the OP.

  1. cd /bin This worked,so we knew /bin had not been deleted
  2. cd /bin; echo ls* This produced "ls lsblk lsmod" rather than "ls*" so we knew there were at least 3 files in /bin
  3. /bin/ls -l /bin/ls This produced "-rwxr-xr-x 1 root root 133792 Jan 18 2018 /bin/ls" so it wasn't a matter of execute permissions being removed.
  4. PATH=/bin:/usr/bin:$PATH This creates a sane couple of places to look for commands, and now ls and sudo are working again.

Conclusion: The PATH variable had become corrupted, and hence bash couldn't find anything. Using step by step debugging starting with just builtin commands (cd and echo) this was diagnosed quickly. Hopefully the OP has dried their tears and is all smiles again.

The OP has just commented that they have "removed some very silly edits to my .bashrc and everything seems to be okay".

icarus
  • 17,920
  • 4
    You are a Unix detective, genius and saint. Thank-you for blessing me with your troubleshooting <3 – Retsek Feb 21 '20 at 00:22
  • 12
    You are not the first person to have mangled the PATH. It was my first thought, too. The difference here is that in an attempt at user-friendliness Ubuntu adds an extension to the Bourne Again shell that runs another program when a command is not found, instead of printing a short "command not found" message. When that program fails, as here, its error messages are vastly less user-friendly than the vanilla "command not found" message. What icarus has not covered is repairing the command-not-found system, to stop the utility program from failing. – JdeBP Feb 21 '20 at 08:44
  • 1
    … as it still is at https://unix.stackexchange.com/q/568967/5132 . (-: – JdeBP Feb 21 '20 at 19:45
  • I was once really surprised to find that some commands could run faster than a command that wasn't found. That drove me nuts for a few days. Thanks Ubuntu! – chicks Feb 25 '20 at 18:09