Tape Archive or tar is a file format for creating files and directories into an archive while preserving filesystem information such as permissions. We can use the tar command to create tar archives, extract the archives, view files and directories stored in the archives, and append files to an existing archive. Tar is a simple yet powerful archiving utility.
This guide will walk you through creating and extracting tar archives on your Linux machine.
In most Linux distributions, you will have tar pre-installed. However, in case you don’t. If that’s your case, use the commands:
Use apt package manager as:
On Arch-based distributions:
On REHL Family, use yum as:
Tar File Formats
Tar supports uncompressed and compressed archives. Common extensions of the tar archives include:
- .tar – A raw tar file.
- .tar.gz, .tgz, .tar.gzip – Gzip tar archive.
- .tar.bz2, .tbz, .tbz2, .tar.bzip2 – Bzipped tar archive.
- .tar.Z, .Z, .taz – Compress tar archive.
Tar Basic Usage
Using tar in the command-line adopts the general syntax:
We start by invoking the tar utility, followed by the operation to perform. Operations may include:
- -c – create an archive
- -x – extract archive
- -t – shows files and directories in the archive.
Next, we pass the options to modify the utility’s behavior. These options can be -v for verbose or -f for archive name, -z to filter the archive via gzip, and more.
Finally, we pass the archive name and the files and directories to add to the archive.
How to Create a Simple Archive
As we mentioned above, tar supports a range of compressions. To specify the type of archive to create, add the desired extension to the file name. For example, to create a gzipped tar archive, enter the filename as myarchive.tar.gz
Use the command below to create the archive with the files: file1, file2, file3, file4
The -c tells tar to create a new archive. The -f flag specifies the file name.
How to Tar a Directory
To create a tar archive in a directory with all the files and sub-directories, pass the directory path. From there, tar recursively finds all the files and directories and adds them to the archive.
An example command is:
You can suppress recursive directory archiving using the –no-recursion flag.
How to Show Contents of a tar Archive
To view the files and directories in a tar archive, we use the -t option. For example:
How to Extract a Tar Archive
To extract a tar archive, use the -x operation flag for extraction. You can pass any tar compression format such as gzip, lzma, bz2, and more.
The most common command for extracting tar archive files is:
To extract a simple tar archive:
The command will extract the contents of the archive to the current directory.
How to Extract a Specific File
In some instances, you may need to get specific files from an archive. To do this, pass the filenames to the tar command separated by space.
How to extract to a specific directory
As mentioned, tar extracts the archive in the current working directory. To change the directory where the archived files are extracted, use the -C flag as:
How to Append Files to an Archive
To add files to an existing archive, we use the -r fo append followed by the name of the archive and the files to add.
How to Remove Files from an Archive
Using the –delete option, we can remove specific files from an archive as:
Tar is a valuable utility in Linux, most packages are archived in tar format, and it is helpful to know how to use the tool.