Linux Tar Files

From Dev Wiki
Jump to navigation Jump to search

Tar is one form of file compression commonly used on Linux machines.

Compression Types

There are two common forms of tar compression, .gz and .xz.


The .gz compression format is handled by gzip algorithms. Gzip was made in the 90's and has been around longer.

Meanwhile, the .xz is a newer compression format that tends to compress files to smaller sizes.


Generally speaking, new files should be compressed to the .xz format, while .gz is still around for compatibility.

Compress a File

To compress a file, one of the following commands (First one is .xz format, second is gzip format.):

 tar -cJpvf <name_of_archive> <path_to_file_or_directory> 
 tar -czpvf <name_of_archive> <path_to_file_or_directory> 

Where:

  • <name_of_archive> is the name of the compressed file you'll create.
  • <path_to_file_or_directory> is the path to the file or directory you wish to compress.
  • Provided flags stands for:
    • c - Creates an archive (compressed file).
    • J - Compresses using xz format.
    • z - Compresses using gzip format.
    • p - Preserves file permissions.
    • v - Displays progress in terminal while compressing.
    • f - Allows specification of archive name.

Extract a File

To extract a compressed file, use the command:

 tar -xvf <name_of_archive> -C <location_to_decompress_to> 

Where:

  • <name_of_archive> is the name of the compressed file you'll create.
  • <location_to_decompress_to> is the path you wish to extract contents to.
  • Provided flags stands for:
    • x - Extracts an archive (compressed file).
    • v - Displays progress in terminal while compressing.
    • f - Allows specification of archive name.

For extracting, you usually don't need to specify the compression type as it can normally determine for you. In instances where you must specify compression type, add one of the following flags:

    • J - Extracts using xz format.
    • z - Extracts using gzip format.