- How To Compress A File or Folder With Tar In Linux
- Step 1: Decide what type of tar compression that you want. The compression type will determine which file extension you should use. Do you want a .tar.gz, .tar.bz2, or .tar.xz file?
- How To Compress A Folder or File With Tar
- How To Compress A Folder Or File With Tar Using Gzip Compression
- How To Compress A Folder Or FIle With Tar Using bzip2 Compression
- How To Compress A Folder Or FIle With Tar Using xz Compression
- Quick Notes
- Permissions
- Compress a folder with tar?
- 2 Answers 2
- Archive, Compress, and Extract Files in Linux Using the Command Line
- Archive a Directory
- Compression with gzip
- Extract a Tarball
- Common Options for Compressing and Archiving Files in Linux
How To Compress A File or Folder With Tar In Linux
To compress a file or folder with the tar command is easy in Linux. Read this article to learn about various ways to compress files or folders with tar.
tar is a ubiquitous command in Linux. Most modern Linux distros should come with tar by default.
Now, I will discuss how to compress a file or folder with tar in Linux.
Step 1: Decide what type of tar compression that you want. The compression type will determine which file extension you should use. Do you want a .tar.gz, .tar.bz2, or .tar.xz file?
Each type of tar file is made with a different compression algorithm. .tar.gz files tend to be able to be compressed quickly, but the compression might be worse than the others. That is to say, the compressed tar file would likely be larger.
.tar.xz files in my experience have among the best compression of any tar algorithm. So if saving space is your #1 priority, then .tar.xz would be good. But one downside of .tar.xz is that it tends to be much slower than other compression algorithms.
.tar.bz2 files may be made slightly slower than .tar.gz files, but the compression may be better.
With tar, compressing 1 file, or 1 folder can be done with basically identical tar commands.
How To Compress A Folder or File With Tar
First let’s assume we want to use Gzip compression which would go with .tar.gz files.
If you have a folder named ‘my_folder’ and you wanted to compress it and all of its files you could do something like:
tar cfvz my_folder.tar.gz my_folder
Now, let’s break down the above command. The cfvz are ‘flags’ or special parameters passed to the tar command telling tar exactly what you want to do.
In the ‘cfvz’ part of the command, c stands for compress, f stands for force (we will force the compression), v stands for verbose which will give you information about the folder while it is being compressed, and finally z is the specific flag which specifies that we want to use gzip compression (making a .tar.gz file)
I generally will use cfv every time. If I want to compress a folder and its contents quickly, I’ll use cfvz, and if I want the best possible compression, I’ll use cfvJ
How To Compress A Folder Or File With Tar Using Gzip Compression
The above example is how you would compress a folder with Gzip. Again, the ‘z’ part of ‘cfvz’ is the part that tells the tar command that you want to use Gzip compression.
tar cfvz my_folder.tar.gz my_folder
Compressing a file is basically done the same way as compressing a folder in tar.
To compress a file called my_file.txt with Tar Using gzip compression, you would do:
tar cfvz my_file.tar.gz my_file.txt
How To Compress A Folder Or FIle With Tar Using bzip2 Compression
The command to compress a folder with bzip2 instead of gzip is almost identical. You only need to change the final letter for the flags to a j, and change the file extension of the compressed file you are creating from .tar.gz to .tar.bz2
Instead of using the following
tar cfvz my_folder.tar.gz my_folder
To compress a folder called my_folder with Tar Using bzip2 compression, you would do:
tar cfvj my_folder.tar.bz2 my_folder
To compress a file called my_file.txt with Tar Using bzip2 compression, you would do:
tar cfvj my_file.tar.bz2 my_file.txt
How To Compress A Folder Or FIle With Tar Using xz Compression
Creating a Tar file using xz compression is quite similar to making a tar file using gzip or bzip2 compression.
The flag that indicates that you want xz compression is ‘J’. Note that it is important that you use uppercase J. Lowercase j would indicate bzip2 compression. Additionally for xz, you would name your compressed file ending in .tar.xz.
To compress a folder called my_folder with Tar Using xz compression, you would do:
tar cfvJ my_folder.tar.xz my_folder
To compress a file called my_file.txt with tar using xz compression, you would do:
tar cfvJ my_file.tar.xz my_file.txt
Quick Notes
When I gave examples, such as tar cfvJ my_file.tar.xz my_file.txt
You can name your .tar.xz file whatever you want, and could even create it in a different directory than the current one you may be using.
Permissions
If you are trying to tar a folder or file that needs higher permissions, you might need to use sudo with tar.
For example, if you were compressing a website in /var/www/
You might need to do something like
sudo tar cfvz ~/my_website.tar.gz my_website
Want to learn how to extract a tar file? Then read our article titled How To Extract A Tar File In Linux
What did you think of this article. Do you have anything to add? Let’s discuss it in the comments below.
Compress a folder with tar?
I’m trying to compress a folder ( /var/www/ ) to ~/www_backups/$time.tar where $time is the current date. This is what I have:
cd /var/www && sudo tar -czf ~/www_backups $time"
I am completely lost and I’ve been at this for hours now. Not sure if -czf is correct. I simply want to copy all of the content in /var/www into a $time.tar file, and I want to maintain the file permissions for all of the files. Can anyone help me out?
2 Answers 2
To tar and gzip a folder, the syntax is:
tar czf name_of_archive_file.tar.gz name_of_directory_to_tar
Adding — before the options ( czf ) is optional with tar . The effect of czf is as follows:
- c — create an archive file (as opposed to extract, which is x )
- f — filename of the archive file
- z — filter archive through gzip (remove this option to create a .tar file)
If you want to tar the current directory, use . to designate that.
To construct filenames dynamically, use the date utility (look at its man page for the available format options). For example:
cd /var/www && tar czf ~/www_backups/$(date +%Y%m%d-%H%M%S).tar.gz .
This will create a file named something like 20120902-185558.tar.gz .
On Linux, chances are your tar also supports BZip2 compression with the j rather than z option. And possibly others. Check the man page on your local system.
This is perfect, thank you. I have one tiny issue though. After creating a tar file of /var/www, it is placed within /var/www directories in the tar file. Here’s the code i’m using now sudo tar -czf ~/www_backups/$time.tar /var/www/» Imagine i have a file called test.txt inside /var/www. After making a tar copy of the file, when i extract it it will be placed inside /var/www directories. Does that make sense? I hope it does, kinda hard to explain. I will check for BZip2 support, thanks for the suggestion!
That’s why you first cd to the directory you want to package, then tar cf file.tar . — that last . instead of specifying the full path will make the paths inside the archive relative to the current directory. You could also use the -C option for tar (look at the man page).
@Qwertylicious -f (from man page) Read the archive from or write the archive to the specified file.The filename can be — for standard input or standard output.
Examples for Most Common Compression Algorithms
The question title for this is simply «Compress a folder with tar?» Since this title is very general, but the question and answer are much more specific, and due to the very large number of views this question has attracted, I felt it would be beneficial to add an up-to-date list of examples of both archiving/compressing and extracting/uncompressing, with various commonly used compression algorithms.
These have been tested with Ubuntu 18.04.4. They are very simple for general use, but could easily be integrated into the OP’s more specific question contents using the the techniques in the accepted answer and helpful comments above.
One thing to note for the more general audience is that tar will not add the necessary extensions (like .tar.gz ) automatically — the user has to explicitly add those, as seen in the commands below:
# 1: tar (create uncompressed archive) all files and directories in the current working directory recursively into an uncompressed tarball tar cvf filename.tar * # 2: Untar (extract uncompressed archive) all files and directories in an uncompressed tarball recursively into the current working directory tar xvf filename.tar # 3: tar (create gzipped archive) all files and directories in the current working directory recursively into a tarball compressed with gzip tar cvzf filename.tar.gz * # 4: Untar (extract gzipped archive) all files and directories in a tarball compressed with gzip recursively into the current working directory tar xvf filename.tar.gz # Note: same options as 2 above # 5: tar (create bzip2'ed archive) all files and directories in the current working directory recursively into a tarball compressed with bzip2 tar cvjf filename.tar.bz2 * # Note: little 'j' in options # 6: Untar (extract bzip2'ed archive) all files and directories in an tarball compressed with bzip2 recursively into the current working directory tar xvf filename.tar.bz2 # Note: same options as 2 and 4 above # 7: tar (create xz'ed archive) all files and directories in the current working directory recursively into a tarball compressed with xz tar cvJf filename.tar.xz * # Note: capital 'J' in options # 8: Untar (extract xz'ed archive) all files and directories in an tarball compressed with xz recursively into the current working directory tar xvf filename.tar.xz # Note: same options as 2, 4, and 6 above
See the tar man page (best to use man tar on your specific machine) for further details. Below I summarize the options used above directly from the man page:
-c, —create
create a new archive-x, —extract, —get
extract files from an archive-v, —verbose
verbosely list files processed-z, —gzip
filter the archive through gzip-j, —bzip2
filter the archive through bzip2-J, —xz
filter the archive through xz-f, —file=ARCHIVE
use archive file or device ARCHIVE
No need to add the — in front of the combined options, or the = sign between the f option and the filename.
I got all this from my recent article, which will be expanded further into a much more comprehensive article as I have time to work on it.
Archive, Compress, and Extract Files in Linux Using the Command Line
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
tar and gzip provide a standard interface to create archives and to compress files in Linux. These utilities take a large number of files, save them together in an archive, and compresses the archive to save space. tar does not compress files by itself. Used in conjunction with gzip , an archived file can be compressed to reduce disk space. The resulting archived file has the file extension, tar.gz and is sometimes called a “tarball”.
Archive a Directory
mkdir testdir && touch testdir/example.txt
tar -cvf testdir.tar testdir/
Compression with gzip
total 9kB drwxrwxr-x 2 linode linode 5kB Jan 30 13:13 testdir -rw-rw-r-- 1 linode linode 1kB Jan 30 13:29 testdir.tar.gz
Extract a Tarball
The flags used in these example stand for:
- -c : Create a new archive in the form of a tar file.
- -v : Verbose flag, outputs a log after running the command.
- -z : Zips or unzips using gzip .
- -x : Extract a file from the archive.
- -f : Define STDOUT as the filename, or uses the next parameter.
Common Options for Compressing and Archiving Files in Linux
Additional flags used with the tar command are:
Flag | Function |
---|---|
-A | Append tar files to an existing archive. |
-d | Show differences between an archive and a local filesystem. |
-delete | Delete from the archive. |
-r | Append files to the end of an archive. |
-t | List the contents of an archive. |
-u | Append but don’t overwrite the current archive. |
These are the basics for working within the command line. Be sure to check the man pages man tar for a more detailed listing of possible flags when compressing and extracting files.
This page was originally published on Thursday, February 1, 2018.