How to compress and decompress files on Linux?

To compress and decompress files on Linux(Ubuntu), you can use the following commands:

Compress files

To compress a file or directory, you can use the tar command.

1
tar -czvf archive_name.tar.gz file_or_directory_name

The options used in this command are:

  • -c: create a new archive
  • -z: compress the archive with gzip
  • -v: show the progress while archiving
  • -f: specify the filename for the archive

For example, to compress a directory named my_folder, you can use the following command:

1
tar -czvf my_folder.tar.gz my_folder

This command will create a new compressed file named my_folder.tar.gz.

Decompress files

To decompress a file or directory, you can use the following command.

1
tar -xzvf archive_name.tar.gz

The options used in this command are:

  • -x: extract files from archive
  • -z: uncompress the archive with gzip
  • -v: show the progress while extracting
  • -f: specify the filename of the archive

For example, to decompress the my_folder.tar.gz file that we created earlier, you can use the following command:

1
tar -xzvf my_folder.tar.gz

This command will extract the contents of my_folder.tar.gz to a directory named my_folder.