0
0
Linux CLIscripting~5 mins

tar with compression (-z, -j, -J) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to save space when storing or sending files. The tar command can bundle files together and compress them to make the size smaller. This helps when you want to move many files as one package or save disk space.
When you want to create a backup of a folder and reduce its size to save disk space.
When you need to send multiple files over the internet as a single compressed file.
When you want to archive logs daily and keep them compressed to save storage.
When you want to extract a compressed archive you received from someone else.
When you want to compress files using different compression methods like gzip, bzip2, or xz.
Commands
This command creates a compressed archive named archive.tar.gz from the folder 'myfolder' using gzip compression (-z). The -c flag means create, -f specifies the filename.
Terminal
tar -czf archive.tar.gz myfolder
Expected OutputExpected
No output (command runs silently)
-c - Create a new archive
-z - Compress archive with gzip
-f - Specify archive filename
This command extracts the contents of the gzip compressed archive archive.tar.gz. The -x flag means extract, -z decompresses gzip, and -f specifies the file.
Terminal
tar -xzf archive.tar.gz
Expected OutputExpected
No output (command runs silently)
-x - Extract files from archive
-z - Decompress gzip archive
-f - Specify archive filename
This command creates a compressed archive named archive.tar.bz2 from 'myfolder' using bzip2 compression (-j). It saves more space but is slower than gzip.
Terminal
tar -cjf archive.tar.bz2 myfolder
Expected OutputExpected
No output (command runs silently)
-c - Create a new archive
-j - Compress archive with bzip2
-f - Specify archive filename
This extracts the bzip2 compressed archive archive.tar.bz2. The -x flag extracts, -j decompresses bzip2, and -f specifies the file.
Terminal
tar -xjf archive.tar.bz2
Expected OutputExpected
No output (command runs silently)
-x - Extract files from archive
-j - Decompress bzip2 archive
-f - Specify archive filename
This creates a compressed archive named archive.tar.xz from 'myfolder' using xz compression (-J). It usually compresses better but takes more time.
Terminal
tar -cJf archive.tar.xz myfolder
Expected OutputExpected
No output (command runs silently)
-c - Create a new archive
-J - Compress archive with xz
-f - Specify archive filename
This extracts the xz compressed archive archive.tar.xz. The -x flag extracts, -J decompresses xz, and -f specifies the file.
Terminal
tar -xJf archive.tar.xz
Expected OutputExpected
No output (command runs silently)
-x - Extract files from archive
-J - Decompress xz archive
-f - Specify archive filename
Key Concept

If you remember nothing else from this pattern, remember: use -z for gzip, -j for bzip2, and -J for xz compression with tar to create or extract compressed archives.

Common Mistakes
Forgetting to use the -f flag with tar when specifying the archive filename.
Tar will not know which file to create or extract, causing an error or unexpected behavior.
Always include -f followed by the archive filename when creating or extracting.
Using the wrong decompression flag when extracting (e.g., using -z for a bzip2 archive).
Tar will fail to decompress the archive because the compression method does not match.
Match the compression flag used during creation: -z for gzip, -j for bzip2, -J for xz.
Trying to compress a file without the -c flag or extract without the -x flag.
Tar won't know whether to create or extract, leading to errors or no action.
Use -c to create archives and -x to extract archives.
Summary
Use tar with -c and -f flags to create archives and specify the archive filename.
Add -z, -j, or -J to compress archives with gzip, bzip2, or xz respectively.
Use tar with -x and matching compression flags to extract compressed archives.