How to archive files with Tar in the Linux terminal

A core part of the philosophy of Unix-like operating systems is for tools to be specialised – each tool should do one job and do it well. That means we need a way to chain programs together to achieve more complex tasks, and this is done with pipes. Every terminal program has streams for standard input, standard output and standard error (usually referred to as stdin, stdout and stderr).

Standard input is how the program accepts information and it defaults to the keyboard when running a command from a terminal. Standard output is the output from the command that defaults to printing to the terminal window. Standard error goes to the same place, but all of these can be changed. Consider the example of:

tar c foo | sdc >foo.tar.sdc

The | character is the pipe and it connects the standard output of one program to the standard input of another. So Tar archives the contents of foo and sends it to stdout, which is then piped to the input of sdc.

Most compressors default to working on a file:

gzip somefile

This compresses the file to somefile.gz. When no file is given, they compress standard input and send the result to standard output. The > character performs redirection and diverts standard output from the terminal to a file, so you have a compressed archive in foo.tar.sdc.

Standard error is not redirected by >. If something goes wrong, error messages are printed to the terminal – which is logical, because if they were sent to the file, you wouldn't know something was wrong.