tar utility is used to create archive files by grouping multiple files together. It can also extract files from existing tar archives, display a list of files contained in an archive, add new files to an existing archive, and perform other operations. Originally designed for storing files on magnetic tape, the name “Tape ARchive” reflects its original purpose. This article provides practical examples and detailed explanations of the most commonly used tar options, demonstrating how to create, extract, and list tar archives using the tar command.
tar command syntax
Tar comes in two versions, BSD tar and GNU tar, which have some functional variations. On most Unix systems, GNU tar is installed by default. The tar command’s basic syntax includes specifying an operation and options, followed by the archive name and file name(s):
tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)]
You must include one
OPERATION argument when using the tar command, and it is required. The most commonly used operations include:
-c) Create a new tar archive.
-x) – Extract the entire archive or one or more files from an archive.
-t) – Display a list of the files included in the archive
The most commonly used
-v) – Show the files being processed by the tar command.
-f archive-name) – Specifies the archive file name.
When running tar commands, you have the option of using either the long or short forms of the operations and options. The long forms are easier to understand, while the short forms are quicker to type. The long-form options begin with two dashes (
--), while the short-form options start with a single dash (
-) that can be left out.
Creating a tar archive
Tar can work with a wide range of compression programs, including
compress. When creating a compressed tar archive, it’s common practice to append the compressor suffix to the archive file name. For instance, an archive compressed with gzip should have a name like “archive.tar.gz.”
To create a tar archive, use the
-c option, followed by
-f and the archive name. For example, to create an archive called “archive.tar” from files named file1, file2, and file3, enter the following command:
tar -cf archive.tar file1 file2 file3
You can also use long-form options like
--file=archive.tar to accomplish the same thing.
Tar allows you to create archives from one or more directories or files. By default, directories are archived recursively, unless you use the
--no-recursion option. To create an archive called
user_backup.tar of the
/home/user directory, run:
tar -cf backup.tar /home/user.
If you want to see the files being processed, use the
Creating a tar.gz archive
Gzip is the most widely used algorithm for compressing tar files. When compressing tar archives with gzip, the archive name should end with either
To compress an archive using the gzip algorithm as it’s created, use the
-z option with tar. For instance, to create a tar.gz archive from given files, you can use the command:
tar -czf archive.tar.gz file1 file2
Creating a tar.bz2 archive
Another commonly used algorithm for compressing tar files is bzip2. When compressing tar archives with bzip2, the archive name should end with either
To compress an archive using the
bzip2 algorithm, use the
-j option with tar. For instance, to create a
tar.bz2 archive from given files, use the following command:
tar -cjf archive.tar.bz2 file1 file2/code>
Listing the content of a tar archive
When you use the tar command with the
-t) option, it displays the contents of a tar archive without extracting it.
For example, to list the contents of the
archive.tar file, run the following command:
tar -tf archive.tar
To view more detailed information, including the file owner, file size, and timestamp, use the
tar -tfv archive.tar
Extracting a tar archive
To extract a tar archive, use the
-x) option, followed by the archive name:
tar -xf archive.tar
It’s common practice to include the
-v option to display the names of the files being extracted:
tar -xvf archive.tar
Extracting a tar archive to a different directory
By default, tar extracts the archive contents into the current working directory. To extract the archive files into a specific directory, use the
For instance, to extract the archive contents into the /opt/files directory, use the following command:
tar -xf archive.tar -C /opt/files
Extracting tar.gz and tar.bz2 archives
When extracting compressed archives like
tar.bz2, there’s no need to include a decompression option. The command is the same as when extracting a regular tar archive:
tar -xf archive.tar.gz
tar -xf archive.tar.bz2
Extracting specific files from a tar archive
In some cases, you may need to extract only certain files from an archive, rather than the entire archive.
To extract specific file(s) from a tar archive, add a space-separated list of the file names you wish to extract after the archive name:
tar -xf archive.tar file1 file2
When extracting files, it’s important to provide their exact names, including the path, as displayed by the
--list (<code>-t) option.
Extracting one or more directories from an archive is the same as extracting files:
tar -xf archive.tar dir1 dir2
Extracting files from a tar archive with a specific file extension
To extract files from an archive based on a wildcard pattern, use the
--wildcards switch and enclose the pattern in quotes to prevent the shell from interpreting it.
tar -xf archive.tar --wildcards '*.js'
Add files to a tar archive
To add files or directories to an existing tar archive, use the
For instance, to add a file named
archive.tar, use the following command:
tar -rvf archive.tar newfile
Remove files from a tar archive
To remove files from an archive, use the
For example, to remove the file
archive.tar, use the following command:
tar --delete -f archive.tar file1