tar - extract or create compressed files

The tar command is used to create compressed archives which represent a file or collection of files. A tar file, commonly known as a “tarball,” a gzip, or a bzip file, will have an extension ending with .tar or .tar.gz. A tar file is a special format that groups files into one. It’s similar to a .zip file in that it can hold multiple files, but it is its own file type. Tar files have the suffix .tar or .tar.gz. Using the tar command in Linux, you can open, view, and create a tar file.

Extract a tar File

If you have a tar archive file that you want to open, all you need to do is use this command:

tar -xzvf files.tar.gz

Here’s what the flags mean that we have used:

  • x: Exctract an archive
  • z: Compress the archive using gzip
  • v: Show progress in the terminal
  • f: Specify the filename of the archive

By default, this command will extract the contents of archive_name.tar.gz into your working directory. You can override this behavior using the “-C” flag at the end of the command. This flag allows you to specify a directory into which the contents of the tar file should be moved:

tar -xzvf files.tar.gz -C /home/user/Documents

View a tar File

When you are sent a tar file, your first instinct may be to open it up. How else are you going to see what’s in the file? But, you can preview the file without extracting its contents to your computer. The following command will let you list the contents of a tar file without extracting its full contents to your computer:

tar -tvf files.tar.gz

This command returns an output that is similar to what you would see if you run the Linux ls command. In this case, our list of files reflects the contents of our tar file. We haven’t opened it up; we’ve only peeked inside. The “-tvf” flags tell our command to print out a list of all the files in our tarball.

Create a tar File

You can easily compress files and directories into a single tar file using the tar command:

tar -czvf file_name.tar.gz /path/to/folder

Here’s what the flags mean that we have used:

  • c: Create an archive
  • z: Compress the archive using gzip
  • v: Show progress in the terminal
  • f: Specify the filename of the archive

You can remember these by thinking that “c” is for “create.” The “zvf” are the same flags that we used to open our file in our first example. While these don’t exactly seem memorable, once you’ve opened a few tar files you will get used to the syntax.

In this example, we’ve compressed one directory into a tar file. The tar command allows us to specify multiple directories or files, too. To do so, you need to provide a list of the files and directories that you want to compress.

tar -czvf files.tar.gz /home/user/folder_1 /home/user/folder_2

How to Exclude Files and Directories

Being able to compress an entire folder is useful, but there may be a case where you want to compress an entire folder aside from a file. You can do this by using the “–exclude” flag:

tar -czvf files.tar.gz --exclude=/home/user/folder_1/stuff /home/user/folder_1

This command will archive everything in /home/user/folder_1 excluding the folder “stuff”. We could also combine the “–exclude” flag with a wildcard if we want to exclude all files that end in a particular file extension:

tar -czvf files.tar.gz --exclude=/home/user/folder_1/*.txt /home/user/folder_1

Conclusion

The tar command allows you to compress files into tarballs, read the contents of a tarball and open a tarball.