How to compress files using tar command on Ubuntu Linux

To compress files using the tar command on Ubuntu Linux, you can use the tar command with the -z or -j options to create a compressed archive file.

Here are the steps:

  1. Open a terminal window.
  2. To compress files and directories using the gzip algorithm, use the following command:
tar -czvf archive.tar.gz file1 file2 dir1 dir2

This command will create a compressed archive file named archive.tar.gz that contains file1, file2, dir1, and dir2. The -c option tells tar to create an archive, the -z option specifies the gzip compression algorithm, and the -v option enables verbose output so you can see the progress of the operation.

  1. To compress files and directories using the bzip2 algorithm, use the following command:
tar -cjvf archive.tar.bz2 file1 file2 dir1 dir2

This command will create a compressed archive file named archive.tar.bz2 that contains file1, file2, dir1, and dir2. The -c option tells tar to create an archive, the -j option specifies the bzip2 compression algorithm, and the -v option enables verbose output.

  1. To extract files from a compressed archive file, use the following command:
tar -xzvf archive.tar.gz

This command will extract the contents of the archive.tar.gz file into the current directory. The -x option tells tar to extract files from an archive.

  1. To extract files from a compressed archive file created with the bzip2 algorithm, use the following command:
tar -xjvf archive.tar.bz2

This command will extract the contents of the archive.tar.bz2 file into the current directory.

Note that you can also use the tar command to create archives without compression by omitting the -z or -j options.

Leave a Comment