How to Compress and Extract Files with TAR in Linux

The tar command stands for “tape archive” and the basic Unix command to archive files. By default the command will save the archive in a .tar file but it can also save it to a gzip file with the .gz extension. The tar command typically is available in most Linux distro.

There are also some alternatives such as the gzip and gunzip which specially handles the .gz file and unzip which is for the .zip extension. These alternatives may not always be available so check the distro package manager if they can be installed as an additional command.

General syntax for tar command:

tar [OPTIONS] [FILES]...
1. Create an archive
tar -cf [archive] [file]

The -c and -f options create a new archive file with the files specified to be archived.

Example:

tar -cf example.tar new1.txt new2.txt

Here’s a breakdown of what each part of the command does:

  • -c: This option tells tar to create a new archive.

  • -f: This option allows you to specify the name of the archive file. In this case, the name of the archive file is example.tar.

  • example.tar: This is the name of the archive file that you’re creating.

  • new1.txt new2.txt: These are the files that you’re adding to the archive. In this case, you’re adding two files, new1.txt and new2.txt, to the archive.

So, the command tar -cf example.tar new1.txt new2.txt creates a new archive file named example.tar and adds the files new1.txt and new2.txt to this archive. Do note that archive file is not compressed; it’s simply a collection of the files.

2. List contents of an archive
tar -tf [archive]

The -t option allows you to view the contents of the archive file without decompressing it.

Example:

Assuming example.tar is a tar archive that contains the files file1.txt, file2.txt, and a directory named dir1 with a file file3.txt inside it, the output would look something like this:

file1.txt
file2.txt
dir1/
dir1/file3.txt
3. Create a Gzip archive
tar czf [archive.tar.gz] [file]

Use the -z option with the tar command to create a new archive using gzip compression.

Example:

If you want to create a gzip compressed tar file named example.tar.gz and zip new1.txt and new2.txt into it, you would use the following command:

tar -czf example.tar.gz new1.txt new2.txt
4. Extract an archive
tar -xf [archive]

The -x option allows you to extract files from an archive to your current working directory. It is also possible to extract certain files by specifying the file names.

Example:

If you want to extract the example.tar archive that contains the text files new1.txt and new2.txt, you would use the following command:

tar -xf example.tar

After running this command, the files new1.txt and new2.txt will be extracted from the example.tar archive and will be available in the current directory. If the files were stored in the archive with a directory structure, that structure will be recreated in the current directory.

5. Add files to an existing archive
tar -rf [archive] [file_to_add]

To append a file or directory to an existing tar archive file, use the -r option. This option adds files to the end of an archive.

Example:

If you want to add the new1.txt file to the example.tar archive, you would use the following command:

tar -rf example.tar new1.txt

After running this command, the new1.txt file will be added to the end of the example.tar archive.

6. Merge archives
tar -Af [archive] [archive_to_be_added]

The -A or --concatenate or --catenate option tells tar to append tar files to an archive, and the -f option specifies the file to use.

Example:

If you want to merge the files of the file1.tar archive into the file2.tar archive, you would use the following command:

tar -Af file2.tar file1.tar

After running this command, the files from file1.tar will be appended to the file2.tar archive.

7. Delete a file from an archive
tar --delete -f [archive] [file]

The --delete option allows you to delete a file or multiple files from an archive at once.

Example:

To remove the new1.txt file from the example.tar archive, you would use the following command:

tar --delete -f example.tar new1.txt

After running this command, the new1.txt file will be removed from the example.tar archive.

8. Find differences between archive and file
tar -df [archive] [file]

To determine the differences between an archive and a file, use the -d option.

Example:

If you want to find the difference between the example.tar archive and the new1.txt file, you would use the following command:

tar -df example.tar new1.txt

After running this command, tar will compare the new1.txt file in the file system with the new1.txt file in the example.tar archive and report any differences.

9. Add only new files to the archive
tar -uf [archive] [files_to_add]

Use the -u option to add files that are newer than the file in the archive. The newer files do not overwrite the older ones in the archive.

Example:

If you want to append all new text files from the current directory to the example.tar archive, you would use the following command:

tar -uf example.tar *.txt

After running this command, all new text files (*.txt) in the current directory will be appended to the example.tar archive.

10. Extract archive to a specific directory
tar -xf [archive] -C [dirpath]

The -C option allows you to extract the archive to a certain directory by specifying the destination path.

Example:

To extract the example.tar archive to the dir/test folder, you would use the following command:

tar -xf example.tar -C dir/test
More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail