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 what tar files are, how you can create a compressed file and how you can open one using the tar command in Linux.
What is a Tar File?
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.
There are a number of programs out there to open tar files, but this is a tutorial for the tar command. Some developers refer to .tar files as “tarballs.”
How to Open a .tar File
tar command allows you to create, open and view tar files. 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
Our code 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, which 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.
How to 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 should know that the tar command comes with an option to see what’s in a tar file without having to open it up. Consider this command:
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
ls command to list 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 of 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.
How to 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:
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 of 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!