Linux Commands

How To Create a Tarball in Linux

Tar offers a great way of combining multiple files and directories in a single package. Tarball facilitates efficient data transmission and allows you to conserve disk space by reducing the file size of numerous files. 

So whether you are looking to create backups, transfer files, or distribute software packages, it is your one-stop solution for everything. However, many users have yet to learn how to make a tarball. This short guide has complete information on creating a tarball in Linux.

How To Create a Tarball in Linux

Although tar comes preinstalled in most Linux distributions, you can verify it using:

tar --version

checking-the-version-of-tar

If the tar utility is not available in the system, please install it by entering the following commands:

Operating System Command
Debian/Ubuntu sudo apt install tar
Fedora sudo dnf install tar
Arch Linux sudo pacman -Sy tar
openSUSE sudo zypper install tar

Once you are done, please run the below command to archive a directory: 

tar -cvf archive.tar file.txt directory_name

Here, replace the file.txt and directory_name with the names of the files and directories you want to archive. For multiple files/directories, mention their names separated by a space.

The -c option tells the utility to create a new tarball.  The -v option enables the verbose mode to display the list of files included in this archive.  The -f option lets you specify the name of the new tarball.

For instance, let’s archive the Documents directory in the doc.tar:

tar -cvf doc.tar Documents

archive-a-directory-using-tar-command

 

If you want to list and display the content of the tar file, then you can use the -t option: 

tar -tvf doc.tar

displaying-the-content-of-a-directory-using-tar-command

Similarly, you can archive multiple directories into a single tarball using the below command: 

tar -cvf list.tar Documents Music

archive-multiple-directories-using-tar

A Quick Wrap-up

So this is how you can archive and create a tarball in Linux. In this quick guide, we have included simple examples to explain how to create the tarball and check its content. Moreover, we have explained an easy approach to archive multiple directories into a single tarball. You should use the v option as it will help you to verify the archived content. 

About the author

Prateek Jangid