How tar works in Linux

Table of Contents

The tar command in Linux is used to bundle up multiple files and/or directories. It’s similar to the zip command. However, zip files are compressed by definition; tar files can be compressed, but don’t have to be.

How Tar works in Linux

The basic command for creating tar.gz files is as below:

$ tar -czf archivename.tar.gz filename…

  • -c tells tar to create a new archive.
  • -z sets the compression method to gzip.
  • -f archive-name.tar.gz specifies the name of archive.
  • filename… represents a list of files and directories to be added into the archive with space-separated. (for example :- filename1 filename2 )

For Example :

As an example, we create a Tar File named “example.tar.gz” with “filename1” and “filename2“

$ tar -czf example.tar.gz filename1 filename2

The above command doesn’t show any output on success. So if you want verify that the archive is created or not, then list the directory contents with ls command.

If you wish to create the tar.gz inside a directory, then type full path to the archive file same as below:

$ tar -czf /home/user/example.tar.gz filename1 filename2

Create a Tar File named “wordpress_backup.tar.gz” of the /var/www/wordpress directory:

$ tar -czf wordpress_backup.tar.gz /var/www/wordpress

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.