Linux: How to compress data in a remote server and save them locally in one command!

One of the most common tasks of a system administrator is to compress some files in a remote server and then move them to another server…

Linux: How to compress data in a remote server and save them locally in one command!
Photo by Anne Nygård on Unsplash

One of the most common tasks of a system administrator is to compress some files in a remote server and then move them to another server, either for backup or data processing. Its relatively a simple operation but it has two pitfalls

  • Remote server might ran out of disk space during compression
  • Needs extra commands to download and delete the compressed file

Usually the procedure is like thisssh server1 'tar -czvf compressed_file.tar.gz /dir/to/compress'
scp server1 'compressed_file.tar.gz' .
ssh server1 'rm compressed_file.tar.gz'

We can simplify the whole procedure and plus eliminate the danger to run out of disk space in the remote server by using the following command.ssh -q server1 "tar -czpf - /dir/to/compress 2>/dev/null" > compressed_file.tgz

How it works

  • -q : ssh will not show banners, this is needed in order not to write those messages to the compressed data which will corrupt the archive.
  • -czpf : various compression options.
  • - : this is needed in order not to write the compressed data to a file but to the terminal.
  • 2>/dev/null : this is needed in order not to write warnings / errors to the terminal along with the compressed data which will corrupt the archive.
  • > compressed_file.tgz : the file name in the local computer to save the remote server compressed data from its terminal.

I hope you found this article useful :)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…