In our first tutorial in this series we looked at the command options styles in tar. Now we move onto the first of the three basic options, creating tar archives.
Creating Tar Archives
When creating an archive we can use the options –create and –file as we have already seen; there are, of course, other options that we can choose to add if they help the situation. It also may help investigating default options that effect the creation of a tar archive.
Defaults to some options are provided by tar is they are not expressed on the command line at run time. The default values are set when the program is compiled. Unless you have compiled tar you will use the defaults they set. To display these we can use the following command:
$ tar --show-defaults --format=gnu -f- -b20 --quoting-style=escape --rmt-command=/usr/lib/tar/rmt --rsh-command=/usr/bin/rsh
The output from the command has been adjusted to display on the page more easily
We can see the -f or –file= option is set the STDOUT (–). This is a strange default setting as it would be effectively writing to the terminal which will fail; so we do need to supply the option –file or -f. The following command can be used to test this default option for the archive file:
$ tar --create /etc/services tar: Refusing to write archive contents to terminal (missing -f option?) tar: Error is not recoverable: exiting now
For the moment, we will leave the default options but we will return to them later. Lets look at creating archives.
–create | -c As we have already seen when creating an archive we use one of these options. This specifies that the file we use as an archive will be created if it does not exist of overwritten if it pre-exists. There is no warning that the archive exists and will be overwritten
–file= | -f This option specifies the name of the archive we will use. When creating the file we generally us the extension or suffix of .tar. This isn’t required but it is recommend and does help. Being able to see the suffix gives us an instant understanding of the file type and also helps with tab completion as your shell will only expand .tar files, ( or compressed versions such as .tgz or .tar.gz) when we try to list or restore an archive
These are the minimum options but we still need to supply at least one argument, the file of files to backup. If we supply a directory name it will archive the complete directory and sub-directory structure. Being able to archive a file requires that we have read permissions to a file and read and execute to a directory.
The following example will archive the bash documentation. Running this command from your home directory will ensure the archive is created there. You must be able to write to the directory in which the archive is created:
$ tar --create --file=bashdoc.tar /usr/share/doc/bash
If we needed to add more than one file or directory we can create a list of names that are space separated. In the example below we now include two directories in the archive list:
$ tar --create --file=bashdoc.tar /usr/share/doc/bash /usr/share/doc/bash-completion
We, of course, can mix files and directories together in this list. In the next example we add in the /etc/hosts file as another argument in the list.
$ tar --create --file=bashdoc.tar /usr/share/doc/bash /usr/share/doc/bash-completion /etc/hosts
As the list grows they it may be more convenient to use a text file as an input list. Regular backups may also benefit from a list of files to include in the archive. This is where we can use the –files-from or -T option. Let me show you. Assuming we would like to backup the 2 directories and single file we used in the previous example, we need to create this as a list in a text file:
$ echo '/usr/share/doc/bash' >> bashdoc $ echo '/usr/share/doc/bash-completion' >> bashdoc $ echo '/etc/hosts' >> bashdoc $ cat bashdoc /usr/share/doc/bash /usr/share/doc/bash-completion /etc/hosts
The last command executed, cat, list the contents of our newly created text file which is just a list of files names to backup, one per line. To archive these files we can use the following example:
$ tar --create --file=bashdoc.tar --files-from bashdoc
In the next tutorial we look at listing tar archives in more details. The video now follows