Tar and download in one step over ssh / scp

26,431

If you follow your description the command becomes "obvious" (at least, when you see it...). You really weren't far off with your attempt, though:

ssh user@remote tar czf - /my/directory/ > /my/local/destination/archive.tgz

(The "duplicate" link cited by Marco provides a cleaner solution. To create this command here I've simply followed the style of your attempt.)

Share:
26,431

Related videos on Youtube

phil
Author by

phil

Updated on September 18, 2022

Comments

  • phil
    phil over 1 year

    (This seems like an obvious / common question, but I couldn't find it when searching - maybe I wasn't using the correct terminology...)

    I want to login over ssh to a remote machine, tar a directory, and download it to my local machine. I don't need to leave a copy on the remote server. Currently I ssh in, create the tar archive, logout, scp the file to my machine, the ssh back in to remove the archive. Not exactly efficient...

    I know this doesn't work, but I'm thinking something like:

    scp user@remote:"tar czf - /my/directory/" ./my/local/destination/archive.tgz
    

    Or is there a better way?

    Thanks.

  • JackDaniels
    JackDaniels about 9 years
    Your answer was great @roaima
  • arekolek
    arekolek over 7 years
    You don't need f -, standard output is used by default. I like this solution better than the other answer, because this way you can use tab completion.
  • roaima
    roaima over 7 years
    @arekolek that depends on your tar. Some versions default to a tape device.