4

Suppose we have a web url to a .tar.gz file.

We could download it, unzip/tar it, and install it.

Is there an elegant way of doing all of the above from terminal, preferably with a one-liner?

Example tar.gz file: https://cran.r-project.org/src/base/R-3/R-3.5.0.tar.gz

Note: I don't mind using a program to do this, but much prefer everything done from the terminal, mostly for efficiency, fewer steps, and being able to give reproducible instructions to others

  • 1
    Is the filename expected to change or will it be the same no matter what? – Allan May 17 at 14:56
  • @Allan would want to be able to use the same technique for any url to a .tar.gz file – stevec May 17 at 14:57
  • @Allan So yep, i hope for a method that works for any .tar.gz file it receives – stevec May 17 at 14:59
  • 1
    Your second comment answered my question. – Allan May 17 at 14:59
3

This will work better as a script that accepts a parameter.

tar_from_url() {
    URL="$1"
    curl -O "$URL"
    tar -xvf "${URL##*/}"
    rm "${URL##*/}"
}

So basically use curl to download, then use tar to extract, finally get rid of downloaded file.

The installation part needs still to be done manually though. The name of the directory it extracts to is unknown and different depending on the actual tar file, as is the method required for installation itself.

| improve this answer | |
  • @nohillside This is great and very helpful. But are you sure it's not possible to include the installation in the script? Perhaps some sensible default location? – stevec May 17 at 15:46
  • 1
    @stevec isn't that use case specific ? If it's a pip package say, what purpose would it serve sitting in /usr/local/opt ? – anki May 17 at 16:50
  • @ankii I don't know enough to say. I always thought installations had some default locations, but if not, I agree it would not make sense putting it somewhere it cannot be used/accessed – stevec May 17 at 17:00
  • 1
    @stevec brew install R installs 4.0.0, see docs.brew.sh/Formula-Cookbook for how to write your own formula. – nohillside May 17 at 17:13
  • 1
    @stevec For a limited set of well-known tar files I probably would just write a bespoke shell script and deploy that. – nohillside May 17 at 17:23
13

If you don't want to keep the tar file, do

curl https://cran.r-project.org/src/base/R-3/R-3.5.0.tar.gz | tar zxf -

This uncompresses and extracts the tar file while downloading it.

The commands to install vary with the package, but you can do this after the download has finished:

cd R-3.5.0 ; ./configure && make
| improve this answer | |
  • Since the question was about efficiency, not waiting for the download to complete before starting to unpack, and not taking up disk space for the packed archive, definitely is the way to go. – DevSolar May 18 at 13:30
  • 1
    One downside of this approach is that if the download failed for any reason, it would have to be restarted from scratch. (Whereas if it was being downloaded to a file, it could usually be resumed from where it left off.) – gidds May 18 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .