How to create tar.gz or tar.bz2 files

A tarball (or tar.gz or tar.bz2) is nothing but a computer file format that combines and compresses multiple files, including information about them, such as the ownership, permissions, and timestamp. Tarballs are common file format on a Linux or Unix-like operating systems. Tarballs are often used for distribution of software/media or backup purposes. This page shows how to create .tar.gz or .bz2 files in Linux using the Linux command line.

Creating tar.gz File

ls -la $HOME/src/hugo/chavkov.com

Sample output

      ~/src/hugo/chavkov.com  on    main-github:main

total 80
drwxrwxr-x 12 vchavkov vchavkov 4096 Mar 26 19:34 .
drwxrwxr-x  7 vchavkov vchavkov 4096 Mar 23 18:08 ..
-rw-rw-r--  1 vchavkov vchavkov   27 Mar 21 11:39 .config-prod.sh
-rw-rw-r--  1 vchavkov vchavkov  132 Mar 13 18:18 .config.sh
drwxrwxr-x 10 vchavkov vchavkov 4096 Mar 27 18:10 .git
-rw-rw-r--  1 vchavkov vchavkov   35 Mar 13 18:18 .gitignore
-rw-rw-r--  1 vchavkov vchavkov   94 Mar 21 11:39 .gitmodules
-rw-rw-r--  1 vchavkov vchavkov    0 Mar 13 18:18 .hugo_build.lock
-rw-rw-r--  1 vchavkov vchavkov  597 Mar 21 16:59 Makefile
-rw-rw-r--  1 vchavkov vchavkov   40 Mar 21 16:59 README.md
drwxrwxr-x  2 vchavkov vchavkov 4096 Mar 13 18:18 archetypes
drwxrwxr-x  3 vchavkov vchavkov 4096 Mar 21 16:59 assets
-rw-rw-r--  1 vchavkov vchavkov 2522 Mar 27 15:21 config.toml
drwxrwxr-x  7 vchavkov vchavkov 4096 Mar 27 15:20 content
drwxrwxr-x  2 vchavkov vchavkov 4096 Mar 13 18:18 data
drwxrwxr-x  8 vchavkov vchavkov 4096 Mar 27 15:27 layouts
-rw-rw-r--  1 vchavkov vchavkov  210 Mar 26 19:34 netlify.toml
drwxr-xr-x  2 vchavkov vchavkov 4096 Mar 21 10:15 public
drwxr-xr-x  3 vchavkov vchavkov 4096 Mar  3 18:43 resources
drwxrwxr-x  3 vchavkov vchavkov 4096 Mar  3 18:50 scripts
drwxrwxr-x  5 vchavkov vchavkov 4096 Mar 21 16:59 static

Most Linux distributions come include the GNU version of tar that supports compressing archives.

The general form of the command for creating tar.gz files is as follows:

tar -czf chavkov.com.tar.gz file-name...

Here’s what the command options mean:

-c - instructs tar to create a new archive. -z - sets the compression method to gzip. -f archive-name.tar.gz - specifies the archive name. file-name… a space-separated list of files and directories to be added to the archive.

The user running the command must have write permissions on the directory where the tar.gz file will be created, and read permissions on the files being added.

For example, to create an archive named “archive.tar.gz” from “file1” and “file2” you would use the following command: