When you understand the way pipes work, you can get seriously creative. This article covers what happens when you combine Unix pipes and SSH. It should be noted that since Unix pipes can be just about anything, there are no doubt going to be commands not on this list would also be useful.

Understanding the Unix Pipeline

Pipes on Unix (and by extension, Linux) are used to chain programs together and make them work together. For example, using cat, you can show the contents of a file, but if you used a pipe, you could chain the cat command to the more command to make the file easier to read through.

The basic idea here is this: program1 fileX | program2. It’s not just limited to one file and two programs, though. Piping can get about as advanced as you need it to be with as many modifiers as you can think of. Note: Some types of pipes can be done without using the |. Some may use > instead.

5 Useful SSH pipe commands

Now that the Unix pipeline makes a little sense, let’s see what we can do with the SSH protocol and pipes. Here’s a list of some really great pipes that most will find useful when combining with SSH.

1.  Compressed file transfer

Forget using scp to transfer files; you can do it with a simple SSH pipe command. No need to install anything.

This uses the tar program to compress your data locally and then is piped over SSH. From there, the remote machine receives the file and extracts it to the folder you specified. You’ll never actually see a .tar archive, but it makes use of one.

2. Running a local script on a remote machine (or remote on local)

Got a script written on your computer and want to test it out really quickly? No need to push the file to it or anything like that. Just pipe your local file through SSH and run it this way instead! With this command you remove the need to push files around to remote machines to execute shell scripts. It saves a lot of time in the long run.

3. Remote hard drive backup

Want to back up your computer to your remote machine without taking the hard drive out physically and hooking it up? It’s easy to do, and with an SSH pipe, no less. Here’s how it works: This makes use of the dd command. It uses your local drive (sda) as the source, and then it pipes the output over SSH to be written to a raw image file.

Note: The drive you may want to back up might have a different denotation. Use the lsblk command to figure out what drive you’re looking to back up. This command will tell you what /dev/ to use in the if= part for the command above.

4. Remote hard drive restoration

Want to restore that image you just backed up to your machine? It’s easy. This time the command works in reverse. Again, if the drive you are restoring to is named differently than what is listed in the example, use the lsblk command to find out what /dev/ it’s listed as.

Run this command, and the .img file you created will be restored over the network to the hard drive that you specify.

5. Send a file

Sending a single file over SSH is easy. Here’s how to do it with pipes. This command makes use of the cat command to send a file through a pipe. You can also retrieve that file with the following command:

Conclusion

Though it might not seem that impressive, pipes can simplify and transform the way you use commands on Linux. Though this list highlights some of the most useful, it’s only the tip of the iceberg. With how versatile the vertical bar is, the possibilities for piping things through SSH are endless. Know any good SSH piping commands? Tell us below! Image Credit: Wikimedia commons