(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?


  • Using only one step is complex – Ruban Savvy Apr 27 '15 at 10:13
  • @Marco yes looks like a matching dup – Skaperen Apr 27 '15 at 11:10
  • @Marco - thanks, I knew it must have been asked and I just wasn't searching with the right terms... – phil Apr 27 '15 at 12:16

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.)

| improve this answer | |
  • i'd go with the pipeline solution in the dup link except i would seriously consider having the remote host do the compression to reduce network bandwidth load. – Skaperen Apr 27 '15 at 11:14
  • Your answer was great @roaima – Ruban Savvy Apr 27 '15 at 11:41
  • 1
    @Skaperen this version also does compression on the remote host (the z flag) – roaima Apr 27 '15 at 11:55
  • 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. – arekolek Aug 19 '16 at 12:10
  • 1
    @arekolek that depends on your tar. Some versions default to a tape device. – roaima Aug 20 '16 at 14:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.