Using File Archivers and Compression Programs in Linux
The purpose of this guide is to teach you how to use the various file archivers and compression programs in Linux.
- A working Linux PC (Any Linux distro)
tar: The tape archival
It is often desirable to combine groups of files into a single file called an archive when sending them to someone by emails, FTP or code repositories. The tar (tape archiver) command is an archiver which can be used for archiving files.
We will now consider several key options of tar. Only one option can be used at a time.
-c Creates an archive
-x Extracts files from an archive
-t Displays files in an archive (table of contents)
The above 3 options are often used together with the following 2 options:
-f Specifies the name of the archive (filename)
-v Displays the progress (verbose)
This is how we create an archive from 2 uncompressed files:
$ tar -cvf examplearchive.tar foo barThe shell returns:
a foo 364k a bar 3785kThe 2 example files above are foo and bar. Note that the verbose output lines all begin with an "a" (meaning: append).
By convention, the .tar extension is used so that one will remember to use the same tar command to extract files from the archive. The files won't be deleted after they are combined to form an archive. To delete them, use the --remove-files option.
$ tar -cvf --remove-files examplearchive.tar foo bar
Now, to extract files from an archive, we use the -x option:
$ tar -xvf examplearchive.tarShell output:
x foo, 372267 bytes, 728 tape blocks x bar, 3875302 bytes, 7569 tape blocksNote that x indicates extract and the archive won't be deleted after the extraction is complete.
Perhaps you want to know the content of the files in an archive before deciding what you want to do to them. You may use the -t option:
$ tar -tvf examplearchive.tarShell output (listing):
-rw-r--r-- 102/10 372267 Jan 10 10:05 2018 foo -rw-r--r-- 102/10 3875302 Jan 10 10:01 2018 barA short video on tar's usage:
We have just scratched the surface of tar, feel free to visit the following links to learn more about tar:
GNU tar manual
tar Linux man page
Das, S. (2013). Your UNIX/Linux: The ultimate guide. New York: McGraw-Hill.