22

I accidentally deleted /usr/bin/test and now I can't update, upgrade and install packages.

I already tried the solution here: apt-get raises /usr/bin/test: Permission denied, but this is the output I got:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  java-common libutempter0
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 29 not upgraded.
Need to get 1,353 kB of archives.
After this operation, 0 B of additional disk space will be used.
Ign:1 http://ph.archive.ubuntu.com/ubuntu impish/main amd64 coreutils amd64 8.32-4ubuntu2
Ign ...
Ign ...
Err:1 http://ph.archive.ubuntu.com/ubuntu impish/main amd64 coreutils amd64 8.32-4ubuntu2
  Could not connect to ph.archive.ubuntu.com:80 (202.79.184.254), connection timed out
E: Failed to fetch http://ph.archive.ubuntu.com/ubuntu/pool/main/c/coreutils/coreutils_8.32-4ubuntu2_amd64.deb  Could not connect to ph.archive.ubuntu.com:80 (202.79.184.254), connection timed out
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

I also tried sudo apt update --fix-missing and got this output:

Err:8 http://ph.archive.ubuntu.com/ubuntu impish InRelease
  Could not connect to ph.archive.ubuntu.com:80 (202.79.184.254), connection timed out
Err:9 http://ph.archive.ubuntu.com/ubuntu impish-updates InRelease
  Unable to connect to ph.archive.ubuntu.com:http:
Err:10 http://ph.archive.ubuntu.com/ubuntu impish-backports InRelease
  Unable to connect to ph.archive.ubuntu.com:http:
Fetched 125 kB in 37s (3,339 B/s)
sh: 1: /usr/bin/test: not found
sh: 1: /usr/bin/test: not found
sh: 1: /usr/bin/test: not found
Reading package lists... Done

Please help me.

19
  • 1
    Any chance you can make a live CD? If so, you could create one with the exact same version and flavor of Ubuntu you have, boot to the live CD, mount your main filesystem somewhere, say, /mnt/mainfs, and then copy (not move, copy) the /usr/bin/test file from the live CD to the correct place in the main filesystem (in my example it would be this: sudo cp /usr/bin/test /mnt/mainfs/usr/bin/test). Simply taking the /usr/bin/test file from the live CD and putting it in the correct place on the main filesystem probobly would fix your issue.
    – cocomac
    Jan 24 at 5:35
  • hello thank you for responding but I am new in using Linux and I did not know what live CD is can you send me a video reference to follow or anything about that. thank you Jan 24 at 5:39
  • I'll send directions in a moment. What exact version of Ubuntu do you have? Normal Ubuntu? Kubuntu? 20.04? 21.10? You can probobly find this in the About section in Settings somewhere.
    – cocomac
    Jan 24 at 5:40
  • oh thank youu very much I had this version Ubuntu 21.10 x86_64 I'll wait for your directions <3 Jan 24 at 5:42
  • 4
    I'm somewhat surprised anything is trying to use /usr/bin/test, since testis a built-in in most common shells.
    – chepner
    Jan 24 at 16:36

6 Answers 6

51

You could use the test commandlet provided by busybox to temporarily replace a missing /usr/bin/test binary file.

First, check that you have busybox, and that its test works:

$ /usr/bin/busybox test -x /usr/bin/busybox && echo Works
Works

Then create a symbolic link:

$ sudo ln -s busybox /usr/bin/test
$ 
$ file /usr/bin/test
/usr/bin/test: symbolic link to busybox

Then re-install the coreutils package, which will overwrite the symlink with the proper binary implementation.

If you don't have busybox, you could even create /usr/bin/test as a shell script and leverage the shell's test builtin:

#!/bin/sh

test "$@"

(don't forget to make it executable, chmod +x /usr/bin/test) again re-installing coreutils right after.

5
  • 8
    Something to bear in mind is that Coreutils Test has some extensions which software might rely upon. If those happen to be absent from Busybox Test (which does not seem to be properly documented), it might break some scripts. As such, for a temporary solution using a #!/bin/bash script may be better.
    – Quasímodo
    Jan 24 at 14:12
  • 1
    The Debian "maintainer scripts" (i.e. the scripts run during package install) are supposed to run with a minimal POSIX compliant shell as /bin/sh. If they need extra features, the package needs to declare an explicit dependency, and this is uncommon. Jan 25 at 9:20
  • sudo ln -s busybox /usr/bin/test. Wait. This creates a symlink pointing to busybox, right? But busybox can do so much more than just busybox test. So how is the system supposed to know that busybox test is supposed to be called? Jan 27 at 8:48
  • Oh cool. "Busyboxes usually decide their "wanted functionality" by checking where they're called from. " android.stackexchange.com/a/64031/292651 Jan 27 at 12:12
  • 4
    Not "where they're called from", but "what name they're called under", as put in argv[0] by the caller. To demonstrate the difference, in bash, you can use exec -a test busybox 1 = 1 to call the busybox binary with the name test in argv[0], despite not having any link to busybox under that name actually exist on the filesystem. Jan 27 at 13:35
37

The simplest solution I can think of, without using the package manager (since it does not work anymore):

  1. Download the coreutils package:

    wget http://de.archive.ubuntu.com/ubuntu/pool/main/c/coreutils/coreutils_8.32-4ubuntu2_amd64.deb
    

    note: use browser if you don't have wget installed. Also note that the exact names are as of January 2022 - if you need to do this at a later date, file names will most likely have changed to reflect updated packages.

  2. Unpack downloaded package

    dpkg-deb -R coreutils_8.32-4ubuntu2_amd64.deb coreutils_unpacked
    
  3. Copy the missing test binary

    sudo cp coreutils_unpacked/usr/bin/test /usr/bin/test
    
  4. Add executable permission

    sudo chmod +x /usr/bin/test
    

Or if you'd like to download the full .iso file

  1. Download the iso file from the official site

  2. Create a temp directory to mount iso file into

    mkdir ubuntu_iso_tmp
    
  3. Mount iso file into newly created dir

    sudo mount -o loop ~/Downloads/Ubuntu_whatever.iso ubuntu_iso_tmp
    
  4. Copy the missing test binary

    sudo cp ubuntu_iso_tmp/usr/bin/test /usr/bin/test
    
5
  • 1
    cp -a would be a good choice to preserve permissions, ownership, and mod-time from the package file. (Then you wouldn't need chmod). I almost always use cp -a for any copying I ever do; I forget which metadata doesn't get copied without -a, but clearly in this case you want to copy as much as possible, like you were extracting directly to there. Or just use mv since you don't need to unpacked version anymore. Jan 25 at 5:11
  • 2
    I would use apt-get download coreutils instead of depending on wget a url that can disappear.
    – Braiam
    Jan 25 at 11:23
  • 2
    @ValentinSolina: You can roll back that edit (click on "edited N hours/days ago". Braiam can always post another answer
    – MSalters
    Jan 25 at 16:13
  • 1
    Rolled back to wget-version, as apt-get is broken in this particular scenario. Jan 26 at 6:38
  • In case package download link becomes invalid or you have a different Ubuntu version, search for coreutils on the official site: packages.ubuntu.com - that's how I found it Jan 26 at 8:17
16

For the specific case of /usr/bin/test, there's another program /usr/bin/[ which is identical except that it requires an additional last argument which is ]. GNU coreutils, which is the implementation of these programs under Ubuntu and other non-embedded Linux systems, ships those two programs as separate executables, so even if one is corrupted or missing, you can use the other. Create /usr/bin/test with the following content:

#!/bin/sh
/usr/bin/\[ "$@" \]

Make it executable (chmod a+rx /usr/bin/test) and you have a completely valid replacement for /usr/bin/test.

Then run apt reinstall coreutils to get the normal /usr/bin/test back.

2
  • Why does one need to escape the brackets?
    – Ruslan
    Jan 26 at 8:11
  • @Ruslan In this particular script, the brackets don't need to be escaped. Jan 26 at 10:28
13

I suggest getting the deleted file from a live USB and putting it back. Here's how.

  1. Download the Ubuntu ISO for your version of Ubuntu. Given that you have Impish (your apt output indicates that), the Ubuntu 21.10 ISO should work for you. Download the ISO labeled Desktop Image from here.
  2. Download balenaEtcher. Get the x64 Linux version. Extract the zip. In a terminal, go into the extracted zip folder. Once you're there, if you do ls, you should see a file that ends in .AppImage. Do chmod +x balenaEtcher-1.7.3-x64.AppImage.
  3. Run balenaEtcher with ./balenaEtcher-1.7.3-x64.AppImage.
  4. Find a flash drive that is bigger than 8GB in capacity, and put it in your computer. All data on this flash drive will be destroyed.
  5. Select the downloaded ISO, and select the flash drive. Hit Flash.
  6. Once it's done, reboot your computer, enter the boot menu, and select the flash drive. Press Try Ubuntu.
  7. In the live USB session, mount the main system. It might automatically do it, and there might be a button in the GUI to do that. Figure out where it is mounted. I'll use /mnt/mainfs as an example, but you should use the real location.
  8. Open a terminal, and run sudo cp /usr/bin/test /mnt/mainfs/usr/bin/test. Replace /mnt/mainfs with wherever you mounted your main system.
  9. You're done. Shut down the live session, unplug the flash drive, and reboot. Normal Ubuntu will be back, and apt should work again.
9
  • 1
    This is a great answer, a bit more advanced users could just replace the use of balenaEtcher with a single dd cmd which is installed by default: $ dd if=path/to/ubuntu.iso of=/dev/sdX bs=1m Detailed answer: unix.stackexchange.com/a/179146 Jan 24 at 14:19
  • 12
    Btw. if you still have a working Ubuntu there's even no need to use a Flash drive, you could just mount the .iso file. $ mount -o loop file.iso /mnt/dir unix.stackexchange.com/a/316407 Jan 24 at 14:22
  • @ValentinSolina I didn't know that, can you post an answer?
    – cocomac
    Jan 24 at 14:24
  • Here @cocomac askubuntu.com/a/1389039/1564755 Jan 24 at 17:15
  • 1
    @Someone Just read through it, and your edit makes sense. Thanks!
    – cocomac
    Feb 3 at 14:54
5

You could reinstall the main package and let the triggers do the job. In this case coreutils and any provided tool of it will be reinstalled by the scripts of coreutils. Not only test.

  1. Download the version installed on your system
    wget http://ph.archive.ubuntu.com/ubuntu/pool/main/c/coreutils/coreutils_8.32-4ubuntu2_amd64.deb
    
  2. Unpack it to its destination
    sudo dpkg --unpack coreutils_8.32-4ubuntu2_amd64.deb
    
  3. Configure or reconfigure it using its triggers:
    sudo dpkg --configure coreutils
    

Done.

2
  • I did mention at the beginning that this does not only restore test. Do you mean something else?
    – starkus
    Jan 27 at 5:41
  • yes you did. I don’t know why I misread it - perhaps scrolled to the beginning of another question. Comment deleted. Jan 28 at 7:01
0

Simply install the OS flavor locally on virtualbox and copy out and upload the binary to the system that’s missing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.