A tar file, also known as a tarball, is a collection of files and directories wrapped up into one single file. This best way to package multiple files or directories without compressing. A tar file always retains file structure and can preserve metadata. You can also create a compressed tar file typically suffixed with .tar.gz (gzip) or .tar.bz2 (bzip2). This is commonly known as an archive file ( can be an uncompressed file as well). Helps easy storage and transfer.
The tar short for Tape Archive (.tar) is used in Linux to create archive files. This could compressed or uncompressed file.
tar [options][archive-file] [file or directory to be archived]
[options]: The operation mode and options you want to use, like
-ffor file, and many others.
[archive-file]: The name of the archive you are creating or extracting.
[file or directory to be archived]: The file or directory that you want to archive or extract.
Useful tar command options:
| ||Create an archive file|
|Verbose mode, show the progress when creating the archive file|
| ||Extract an archive file|
|Compress the archive using gzip compression algorithm|
|Compress the archive using bzip2 compression algorithm|
|-J||Compress the archive using xz compression algorithm|
|Display files in the archive file|
|Adds file or directory to a tar file|
|Allows you to specify an archive filename.|
Recommended Read: tar Cheat Sheet: Quick Reference Guide
Creating tar archives
To create a tar archive file use -c option followed by -f option to specific filename of the archive
tar -cf project.tar file1.txt file2.txt dir1
This command creates an archive named project.tar that contains file1.txt, file2.txt, and dir1 in the current directory. Remember this is not a compressed file. You can add -v option to list all the files being archived. Then the option would be -cvf.
The color code for archive file is bright red (by default) and you can easily identify it using the ls command.
Extracting tar archives
Let's now try to extract .tar archive file which we created earlier.
To extract a tar archive, use the
-x (extract) option, again followed by
-f to specify the archive file:
tar -xf project.tar
This command extracts all files and directories from project.tar to the current directory. After extraction, you can delete the tar archive file if it's not required anymore.
Instead of extracting to the current directory, you can use -C (Change to directory) option to extract tar file to a specific directory.
tar -xf project.tar -C /path/to/directory
Listing contents of tar archives
To view the contents of a tar archive without extracting it, use the
-t (list) option:
tar -tf project.tar
It is good practice to list archive file contents before extracting to make sure you are extracting the right file.
When creating or extracting a tar archive it is good to add > /dev/null at the end of the line. This redirects the command's output to /dev/null, effectively discarding it. This is useful with the
v option if you don't want to see the output but still want any errors to be printed to the terminal.
Adding to existing tar archive
Use option -r to add files to an existing archive file. The archive file should be .tar file.
tar -rvf project.tar file3.txt
This command adds a new file named newfile.txt to the existing archive file named file1.tar.
Note: It's not possible to add files to a compressed archive file. The workaround would be to gunzip first, then add files to .tar file, and then gzip that file.
Use -z option to tell tar to use gzip compression.
tar -czf projectarchive.tar.gz projectdir1
This command creates a gzip-compressed archive file named projectarchive.tar.gz of the directory named projectdir1 in the current directory.
Use -j option to tell tar to use bzip2 compression.
tar -cjf projectarchive.tar.bz2 projectdir1
This command creates a gzip-compressed archive file named projectarchive.tar.bz2 of the directory named projectdir1.
Instead of compressing one whole directory, you can choose multiple directories or files. Simply need to specify the names of the directories you want to compress as arguments to the tar command.
tar -czf projectsarchive.tar.gz projectdir1 projectdir2
This compresses two directories named projectdir1 and projectdir2 into a gzip-compressed tarball named projectsarchive.tar.gz. Remember tar maintains the directory structure when archiving, so when you extract you get a separate directory.
tar -czf filewww.tar.gz /etc/nginx /var/www/html /var/log/nginx
When you extract the filewww.tar.gz archive that you created, the original directories and their content will be recreated in the directory where you perform the extraction. ie The directories /etc/nginx, /var/www/html, and /var/log/nginx will be recreated in the location where you execute the extraction command.
To decompress and extract use -x option with tar command. Doesnt matter whether it's a gzip or bzip2 tar archive use the same option.
Example to extract tar.gz compressed file:
To extract or untar archive file named compressed-file.tar.gz, run the following command:
tar -xzf projectsarchive.tar.gz
This command decompresses and extracts a gzip tar archive file named projectsarchive.tar.gz to the current directory. As mentioned before you can use -C option to change the location where the file is to be extracted.
From the archive file, you can choose specific files for extraction. For example:
tar -xzvf compressed-file.tar.gz "file1.txt" "file2.txt"
The tar command use --exclude option to specify patterns of files or directories that should be excluded from the archive.
tar --exclude="pattern-to-exclude" [options] [archive_name] [path] or tar [options] [archive_name] --exclude="pattern-to-exclude" [path]
Generally, the order of the option doesn't matter in GNU and Linux commands. So you can place --exclude option either before or after the directory or file arguments.
Exclude a single file:
tar --exclude='file3.txt' -zcvf projectexclude.tar.gz .
Exclude multiple files and directories:
tar --exclude='file4.txt' --exclude='Directory1' -zcvf excludedir.tar.gz .
Remember to remove trailing slashes when excluding directories. The pattern matching used by tar treats paths with trailing slashes differently than those without. If you use --exclude='dir1/subdir1' (without a trailing slash), the tar command will exclude subdir1 and its contents from the archive as expected.
Exclude specific file extensions:
tar --exclude='*.txt' -zcvf excludetext.tar.gz .