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.
You’ve just downloaded a file from the internet and it has the “.tar” extension. You try to unzip it using traditional unzipping programs, but it doesn’t want to open. What’s going on?
You may be having trouble because “.tar” files aren’t zipped; they are their own file type. Opening a .tar file can be frustrating, especially because the command you can use to open it has a lot of options.
In this guide, we’re going to help you overcome that frustration. We’ll discuss how you can create a compressed file and how you can open one using the tar command in Linux.
What is the Linux tar Command?
The tar command lets you create compressed archives which contain a particular file or set of files. The resultant archive files are commonly known as tarballs, gzip, bzip, or tar files.
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.
tar Command: Open a File
If you have a tar archive file that you want to open, all you need to do is use this command:
tar -xzvf linux_files.tar.gz
This Linux command line operation returns:
x linux/ x linux/names.txt x linux/app.txt x linux/README.md x linux/config/app.sh
The -xzvf flags tell the tar command that you want to extract files from an archive. That’s a handy way to remember this list of four characters: it starts with x because it allows you to extract files.
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 linux_files.tar.gz -C /home/careerkarma/Documents
This command will extract our archive into the /home/careerkarma/Documents folder on our Linux computer.
tar Command: View a 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 linux_files.tar.gz
Our command returns:
drwxr--r-- 0 James staff 0 Jun 12 11:03 linux/ -rw-r--r-- 0 James staff 89 Jun 12 09:04 linux/names.txt -rw-r--r-- 0 James staff 2 Jun 12 09:57 linux/app.txt -rwxr--r-- 0 James staff 0 Jun 12 08:06 linux/README.md -rw-r--r-- 0 James staff 2 Jun 12 10:26 linux/config/app.sh
This command returns an output that is similar to what you would see if you run the Linux ls command. The ls command lists the files in a directory. 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. In our example above, we can see that all the files in our tarball are in a directory called “linux”. When we open this file up, all of these files will be moved onto our computer.
tar Command: Create a .tar File
In the Linux world, there are a lot of files stored in .tar.gz files. If you’ve got some data you want to store in one, you are in the right place. 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
Consider this example:
tar -czvf linux_archive.tar.gz /home/careerkarma/linux
Our code returns:
a linux a linux/names.txt a linux/app.txt a linux/README.md a linux/config/app.sh
The output of this command shows us a list of the files that have been archived.
This command creates an archive file called “linux_archive.tar.gz” based on the contents of the /home/careerkarma/linux directory. 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. Our tar archive is a single file.
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. Try running the following command:
tar -czvf linux_archive.tar.gz /home/careerkarma/linux /home/careerkarma/linux_guides
Our code returns:
a linux a linux/names.txt a linux/app.txt a linux/README.md a linux/config/app.sh a linux_guides/tutorial.md
You can see that the contents of our “linux” and “linux_guides” folders have been compressed into one archive.
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 at the end of your tar command:
tar -czvf linux_archive.tar.gz /home/careerkarma/linux --exclude=/home/careerkarma/linux/app.txt
This command will archive everything in the /home/careerkarma/linux folder excluding the file “app.txt”. 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 linux_archive.tar.gz /home/careerkarma/linux --exclude=/home/careerkarma/linux/*.txt
All the files in our “linux” folder will be archived except for those which end in “.txt”.
The tar command allows you to compress files into tarballs, read the contents of a tarball and open a tarball. As a reminder, here are the commands you can use to work with tar:
- Open a file: tar -xzvf [filename]
- View a file: tar -tvf [filename]
- Create a file: tar -czvf [name_of_new_file] [files_or_folders_to_archive]
Now you’re ready to start using the tar command like a Linux professional!
To learn more about the command line, read our complete guide on How to Learn Linux.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more