Tar was originally designed for creating archives to store files on magnetic tape, which is why it has its name “Tape ARchive”.This article shows how to use the tar command to extract, list, and create tar archives through practical examples and detailed explanations of the most common tar options.
Table of Contents
- 1 tar Command Syntax
- 2 Creating Tar Archive
- 3 Creating Tar Gz Archive
- 4 Creating Tar Bz2 Archive
- 5 Listing Tar Archives
- 6 Extracting Tar Archive
- 7 Extracting Tar Archive in a Different Directory
- 8 Extracting Tar Gz and Tar Bz2 Archives
- 9 Extracting Specific Files from a Tar Archive
- 10 Extracting Files from a Tar Archive using Wildcard
- 11 Adding Files to Existing Tar Archive
- 12 Removing Files from a Tar Archive
tar Command Syntax
There are two versions of tar, BSD tar , and GNU tar , with some functional differences. Most Linux systems come with GNU tar pre-installed by default.
The general syntax for the tar command is as follows:
tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)]
Creating Tar Archive
To create a tar archive, use the -c option followed by -f and the name of the archive.
For example, to create an archive named archive.tar from the files named file1, file2, file3, you would run the following command:
tar -cf archive.tar file1 file2 file3
Use the -v option if you want to see the files that are being processed.
Creating Tar Gz Archive
Gzip is the most popular algorithm for compressing tar files. When compressing tar archives with gzip, the archive name should end with either tar.gz or tgz.
The -z option tells tar to compress the archive using the gzip algorithm as it is created. For example, to create a tar.gz archive from given files, you would run the following command:
tar -czf archive.tar.gz file1 file2
Creating Tar Bz2 Archive
Another popular algorithm for compressing tar files is bzip2. When using bzip2, the archive name should end with either tar.bz2 or tbz.
To compress the archive with the bzip2 algorithm, invoke tar with the -j option. The following command creates a tar.bz2 archive from the given files:
tar -cjf archive.tar.bz2 file1 file2
Listing Tar Archives
When used with the –list (-t) option, the tar command lists the content of a tar archive without extracting it.
The command below will list the content of the archive.tar file:
tar -tf archive.tar
file1
file2
file3
To get more information such as the file owner , file size, timestamp use the –verbose (-v) option:
tar -tvf archive.tar
file1 -rw-r--r-- linuxize/users 0 2018-09-08 01:19
file2 -rw-r--r-- linuxize/users 0 2018-09-08 01:19
file3 -rw-r--r-- linuxize/users 0 2018-09-08 01:19
Extracting Tar Archive
Most of the archived files in Linux are archived and compressed using a tar or tar.gz format. Knowing how to extract these files from the command line is important.
To extract a tar archive, use the –extract (-x) option followed by the archive name:
tar -xf archive.tar
It is also common to add the -v option to print the names of the files being extracted.
tar -xvf archive.tar
Extracting Tar Archive in a Different Directory
By default, tar will extract the archive contents in the current working directory. Use the –directory (-C) to extract archive files in a specific directory:
For example, to extract the archive contents to the /opt/files directory, you can use:
tar -xf archive.tar -C /opt/files
Extracting Tar Gz and Tar Bz2 Archives
When extracting compressed archives such as tar.gz or tar.bz2 , you do not have to specify a decompression option. The command is the same as when extracting tar archive:
tar -xf archive.tar.gz
tar -xf archive.tar.bz2
Extracting Specific Files from a Tar Archive
Sometimes instead of extracting the whole archive, you might need to extract only a few files from it.
To extract a specific file(s) from a tar archive, append a space-separated list of file names to be extracted after the archive name:
tar -xf archive.tar file1 file2
When extracting files, you must provide their exact names, including the path, as printed by –list (-t).
Extracting one or more directories from an archive is the same as extracting files:
tar -xf archive.tar dir1 dir2
If you try to extract a file that doesn’t exist, an error message similar to the following will be displayed:
tar -xf archive.tar README
tar: README: Not found in archive
tar: Exiting with failure status due to previous errors
Extracting Files from a Tar Archive using Wildcard
To extract files from an archive based on a wildcard pattern, use the –wildcards switch and quote the pattern to prevent the shell from interpreting it.
For example, to extract files whose names end in .js (Javascript files), you can use:
tar -xf archive.tar --wildcards '*.js'
Adding Files to Existing Tar Archive
To add files or directories to an existing tar archive, use the –append (-r) operation.
For example, to add a file named new_file to archive.tar, you would run:
tar -rvf archive.tar new_file
Removing Files from a Tar Archive
Use the –delete operation to remove files from an archive.
The following example shows how to remove the file file1 from archive.tar:
tar --delete -f archive.tar file1