How to Cre­ate & Extract tar.gz Files in Linux

The most com­mon com­pressed archive file format in Linux is the tar.gz format. Tar file is an archive file format. Tar.gz is a com­pressed tar file.

How to cre­ate a com­pressed tar.gz file from a folder or file in Linux?

In order to cre­ate a com­pressed tar.gz archive from a folder/​file we need to run the fol­low­ing tar com­mand:
tar czf new-tar-file-name.tar.gz file-or-folder-to-archive

Here is the com­mand explan­a­tion:

tar — the tar com­mand.

c — cre­ate new archive.

z — com­press the archive using gzip.

f — use archive file.

new-tar-file-name.tar.gz — the name of the tar.gz to cre­ate.

file-or-folder-to-archive — the name of the folder we want to archive.

How to cre­ate a com­pressed tar.gz file from mul­tiple files and folders in Linux?

In order to cre­ate a com­pressed tar.gz file from mul­tiple files or/​and folders we need to run the same tar com­mand we used when we archived a single file/​folder and to append the rest of the files/​folders’ names to it.
tar -czf new-tar-file-name.tar.gz file1 file2 folder1 folder2

How to extract a com­pressed tar.gz file in Linux?

tar -xzf tar-file-name.tar.gz

Here is the com­mand explan­a­tion:

tar — the tar com­mand.

x — extract the archive.

z — uncom­press the archive using gzip.

f — use archive file.

tar-file-name.tar.gz — the name of the tar.gz to cre­ate.

The tar com­mand will extract all the files/​folders in the archive to the cur­rent dir­ect­ory.