Tar: extract single file to a directory without creating subfolders

tar

My goal is to get the helm binary from helm-v2.5.0-linux-amd64.tar.gz into my /usr/local/bin in as few steps as possible. The challenge is that the .tar.gz file contains the binary in a linux-amd64 subdirectory.

So when I do this:

$ wget -P /tmp https://storage.googleapis.com/kubernetes-helm/helm-v2.5.0-linux-amd64.tar.gz
$ tar -xzvf /tmp/helm-v2.5.0-linux-amd64.tar.gz -C /usr/local/bin linux-amd64/helm

I end up with /usr/local/bin/linux-amd64/helm instead of /usr/local/bin/linux-amd64/helm.

Is there a tar parameter I am missing or do I need to include some mv & cleanup steps?

Best Answer

You need to use --strip-components=NUMBER:

--strip-components=NUMBER
          Strip NUMBER leading components from file names on extraction.

which means that your command should look like this:

tar -xzvf /tmp/helm-v2.5.0-linux-amd64.tar.gz -C /usr/local/bin --strip-components=1 linux-amd64/helm
Related Question