Working with compression and tar files

Please show me several examples of using the GNU/Linux tar command with compression.

Here are several examples of using the GNU/Linux tar command with compression:

Creating a compressed archive:

  • Using gzip compression:

    Bash
    tar -czvf archive.tar.gz directory_to_compress/
    

    This creates a compressed archive named archive.tar.gz containing the contents of the specified directory.

  • Using bzip2 compression:

    Bash
    tar -cjvf archive.tar.bz2 directory_to_compress/
    

    This creates a compressed archive named archive.tar.bz2 using bzip2 compression.

  • Using xz compression:

    Bash
    tar -cJvf archive.tar.xz directory_to_compress/
    

    This creates a compressed archive named archive.tar.xz using xz compression, often providing the best compression ratio.

Extracting a compressed archive:

  • Extracting a gzip-compressed archive:

    Bash
    tar -xzvf archive.tar.gz
    
  • Extracting a bzip2-compressed archive:

    Bash
    tar -xjvf archive.tar.bz2
    
  • Extracting an xz-compressed archive:

    Bash
    tar -xJvf archive.tar.xz
    

Additional options for compression:

  • Specifying compression level with gzip:

    Bash
    tar -czvf archive.tar.gz -9 directory_to_compress/
    

    This compresses using gzip with the highest compression level (9).

  • Auto-selecting compression based on file extension:

    Bash
    tar -caf archive.tar.bz2 directory_to_compress/
    

    This uses bzip2 compression because the archive name has a .bz2 extension.

Key points to remember:

  • The -c option is used for creating an archive.
  • The -v option provides verbose output, showing the files being processed.
  • The -f option specifies the name of the archive file.
  • The -z-j, or -J options specify the compression method (gzip, bzip2, or xz, respectively).
  • You can use -C to specify a different directory to extract files into.
  • For more detailed information, refer to the man tar command.
You should also read:

Configuration files

Christiane is a RHEL Sysadmin at CNN: Christiane is going to take her RHEL exam in 1 week. Please list and explain the…