Looking for how to untar a file?
In this article, we shall look at how to use the tar command to tar a file, tar a folder or, should you be lexically happier with the phrase “tar a directory” then we shall look at how to tar a directory as well.
Tar by itself merely groups one or more files, folders or directories into one big tarball of a file and gives that file the .tar file extension.
Although convenient for sending over the network, it does not really assist with much that scp or sftp cannot achieve when sending files and folders. However, when partnered with a compression tool such as gzip, bzip2 or xz_utils then tar comes into its own. Tar has the ability to not only join multiple files together but also compress them in a single command.
How to tar a single file
Find your target file and execute tar with the –c flag to indicate you are creating an archive. Then add the –f flag to indicate you plan to create the archive as a file. You also need to specify the output file and the input file in that order.
tar –cf my_tarball.tar my_file.txt
This can also be expressed in longhand if it is easier to remember that way:
tar –-create –-file my_tarball.tar my_file.txt
How to tar a folder
Surprising as it may seem, this is done in exactly the same way as you would tar a file. That is, execute tar with the –c flag to indicate you are creating an archive. Then add the –f flag to indicate you plan to create the archive as a file. You also need to specify the output file and the input folder.
tar –cf my_tarball.tar my_folder
How to tar a directory
As you may have guessed, with folder and directory being the same thing, the command for this is identical to the command for how to tar a folder. Execute tar with the –c flag to indicate you are creating an archive. Then add the –f flag to indicate you plan to create the archive as a file. You also need to specify the output file and the input directory.
tar –cf my_tarball.tar my_directory
How to tar multiple files
As you may have gleaned from the above, the process is basically the same as previously. However, this time you need to specify all the files and/or folders (separated by spaces) after the name of the archive being created.
tar –cf my_tarball.tar my_file_1.txt my_folder my_file_2.txt my_file_3.txt my_directory
How to tar with gzip
As tar has gzip compatibility built into it now, all you need to do is add an extra flag to specify you want to create the archive and then gzip it.
Execute tar with the –c flag to indicate you are creating an archive, -z to specify you would like to compress it with gzip and the –f flag to indicate the archive files you plan to use:
tar –czf my_tarball.tar.gz my_file_1.txt my_folder my_directory my_file_2.txt
You can also write this in longhand if it is easier to remember that way:
tar –-create –-gzip –-file my_tarball.tar.gz my_file.txt my_folder my_directory
How to tar with bzip2
If you would prefer to use the slightly more space saving bzip2 compression instead of gzip, tar supports that, too. All you need to do is change the extra flag used to specify you want to create the archive and then bzip2 it instead of gzip it.
Execute tar with the –c flag to indicate you are creating an archive, -j to specify you would like to compress it with bzip2 and the –f flag to indicate the archive files you plan to use:
tar –cjf my_tarball.tar.bz2 my_file_1.txt my_folder my_directory my_file_2.txt
You can also expand this to longhand if it is easier to remember that way:
tar –-create –-bzip2 –-file my_tarball.tar.bz2 my_file.txt my_folder my_directory
How to tar with xz
If you prefer to use xz compression instead of gzip or bzip2, tar supports that as well. xz compression is most effective when compressing large files. Again, all you need to do is change the extra flag used to specify you want to create the archive and then compress it with xz instead of gzip or bzip2.
Execute tar with the –c flag to indicate you are creating an archive, -J to specify you would like to compress it with xz and the –f flag to indicate the archive files you plan to use:
tar –cJf my_tarball.tar.xz my_file_1.txt my_folder my_directory my_file_2.txt
You can also write this in longhand if it is easier for you to remember that way:
tar –-create –-xz –-file my_tarball.tar.xz my_file.txt my_folder my_directory
Verbose mode
Tar shows you more detail about what it is doing if you add the –v flag (–verbose) to any of the above commands. This is particularly useful when you either have many files or several large files. Without the verbose flag specified, tar seems to sit there doing nothing until it suddenly finishes which can be worrying or disconcerting for many users. By adding the verbose flag, it shows you what stage of the current job it is at:
tar –czvf my_tarball.tar.gz my_file.txt my_folder my_directory
This can also be expressed in longhand if it is easier to remember that way:
tar --create --gzip --verbose --file my_tarball.tar.gz my_file.txt my_folder my_directory
Additional options
Tar comes with plenty of other options that can be used in combination with the above.
Exclude
The exclude function allows you to do exactly that. You can specify what you do not want included in your archive from a set that does include that. For example, if you have a folder called “private” in “my_folder”, you can specify to archive “my_folder” whilst excluding “private”. This saves you the bother of having to move files around to be able to create archives of exactly what you want:
tar –czf my_tarball.tar.gz --exclude=”private” my_folder
Remove files
The remove files option is great for when you are migrating your data from one machine to another or trying to archive old data. Once tar has finished making the tarball file successfully, it then removes the source files:
tar --czf my_tarball.tar.gz my_file.txt my_folder my_directory -–remove-files
Append
Tar will also let you append (add) additional files to a pre-existing tarball by using the append flag. This is handy when you make a tarball from files scattered all over your machine and accidentally forget one. Usage is the same but we specify the –r (–append) flag to indicate we are retroactively appending to a pre-existing tarball:
tar –rf my_tarball.tar my_file_2.txt my_folder_2 my_directory_2
Note: This only works with non-zipped tarballs. If you want to add to a zipped tarball, you will need to unzip it first with gunzip, bunzip or xz_dec. Next, you append the file(s) to the tarball and finally re-zip the tarball with gzip, bzip2 or xy accordingly. Unless the compressed tarball is large or complex, it is often simpler to delete it and recreate it.
Conclusion
There are a wide range of methods of how to tar a file but the most common by far is creating a tarball with gzip
tar –czf my_tarball.tar.gz my_file_1.txt my_folder my_directory my_file_2.txt
For further reading, you may wish to look at the official ManPages for tar.

Comments