Score:22

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

in flag

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.

cocomac avatar
cn flag
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.
paolo gonzales avatar
in flag
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
cocomac avatar
cn flag
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.
paolo gonzales avatar
in flag
oh thank youu very much I had this version ```Ubuntu 21.10 x86_64``` I'll wait for your directions <3
starkus avatar
de flag
`dpkg -S /usr/bin/test` shows that `/usr/bin/test` is provided by the package `coreutils`. You could try to reinstall and reconfigure it.
paolo gonzales avatar
in flag
@starkus how can I install and reconfigure it?
cocomac avatar
cn flag
@paologonzales You're free to try whatever you want. But I'd advise against trying to reinstall/reconfigure it. If you want, the command to reinstall it is `sudo apt install --reinstall coreutils`. But I think you've already tried that, and given the `Could not connect` errors, I don't think that trying to reinstall it will work well. I suggest trying what I said instead.
paolo gonzales avatar
in flag
that also what I think that is why I'm asking him what does he mean about installing and reconfiguring it. Im currently installing the ISO internet is slow
starkus avatar
de flag
something with 'sudo dpkg -i /var/cache/apt/archives/coreutils*.deb'
us flag
Will `cp /usr/bin/[ /usr/bin/test` work for the interm?
matteol avatar
cz flag
@doneal24 no /usr/bin/[ wants a closing ]
be flag
I'm somewhat surprised anything is trying to use `/usr/bin/test`, since `test`is a built-in in most common shells.
ne flag
most solutions are going to involve downloading *something*; you are likely to need to figure out why you got `Could not connect to ph.archive.ubuntu.com`. do you have an internet connection?
tz flag
@chepner: "test" is not a built-in in sh. Most install scripts, for portability reasons, strive to use sh (as sh is the one shell always supposed to be present), and therefore also have to use the "posix" test program (also supposed to be present).
be flag
`sh` isn't really a shell (or at least, the actual Bourne shell that originally bore the name is rarely used anymore); it's the umbrella name for whatever POSIX-compatible shell a POSIX-compliant system is required to provide.
ag flag
And this is why you should be careful using su and sudo :-( I agree that the easiest is to copy the binary from another, healthy system (including a live cd). You may want to experiment reinstalling Ubuntu on top if you do not care about the data on the system.
ag flag
@ysth I think the error message may be misleading if the programmer did not expect that `test` might be missing.
Peter - Reinstate Monica avatar
hn flag
You could always write a rudimentary replacement. Chances are that the install scripts only use a small subset of the /bin/test functionality, most likely checking for existing files.
ne flag
@ThorbjørnRavnAndersen the "connection timed out" made me think it unlikely to be related to test missing
Score:50
hr flag

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.

Quasímodo avatar
jp flag
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](https://busybox.net/downloads/BusyBox.html)), it might break some scripts. As such, for a temporary solution using a `#!/bin/bash` script may be better.
cn flag
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.
Eric Duminil avatar
us flag
`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?
Eric Duminil avatar
us flag
Oh cool. "Busyboxes usually decide their "wanted functionality" by checking where they're called from. " https://android.stackexchange.com/a/64031/292651
in flag
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.
Score:36
za flag

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
    
Peter Cordes avatar
fr flag
`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.
in flag
I would use `apt-get download coreutils` instead of depending on wget a url that can disappear.
cn flag
@ValentinSolina: You can roll back that edit (click on "edited N hours/days ago". Braiam can always post another answer
ag flag
Rolled back to `wget`-version, as `apt-get` is broken in this particular scenario.
za flag
In case package download link becomes invalid or you have a different Ubuntu version, search for coreutils on the official site: https://packages.ubuntu.com - that's how I found it
Score:16
tr flag

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.

Ruslan avatar
bv flag
Why does one need to escape the brackets?
Gilles 'SO- stop being evil' avatar
tr flag
@Ruslan In this particular script, the brackets don't need to be escaped.
Score:13
cn flag

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.
za flag
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: https://unix.stackexchange.com/a/179146
za flag
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 https://unix.stackexchange.com/a/316407
cocomac avatar
cn flag
@ValentinSolina I didn't know that, can you post an answer?
za flag
Here @cocomac https://askubuntu.com/a/1389039/1564755
Someone avatar
my flag
If you are burning the ISO file into a USB then the correct term is Live "USB" not "CD".
cocomac avatar
cn flag
@Someone Oops, I didn't know that. Feel free to [suggest an edit](https://askubuntu.com/posts/1388900/edit)
Someone avatar
my flag
I have edited *(I am having more than 2k rep so my edits are applied immediately, feel free to rollback)* your answer and replaced Live CD with live session as that seems more appropriate. I have also changed the minimum capacity to 8GB as live session on a 4GB USB will break itself.
cocomac avatar
cn flag
@Someone Just read through it, and your edit makes sense. Thanks!
ChanganAuto avatar
us flag
#4 is still wrong Ubuntu 21.10 ISO is 2.9GB) and recommending a 3rd party software when Startup Disk Creator is installed by default makes no sense.
Score:5
de flag

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.

starkus avatar
de flag
I did mention at the beginning that this does not only restore `test`. Do you mean something else?
ag flag
yes you did. I don’t know why I misread it - perhaps scrolled to the beginning of another question. Comment deleted.
Score:0
cn flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.