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:
Bashtar -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:
Bashtar -cjvf archive.tar.bz2 directory_to_compress/
This creates a compressed archive named
archive.tar.bz2
using bzip2 compression.Using xz compression:
Bashtar -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:
Bashtar -xzvf archive.tar.gz
Extracting a bzip2-compressed archive:
Bashtar -xjvf archive.tar.bz2
Extracting an xz-compressed archive:
Bashtar -xJvf archive.tar.xz
Additional options for compression:
Specifying compression level with gzip:
Bashtar -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:
Bashtar -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.