How can I make the compression and decompression of an archive via ssh with tar command, without creating a temporary file:

I've an exemple:


tar -cf - myFile.txt | ssh $IP_SERVER ./ScriptServer.sh 


tar -x -C ~/backups/ -f -

These code snippet is not mine, and I don’t understand how the -f - works in the tar command, the man page is useless.


With -f archive.tar, tar -c would write to the archive, tar -x would extract from it. -f - is a special syntax which means "write to stdout" (if creating) or "read from stdin" (if extracting).

This allows to use tar in pipes. The client command pipes tar to ssh. On the remote side SSH server runs ./ScriptServer.sh and pushes incoming stream to its stdin. tar -f - inside the script reads this stream.

You're right that -f - is not documented in man 1 tar (at least in manuals I read). Without -f the tool should use stdout or stdin anyway. Few other tools support - as a special "filename" to explicitly point to stdout or stdin; I suppose the support of -f - in tar is just a compliance to this semi-standard.


I don’t understand how the -f - works in the tar command, the man page is useless.

Which man page are you looking at? The one from the command line of your system via man tar or something else? Checking this online man page for tar—as well as this one—shows the following:

-f, --file=ARCHIVE
use archive file or device ARCHIVE

So the -f would read the input from a specific file… But what then is the - after the -f? It sure as hell is not a file. That is basically a special character telling the command—in this case—to get the the data from stdin (standard input)… And that stdin comes from the first script piping the output—via the | pipe via SSH to the receiving script on the destination.


Here is an example without creating script on receiver side. To transfer files via uncompressed flow from local to remote unix:


cd "$LocalDir"
tar -cf - "$LocalFile" | ssh $RemoteIP "cd '$RemoteDir'; tar -xf -"

POSIX Tar not compresses files, only concatenates. Also you may add light compression in such way

tar -cf - "$LocalFile" | gzip -1 | \
   ssh $RemoteIP "cd '$RemoteDir'; gzip -cd | tar -xf -"

I do not recommend you to add deep compression due to larger time of transferring.

If you want one-liner, here it is:

tar -cf - myFile.txt | gzip -1 | ssh "cd backups; gzip -cd | tar -xf -"

Tested to transfer file from RHEL6 to Solaris11 and backwards from Solaris to RHEL.


The first command "tar -cf -" will create the tar to standard output (stdout) instead of a regular file.

This will be sent as stdin through ssh, and used as such by server-script.

See also : https://www.cyberciti.biz/faq/howto-use-tar-command-through-network-over-ssh-session/ and https://www.linuxjournal.com/content/working-stdin-and-stdout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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