Search code examples
linuxcurltarubuntu-18.04xz

Ubuntu 18.04: Cannot extract downloaded tar.xz file with a single command


I am trying to download and extract tar.xz file with a single line command. However, it doesn't consistently work for all of the links. I can manually download and extract it.

I am able to download glibc and extract without any issue.

curl https://ftp.gnu.org/gnu/glibc/glibc-2.26.tar.xz | tar -xJ -C ${PWD} --strip-components 1

When it comes to download the following file, it surprisingly fails. I couldn't find out what makes the difference. This is the single link I have seen so far which fails.

Any idea why the following command fails?

curl https://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/arm-linux-gnueabihf/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz | tar -xJ -C ${PWD} --strip-components 1

It fails with the following message.

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
xz: (stdin): File format not recognized
tar: Child returned status 1
tar: Error is not recoverable: exiting now           

Any help would be appreciated.


Solution

  • Use curl -L to follow redirects:

    curl -L https://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/arm-linux-gnueabihf/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz | tar -xJ --strip-components 1
    

    To debug such issues, you can run the curl command by itself and observe that in this case, it doesn't return any data:

    $ curl https://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/arm-linux-gnueabihf/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz
    $
    

    You can then further run curl -v to see what's going on. It shows a HTTP/1.1 302 Found with an empty body and a redirection to a different file, which is how you know to add -L to follow the redirection.