Creating and extracting a file archive
If you need to archive several small files or complete directory constructions, you should first combine them into a file archive. If the individual file or the file archive construction to be transferred is large, you should compress it.
To create a file archive, use the command tar or gtar (GNU tar). The latter should preferably be used, because archives made with the gtar command are interchangeable between all machines.
Below is an example of using the gtar command:
gtar cvf $TMPDIR/progdir.tar progdir
gtar tvf $TMPDIR/progdir.tar
Here a directory construction under progdir parent directory was compiled into a tar file and named $TMPDIR/progdir.tar. Additionally, to check the result, the archive contents were printed out by using the gtar command’s tvf selectors.
The file archive is extracted by using the gtar command’s selector -x. In the example below
cd uusiprog
gtar xvf $TMPDIR/progdir.tar
a new, empty directory is first opened and the file archive is extracted there. The command tar creates subdirectory progdir under parent directory uusiprog; and the subdirectories and files are saved under progdir.
Compressing files
Compressing saves storage space, but it may take a lot of time. Therefore, it should be used mainly for large files that will need to be stored for a long time.
Compressing is done with the command gzip or compress. Of these two, gzip is recommended. The file name extensions for files compressed with gzip and compress are .gz and .Z, respectively. Both commands delete the original file.
Below is an example of compressing a file archive.
gzip progdir.tar
A file archive can also be created and compressed with one command by specifying the gtar command with selector –z.
gtar -cvzf progdir.tar.gz progdir
The compressed file is decompressed with command gunzip as follows:
gunzip progdir.tar.gz
The compressed tar archive can be decompressed with selector -z added to the gtar-command:
gtar -xvzf progdir.tar.gz
Commands gunzip and uncompress make the decompression permanent. To avoid the permanent function, you can use command zcat. This command extracts compressed files and prints out their contents on the stdout channel. The zcat command is usually used in conjunction with pipes (|).
In the example below, the compression of file data.tar is extracted and the data is fed directly with the tar command:
zcat data.tar.Z | gtar xvf -
Below, command zcat is used together with certain other commands:
zcat huge.file.Z | tail
zcat huge.file.Z | grep 'warning'
First, the tail of the packed file huge.file.Z was printed out, and then all rows containing the word warning were searched and printed out.