Tar

From HPC Wiki
Jump to navigation Jump to search

General

Tar is a program used for archiving files and folders which is similar to using zip. The resulting archive has the ending .tar. Since it is commonly used in Linux systems, most Linux distributions can (un-)pack .tar files via command on the shell. An important difference to zip is that tar itself uses no compression. This is why it is often used together with a compression format (resulting in file endings like .tar.gz or .tar.bz2).

Command Syntax

The general syntax of the command for creating an archive is

$ tar -c [options] -f archive.tar file1 [file2 ...]

The syntax for extracting is

$ tar -x [options] -f archive.tar [-C path]
Important options
-c create a new archive
-x extract from existing archive
-f the corresponding archive file (which is either created or extracted), must be followed by a file name and not another option!
-v verbose mode
-C specify path where the files get extracted to
-j additionally (de-)compress with bzip2
-z additionally (de-)compress with gzip

Examples

$ tar -vxf some_archive.tar -C ~/folder

This extracts some_archive.tar into the specified folder. Note that some options can simply be appended (e.g. -vx) while others (namely -f and -C) need additional arguments.


$ tar -xzf some_archive.tar.gz

This decompresses and extracts the specified archive.


$ tar -cjf source.tar.bz2 *.py readme.txt

This archives and compresses all Python scrips and the readme.txt in the current folder.

More Information

The complete documentation can be found at https://www.gnu.org/software/tar/manual/index.html

More options and usages can be found at https://www.geeksforgeeks.org/tar-command-linux-examples/